March 22, 2011 at 9:55 am
Bradley Deem (3/22/2011)
UMG Developer (3/21/2011)
Interesting question, thanks, and a good reason you should always specify the data type for hardcoded items in the first set of values or in the first query when using UNION.I'm not sure this is true. It looks to me Data Type Precendence determines the data type not the order in the UNION or VALUES clause. Consider the following.
Bradley,
Thanks for pointing that out, of course I should have known better. Here is a link to the Table Value Constructor in BOL that mentions the data type precedence: http://msdn.microsoft.com/en-us/library/dd776382.aspx
March 22, 2011 at 9:57 am
Great question.
UMG,
Thanks for the link, that's good to know for sure.
March 22, 2011 at 10:12 am
thanks for the question
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 22, 2011 at 12:02 pm
This question is worth more than a point 😀
M&M
March 23, 2011 at 12:04 am
Bradley,
You are right. The datatype is determined by the order of precedence as explained in http://msdn.microsoft.com/en-us/library/ms190309.aspx . Thanks for pointing out this and i apologize everyone for making this mistake.
We can verify this by small example.
For integer datatype the order of precedence level is
1:bigint (highest precedence)
2:int
3:smallint
4:tinyint (lowest precedence) .
Note: For demo i have choosen only the above 4 datatype.
After running the below code Go to
DataBases -> SystemDataBases -> tempdb -> TestTable -> Columns . Its clear that column C1 is created with type bigint and C2 is created with type int based on the highest precedence in the select list.
-- Sample code
use tempdb
go
select * into TestTable from
(
select CAST(1 as smallint) as C1, CAST(6 as smallint) as C2
union
select CAST(2 as tinyint),CAST(6 as int)
union
select CAST(3 as bigint),CAST(6 as smallint)
union
select CAST(4 as int),CAST(6 as tinyint)
) T
Thanks again.
Gopi
March 23, 2011 at 12:20 am
Hi Gopi,
select * into TestTable from
(
select CAST(1 as smallint) as C1, CAST(6 as smallint) as C2
union
select CAST(2 as tinyint),CAST(6 as int)
union
select CAST(3 as bigint),CAST(6 as smallint)
union
select CAST(4 as int),CAST(6 as tinyint)
) T
Note: Good only....
But u've given only these are the orderwise datatype based on size . but i need how i can come to know query wise.. plz will u give example and we must see the difference in that example visually.
March 23, 2011 at 12:32 am
I am sorry i couldn't get your question. Can you pls explain you question in detail.
Thanks
March 23, 2011 at 1:27 am
Hi Gopi,
select * into TestTable from
(
select CAST(1 as smallint) as C1, CAST(6 as smallint) as C2
union
select CAST(2 as tinyint),CAST(6 as int)
union
select CAST(3 as bigint),CAST(6 as smallint)
union
select CAST(4 as int),CAST(6 as tinyint)
) T
Note: Practically i want to see the difference... will u give example for that..
March 23, 2011 at 2:21 am
As mentioned earlier, after executing the above code,
Go to
DataBases -> SystemDataBases -> tempdb -> TestTable -> Columns .
You can see that column C1 is created with type bigint and C2 is created with type int based on the highest precedence in the select list.
I think this could be the practical example you are expecting.
Thanks
Gopi
March 23, 2011 at 2:57 am
Nice question, thanks.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
March 23, 2011 at 8:12 am
greated question
Malleswarareddy
I.T.Analyst
MCITP(70-451)
March 24, 2011 at 7:03 am
Hi,
It's really very good question. thanks for sharing.
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
March 24, 2011 at 1:00 pm
Note:
insert into #test values (2,'D'),('','E') is equal to insert into #test select 2,'D' union select '','E'
Actually, its like:
insert into #test values (2,'D'),('','E') is equal to insert into #test select 2,'D' union all select '','E'
March 26, 2011 at 9:47 am
Really good question. Thanks.
Tom
March 27, 2011 at 9:46 pm
Excellent question. Good point emphasized when inserting with multiple value segments with single insert statement.
Viewing 15 posts - 31 through 45 (of 48 total)
You must be logged in to reply to this topic. Login to reply