Have I been smoking something?? Insert construction question.

  • marc.snoeys (5/17/2013)


    That's basically my point 🙂

    There is nothing wrong with the INSERT-statement at all.

    But, my personal opinion is that it would be more intuitive to be able to write an INSERT-statement just like an UPDATE-statement.

    But that's merely a discussion on improvement/addition of syntax.

    There is nothing preventing you from doing something like this:

    insert mytable(col1,col2,col3,col4)

    select

    col1 = myothertable.col3

    , col2 = getdate()

    , col3 = newid()

    , col4 = 'foo'

    from myothertable

    where foo='bar';

    If you find that more readable, you can handle it like that, but this doesn't guarantee that what you alias as col2 actually ends up in col2 - that relies on your good skills.

    Personally, if I have a large number of columns in an insert, I like to put the column name in a comment in the select:

    insert mytable(col1,col2,col3,col4)

    select

    myothertable.col3 -- col1

    , getdate() -- col2

    , newid() -- col3

    , 'foo' -- col4

    from myothertable

    where foo='bar';

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Sergiy (5/22/2013)


    marc.snoeys (5/17/2013)


    That's basically my point 🙂

    There is nothing wrong with the INSERT-statement at all.

    But, my personal opinion is that it would be more intuitive to be able to write an INSERT-statement just like an UPDATE-statement.

    But that's merely a discussion on improvement/addition of syntax.

    Try to insert 2 or more records using your proposed syntax.

    Or implement a check for existence of a key value in the table.

    I could hardly name it any kind of improvement.

    That's a non-argument because you can't insert two or more records with the VALUES-syntax as well 😉

    "Walking on water and developing software from a specification are easy if both are frozen." -- Edward V. Berard, "Life-Cycle Approaches"

  • mister.magoo (5/22/2013)


    marc.snoeys (5/17/2013)


    That's basically my point 🙂

    There is nothing wrong with the INSERT-statement at all.

    But, my personal opinion is that it would be more intuitive to be able to write an INSERT-statement just like an UPDATE-statement.

    But that's merely a discussion on improvement/addition of syntax.

    There is nothing preventing you from doing something like this:

    insert mytable(col1,col2,col3,col4)

    select

    col1 = myothertable.col3

    , col2 = getdate()

    , col3 = newid()

    , col4 = 'foo'

    from myothertable

    where foo='bar';

    If you find that more readable, you can handle it like that, but this doesn't guarantee that what you alias as col2 actually ends up in col2 - that relies on your good skills.

    Personally, if I have a large number of columns in an insert, I like to put the column name in a comment in the select:

    insert mytable(col1,col2,col3,col4)

    select

    myothertable.col3 -- col1

    , getdate() -- col2

    , newid() -- col3

    , 'foo' -- col4

    from myothertable

    where foo='bar';

    Playing the devil's advocate I could say that both your suggestions are a kind of mimicking the UPDATE-syntax with the INSERT 🙂

    But the layout of the code is more important than the syntax.

    And I also use your second style of writing the INSERT-statement.

    I even put the column-names on a seperate line as well, with comment if it's appropriate.

    "Walking on water and developing software from a specification are easy if both are frozen." -- Edward V. Berard, "Life-Cycle Approaches"

  • marc.snoeys (5/22/2013)


    That's a non-argument because you can't insert two or more records with the VALUES-syntax as well 😉

    Like this you mean?

    CREATE TABLE #test (

    ID INT

    )

    INSERT INTO #test (ID)

    VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)

    SELECT * FROM #test AS t

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (5/23/2013)


    marc.snoeys (5/22/2013)


    That's a non-argument because you can't insert two or more records with the VALUES-syntax as well 😉

    Like this you mean?

    CREATE TABLE #test (

    ID INT

    )

    INSERT INTO #test (ID)

    VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)

    SELECT * FROM #test AS t

    I stand corrected

    "Walking on water and developing software from a specification are easy if both are frozen." -- Edward V. Berard, "Life-Cycle Approaches"

  • You cannot call insert like that, = operator works while you are updating any row or table.

  • Bhaskar.Shetty (5/23/2013)


    You cannot call insert like that, = operator works while you are updating any row or table.

    Before making blanket statements like this, I find that it is always best to double-check, lest you are left looking silly in perpetuity:

    create table #test (ID int)

    insert into #test

    (ID)

    select Id = 1

    union all

    select Id = 2

    select *

    from #test as t

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin (5/23/2013)


    Bhaskar.Shetty (5/23/2013)


    You cannot call insert like that, = operator works while you are updating any row or table.

    Before making blanket statements like this, I find that it is always best to double-check, lest you are left looking silly in perpetuity:

    create table #test (ID int)

    insert into #test

    (ID)

    select Id = 1

    union all

    select Id = 2

    select *

    from #test as t

    First check the way insert statement has been written and tried, and solutions what you have given may be one from lot, there are plenty of other solution which you can use it for this purpose.

    before calling other silly better you readout the problem first, the guy has asked whether he can write the statement in that manner or not

  • Your statement:

    You cannot call insert like that, = operator works while you are updating any row or table.

    Is plainly wrong - I proved it in code. And now you are proving that you do not have the grace to admit it.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin (5/23/2013)


    Your statement:

    You cannot call insert like that, = operator works while you are updating any row or table.

    Is plainly wrong - I proved it in code. And now you are proving that you do not have the grace to admit it.

    I already adimited that the solution given by you is one from the lot, but there's no similarity between what tobar as asked for and your solutions, and if you know something more than others, its better you mention that, instead calling other silly, and nobody can become expert from attaching a freaking face on his own profile... 🙂

  • Hey all. I started this little drama and I did not foresee the avenues it would take (had I I still would have started it, but that is a different story). I really have learned some great possibilities to use with my insert statements. THANK YOU all for your input, challenges, and thoughts.

    <><
    Livin' down on the cube farm. Left, left, then a right.

  • Bhaskar.Shetty (5/23/2013)


    You cannot call insert like that, = operator works while you are updating any row or table.

    You mean the way Marc suggested?

    insert mytable(col1,col2,col3,col4)

    select

    col1 = myothertable.col3

    , col2 = getdate()

    , col3 = newid()

    , col4 = 'foo'

    from myothertable

    where foo='bar';

    The = isn't just used in updates, it's also the older way to assign column aliases.

    SELECT ThisIsTheColumnAlias = 1

    Run that, you get a single row (value 1) in a column that's called ThisIsTheColumnAlias. Since that's a valid form for the select, it's also valid for the insert...select.

    Create Table #Test (

    Num Int

    )

    Insert into #Test (Num)

    SELECT ThisIsANumber = 1

    Select * From #Test

    Drop table #Test

    Personally I still prefer the AS method for aliasing columns, though you can do some odd tricks with update using aliases like this.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • mister.magoo (5/22/2013)


    insert mytable(col1,col2,col3,col4)

    select

    col1 = myothertable.col3

    , col2 = getdate()

    , col3 = newid()

    , col4 = 'foo'

    from myothertable

    where foo='bar';

    The one draw back with this method, I just noticed :-D, is that you must specify all the columns in the table. If you are only updating 5 columns out of 20 ... Oh well.

    Tobar (5/16/2013)


    Change is always possible. There once was a time when you could not do "positional or named parameters". Hope springs eternal.

    Maybe there is hope that they will change it so that you don't have to specify all when you use "assignment style" or whatever it will be called. :w00t:

    <><
    Livin' down on the cube farm. Left, left, then a right.

  • GilaMonster (5/23/2013)


    Bhaskar.Shetty (5/23/2013)


    Since that's a valid form for the select, it's also valid for the insert...select.

    Very arguable.

    All those aliasings remain within SELECT and have no relevance to INSERT.

    INSERT takes values from a recordsert supplied in physical order of columns and ignores whatever aliases you assigned.

    See this example:

    insert mytable(col1,col2,col3,col4)

    select

    col2 = getdate()

    , col1 = myothertable.col3

    , col3 = newid()

    , col4 = 'foo'

    from myothertable

    where foo='bar';

    INSERT will put values into wrong columns despite your best effort of aliasing.

    _____________
    Code for TallyGenerator

  • Sergiy (5/23/2013)


    GilaMonster (5/23/2013)


    Since that's a valid form for the select, it's also valid for the insert...select.

    Very arguable.

    What, that it's valid? Since it passes a syntax check it's valid T-SQL, hard to argue that it's not valid (unless in SQL 2012 that aliasing form has been removed and a query using that aliasing form fails a syntax check).

    I never claimed it's a good idea or that it would magically prevent column order mistake, personally I don't like that form of aliasing and never use it. All I said was that it's a valid (ie correct, parsable) form of T-SQL.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Viewing 15 posts - 16 through 30 (of 47 total)

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