Lee posted on December 7, 2009 13:15

Geeking with sql_variant.

I found a really cool way use on the sql_variant type that I need to post, but wanted to share one thing that I saw today.  Run the following script in SQL Server 2008: 

IF OBJECT_ID ('test') IS NOT NULL
       DROP TABLE test
GO 

CREATE TABLE test
             (ID int
              ,VAL int
              )

GO

INSERT INTO test VALUES (1,2), (2, 2)
GO

IF OBJECT_ID ('test') IS NOT NULL
       DROP TABLE test
GO

CREATE TABLE test
              (ID int
              ,VAL sql_variant
              )
GO

INSERT INTO test VALUES (1,'A'), (2, 2)
GO

/*
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'A' to data type int.
*/

 

 

There are some blogs on the internet stating to stay clear of this data type, and I probably would have to agree. However, at my latest client, I actually used the type in a really neat scenario and it worked like a charm. Well, almost.  I"m finding some really weird stuff happening when you use the data type.  Will be writing that one up soon.

 

 

 

 

 

Lee

 

 

 

----------------------------

“On, Donner! On, Blitzen! On, Chewy! On, Tavo!  C'mon, Becto! And then, the reindeers used ta take off into da sky and fly across da sky, man!”

 


Posted in: TSQL  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  February 2012  »
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog