T-SQL

  • For datatype , this doubt clarification i've posted one answered already.

    varchar accepts both number and character and without single quote number also like (1,'b') or ('1','b')

    both possible in sqlserver2005

  • This was removed by the editor as SPAM

  • ya i'm Agreeing this.

    First of all insert into #test values(2,'A'),(' ' ,'B') this is not there in sqlserver 2005.

    So now we can't consider data type issue.

    IF u go with sqlserver2008 ,yes of course we need to consider about data types.

    Thank you

  • Hi Sutha,

    Still you have same issue in SQL Server 2005 using old method.

    Try this and compare the results.

    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

    Implicit conversion matters both in 2005 and 2008 when using union in the select before inserting the records.

  • That new syntax in 2008 is handy. I'll have to try it out. Nice to learn something new. Thanks.

  • 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • 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.

  • Thanks for the question - clearly highlights the pitfalls in implicit conversions.

  • 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

  • Bradley Deem (3/22/2011)


    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.

  • Interesting, I did not realize that the Table Value Constructor behaved like a UNION. It makes sense now that it has been pointed out.

  • 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.

  • 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.

  • 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 .

  • This was an excellent learning question.

    Thank you

Viewing 15 posts - 16 through 30 (of 48 total)

You must be logged in to reply to this topic. Login to reply