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:

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




by Lee Everest, M.S.

Search


Ads

Calendar

«  July 2010  »
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
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 2010 Lee Everest's SQL Server, etc. weblog