Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Insert Into with nested Selects Expand / Collapse
Author
Message
Posted Monday, April 7, 2014 10:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 8, 2014 1:49 PM
Points: 7, Visits: 14
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?



Post #1559189
Posted Monday, April 7, 2014 10:52 AM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:04 AM
Points: 13,093, Visits: 12,571
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1559191
Posted Monday, April 7, 2014 11:20 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 3,808, Visits: 8,562
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1559197
Posted Monday, April 7, 2014 11:20 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:50 AM
Points: 20,744, Visits: 32,557
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





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1559198
Posted Monday, April 7, 2014 1:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:02 PM
Points: 35,397, Visits: 31,955
Why are we inserting into an IDENTITY column????

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

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1559257
Posted Monday, April 7, 2014 2:23 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:04 AM
Points: 13,093, Visits: 12,571
Jeff Moden (4/7/2014)
Why are we inserting into an IDENTITY column????


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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1559272
Posted Monday, April 7, 2014 4:32 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:02 PM
Points: 35,397, Visits: 31,955
Sean Lange (4/7/2014)
Jeff Moden (4/7/2014)
Why are we inserting into an IDENTITY column????


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

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1559300
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse