Insert Into with nested Selects

  • Hello,

    I have a table HemAnimal and a table HemTransfer.

    For some reason there is no row created in the table HemTransfer when an animal is added to the database by the program.

    So I need to manually create the HemTransfer event. I would like to do this using a script instead of copy row, paste row and edit fields manually.

    I came up with this:

    Use Lely

    Set Identity_Insert dbo.HemTransfer ON

    Insert into dbo.HemTransfer (TraTrtId,TraAniId,TraDate)

    Values (2,(Select HemAnimal.AniId Where HemAnimal.AniUserNumber = 123456),(Select HemAnimal.AniBirthday Where HemAnimal.AniUserNumber = 123456))

    Set Identity_Insert dbo.HemTransfer OFF

    Basically this says "Create a row and set the transfer type to birthday, get the ID from the HemAnimal table where the Animal Number is 123456, set the transfer date equal to the Animals birthdate from the HemAnimal table for Animal 123456."

    I get this error:

    Msg 4104, Level 16, State 1, Line 4

    The multi-part identifier "HemAnimal.AniUserNumber" could not be bound.

    Msg 4104, Level 16, State 1, Line 4

    The multi-part identifier "HemAnimal.AniId" could not be bound.

    Msg 4104, Level 16, State 1, Line 4

    The multi-part identifier "HemAnimal.AniUserNumber" could not be bound.

    Msg 4104, Level 16, State 1, Line 4

    The multi-part identifier "HemAnimal.AniBirthday" could not be bound.

    What am I doing wrong?

  • Your subselects don't have a FROM

    Select HemAnimal.AniId Where HemAnimal.AniUserNumber = 123456

    It seems that your inserts are over complicated but without more details about the tables and what you are trying to do it is difficult to offer any direction.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean is right, you're overcomplicating things. You can use INSERT INTO...SELECT instead of values.

    INSERT INTO dbo.HemTransfer (

    TraTrtId

    ,TraAniId

    ,TraDate

    )

    SELECT 2

    ,ha.AniId

    ,ha.AniBirthday

    FROM HemAnimal ha --This is a guess

    WHERE ha.AniUserNumber = 123456

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Try the following:

    Use Lely;

    GO

    Set Identity_Insert dbo.HemTransfer ON;

    go

    Insert into dbo.HemTransfer (TraTrtId,TraAniId,TraDate)

    Select 2, HemAnimal.AniId, HemAnimal.AniBirthday from HemAnimal Where HemAnimal.AniUserNumber = 123456;

    go

    Set Identity_Insert dbo.HemTransfer OFF;

    go

  • Why are we inserting into an IDENTITY column???? :ermm::blink::crying::pinch::sick:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (4/7/2014)


    Why are we inserting into an IDENTITY column???? :ermm::blink::crying::pinch::sick:

    Sounds to me like the OP is going inserting data that was missed in the original programming. Therefore you need to insert those values into the identity column to ensure RI.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (4/7/2014)


    Jeff Moden (4/7/2014)


    Why are we inserting into an IDENTITY column???? :ermm::blink::crying::pinch::sick:

    Sounds to me like the OP is going inserting data that was missed in the original programming. Therefore you need to insert those values into the identity column to ensure RI.

    I hope that's the reason and not some misconception that PK reusage is a good idea or worse. I'm hoping the OP will answer on this one.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

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