Call now: (800) 766-1884  



 Home


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA



 Articles
 Services
 SQL Server Scripts
 Scripts Menu



 

 

 

   
  SQL Server Tips by Gama and Naughter


Variant data type

 

The variant data type can store data from many different data types and a reference indicating which data type it is. This is very useful for an input parameter from an SP that can handle many different data types or a column in a table with heterogeneous data. The following example shows how to work with it:

 

--declare variables

DECLARE @v sql_variant, @i int, @c char(4), @t varchar(4)

--set variables’ values for test

SET @i=13456

SET @c='abc'

SET @t='abc'

--assign integer to variant and get properties

SET @v=@i

SELECT SQL_VARIANT_PROPERTY(@v,'BaseType'), SQL_VARIANT_PROPERTY(@v,'TotalBytes'), SQL_VARIANT_PROPERTY(@v,'MaxLength')

--assign char to variant and get properties

SET @v=@c

SELECT SQL_VARIANT_PROPERTY(@v,'BaseType'), SQL_VARIANT_PROPERTY(@v,'TotalBytes'), SQL_VARIANT_PROPERTY(@v,'MaxLength')

--assign varchar to variant and get properties

SET @v=@t

SELECT SQL_VARIANT_PROPERTY(@v,'BaseType'), SQL_VARIANT_PROPERTY(@v,'TotalBytes'), SQL_VARIANT_PROPERTY(@v,'MaxLength')

 

CREATE TABLE #tmp(Vcolumn SQL_VARIANT)

INSERT #tmp VALUES(@i)

INSERT #tmp VALUES(@c)

INSERT #tmp VALUES(@t)

SELECT Vcolumn, SQL_VARIANT_PROPERTY(Vcolumn,'BaseType') FROM #tmp

DROP TABLE #tmp

 

The variant data type keeps track of the original data type, current length and maximum length, available through these properties.

 


The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter

 http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm
 

 

Burleson Consulting Remote DB Administration


 

 


 

 

 

 

 
Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.
 


Burleson Consulting
SQL Server database support

 

Copyright © 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter