Question : VARCHAR(MAX) & VARBINARY(MAX) from php

I am storing documents in a varbinary(max) column in a table.
Obviously, I need to be able to retreive those documents.

Unfortunately, I am accessing the db from php code, and there doesn't seem to be any facility to stream back large columns by chunk.

Hence I am looking into a way to transform a single column into a resultset using a stored procedure.

I managed to do that using a CLR stored proc, but it seems I should be able to do that using straight TSQL.

I'm using the naive approach leads me to a wall, since I can't seem to output a constructed resultset from a stored proc. For instance, I can't find a way to output the results of a while loop as a resultset, which seems like something I should be able to do.

I looked into cursor varying output stuff but that didn't lead me anywhere at all.


So, to summarize:

1) Is there a way in tsql to stream a varbinary(max) back to the client (there is the .WRITE construct to update it). The only way seems to use the substring function, but then you have to do multiple request, which is massively inneficient.

2) If not, how can I construct a TSQL stored procedure, that splits a varbinary in chunks and return them as a resultset

For the record, here is how I did it in a CLR stored proc:


[Microsoft.SqlServer.Server.SqlProcedure]
    public static void ReadBLOB(String table, int id, String field) {
        using (SqlConnection connection = new SqlConnection("context connection=true")) {
            connection.Open();
            SqlCommand command =
                new SqlCommand(String.Format("SELECT [{0}] FROM [{1}] WHERE id = {2}", field, table, id), connection);
           
            byte[] buffer = new byte[65535];
            SqlDataRecord record = new SqlDataRecord(new SqlMetaData("data", SqlDbType.VarBinary, SqlMetaData.Max));
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
            SqlContext.Pipe.SendResultsStart(record);
            while (reader.Read()) {
                long offset = 0;
                long byteCount;
                while ((byteCount = reader.GetBytes(0, offset, buffer, 0, buffer.Length)) > 0) {
                    if (byteCount == buffer.Length) {
                        record.SetValue(0, buffer);
                    }  else {
                        byte[] tmp = new byte[byteCount];
                        Array.Copy(buffer, tmp, byteCount);
                        record.SetValue(0, tmp);
                    }
                    SqlContext.Pipe.SendResultsRow(record);
                    offset += byteCount;
                }
            }
            SqlContext.Pipe.SendResultsEnd();
        }
    }


Thanks


Blaise

Answer : VARCHAR(MAX) & VARBINARY(MAX) from php

i hope this will help for you
http://www.codeproject.com/cs/database/blob_compress.asp
Random Solutions  
 
programming4us programming4us