Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL


T-SQL

Author
Message
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8323 Visits: 11579
A nice question. Thanks!
My only gripe (yes, I have one - nopt a big one, though) is that the difference between 1 and '1' is easily lost. I almost chose option 1 because I thought the three INSERT blocks were all identitical. Since the goal of the question was to test understanding of implicit conversions, not detailed proofreading, a comment or other explicit note about these subtle differences would have been nice.


Gopinath Srirangan (3/22/2011)
Still you have same issue in SQL Server 2005 using old method.

True. And even in a less elaborate syntax than what you post:
create table #test(c1 varchar(10), c2 varchar(10));
go

insert into #test
select '1','A' union all
select '','B';

insert into #test
select 2,'D' union all
select '','E';

insert into #test
select '','G' union all
select '','H';

go

select * from #test;




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Rose Bud
Rose Bud
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1577 Visits: 1062
Gopi>>The important thing that i want to bring out is the datatype of the final result set is based on the first statement in the insert list.

The examples in your question illustrate this point perfectly. Yet another example of the dangers of implicit conversion.

Gopi>>Hope this is helpful ?

More than you thought: You may consider me ignorant, but I have been using 2008 for a year and I never knew you could insert multiple records with one single INSERT statement. Furthermore, I would not have guessed that doing so would behave like a union. Your question and explanation drive these points home quite well. Thank you.
Duncan Pryde
Duncan Pryde
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3396 Visits: 1552
Thanks for the question - clearly highlights the pitfalls in implicit conversions.
Bradley Deem
Bradley Deem
Mr or Mrs. 500
Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)

Group: General Forum Members
Points: 567 Visits: 1248
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.

create table #test(c1 varchar(10), c2 varchar(10));

insert into #test
select '','I' union all
select 3,'J';

select * from #test;



Which returns
c1 c2
0 I
3 J

Data Type Prescendance http://msdn.microsoft.com/en-us/library/ms190309.aspx
tejaswini.patil
tejaswini.patil
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1060 Visits: 234
Bradley Deem (3/22/2011)
[quote]UMG Developer (3/21/2011)

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.


Thanks Bradley. Quite interesting learning for me.
Cliff Jones
Cliff Jones
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4069 Visits: 3648
Interesting, I did not realize that the Table Value Constructor behaved like a UNION. It makes sense now that it has been pointed out.
cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
Hugo Kornelis (3/22/2011)
A nice question. Thanks!
My only gripe (yes, I have one - nopt a big one, though) is that the difference between 1 and '1' is easily lost. I almost chose option 1 because I thought the three INSERT blocks were all identitical. Since the goal of the question was to test understanding of implicit conversions, not detailed proofreading, a comment or other explicit note about these subtle differences would have been nice.

Except the code was nicely color coded so the text was a different color than the integers.
SanDroid
SanDroid
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1414 Visits: 1046
Cliff Jones (3/22/2011)
Interesting, I did not realize that the Table Value Constructor behaved like a UNION. It makes sense now that it has been pointed out.


Great Question. I also have not used SQL 2008 new features enough to see this happen yet.
Thanks for pointing that out.
pksutha
pksutha
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 58
Hi Gopi,



Fantastic answer and Explanation..
good
good

select * into #D1 from (
select '1' as c1,'A' as c2
union
select '','B'
) T

select * from #D1
go

select * into #D2 from (
select 1 as c1,'A' as c2
union
select '','B'
) T
select * from #D2

drop table #D1
drop table #D2




Note : Really and very exact explanation....

i understood very nice...

thank u .
c00578
c00578
SSC-Addicted
SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)

Group: General Forum Members
Points: 491 Visits: 70
This was an excellent learning question.

Thank you
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search