Showing posts with label Stored Procedure. Show all posts
Showing posts with label Stored Procedure. Show all posts

Thursday, July 18, 2013

How to initialize / set a binary variable in T-SQL?

I have a stored procedure that expects a binary input parameter. I wanted to test it using a T-SQL statement in the query window. But i was wondering how to declare and set a binary variable using T-SQL.

How-i-fixed-it:
Just define the variable and set the bytes individually.

Example for a 6-Byte-Array:
declare @roi varbinary(6);

set @roi = 0x100010001000

exec sp_GetData_ROI 3001, @roi

SQLCLR SQL-Server UDF/SP binary parameter is cut after 8000 Bytes

I created an SQLCLR C# Stored Procedure which expects a paramter as large byte[] and returns the result in a large byte[].

Using the SQLBinary class as default, i run into the problem that the parameter was cut after 8000 Bytes.

How-i-fixed-it:
I found the right hints here:
http://stackoverflow.com/questions/840552/clr-udf-returning-varbinarymax

SqlBinary is VARBINARY(8000) while the default for SqlBytes is VARBINARY(MAX)

I found two ways to overcome the 8000 Bytes limit:

For the large return value i used:
SqlDataRecord record = new SqlDataRecord(
new SqlMetaData("Data", SqlDbType.VarBinary, -1));

For the input Parameter i used:
SqlBytes roiBytes

C# SQLCLR SqlMetaData with SqlDbType.Float column getting InvalidCastException on SqlDataRecord.SetFloat

In my SQLCLR C# Stored Procedure on SQL Server 2008 R2 i returned the results using a SqlDataRecord that has a column of SqlDbType.Float.

Trying to set the value using the SetFloat Method, i got an InvalidCastException.

How-i-fixed-it:
I found out that SqlDbType.Float is equal to a System.Double data type. Using the SetDouble Method it works fine.

Here i found the right hints:
http://msdn.microsoft.com/de-de/library/vstudio/system.data.sqldbtype(v=vs.80).aspx

http://msdn.microsoft.com/de-de/library/vstudio/microsoft.sqlserver.server.sqldatarecord.setfloat.aspx

http://msdn.microsoft.com/de-de/library/vstudio/microsoft.sqlserver.server.sqldatarecord.setdouble.aspx