SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert Into with nested Selects


Insert Into with nested Selects

Author
Message
domleg
domleg
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 22
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?
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

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

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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16627 Visits: 19098
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
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39555 Visits: 38554
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




Cool
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)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86574 Visits: 41098
Why are we inserting into an IDENTITY column???? ErmmBlinkCryingPinchSick

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26195 Visits: 17539
Jeff Moden (4/7/2014)
Why are we inserting into an IDENTITY column???? ErmmBlinkCryingPinchSick


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.

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)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86574 Visits: 41098
Sean Lange (4/7/2014)
Jeff Moden (4/7/2014)
Why are we inserting into an IDENTITY column???? ErmmBlinkCryingPinchSick


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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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