April 7, 2014 at 10:46 am
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?
April 7, 2014 at 10:52 am
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/
April 7, 2014 at 11:20 am
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
April 7, 2014 at 11:20 am
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
April 7, 2014 at 1:47 pm
Why are we inserting into an IDENTITY column???? :ermm::blink::crying::pinch::sick:
--Jeff Moden
Change is inevitable... Change for the better is not.
April 7, 2014 at 2:23 pm
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/
April 7, 2014 at 4:32 pm
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
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply