Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert Into with Where clause error

Author  Topic 

osupratt
Posting Yak Master

238 Posts

Posted - 2010-08-16 : 14:56:09
Would anyone know what is wrong with this:

INSERT INTO TestFTHeader (Shift)
SELECT Misc_Text_Field_5
FROM Field_Ticket_Detail AS dtl
WHERE TestFTHeader.FTNumber = dtl.Field_Ticket_Number

? Sorry, probably something very easy. I'm getting this error:

The multi-part identifier "TestFTHeader.FTNumber" could not be bound.

When I take out the identifier and have the syntax look like this:

INSERT INTO TestFTHeader (Shift)
SELECT Misc_Text_Field_5
FROM Field_Ticket_Detail AS dtl
WHERE FTNumber = dtl.Field_Ticket_Number

I get this error:

Invalid column name 'FTNumber'.

I'm kind of lost on this. Thanks for anyone's help.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-08-16 : 15:23:34
Can you explain in words what you are trying to accomplish. From what I can see, I think what you need is an UPDATE and not an INSERT.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-16 : 15:23:48
You can't reference the table that is in the INSERT part like that. If it needs to be in the SELECT portion, then you have to JOIN to it.

INSERT INTO TestFTHeader (Shift)
SELECT Misc_Text_Field_5
FROM Field_Ticket_Detail AS dtl
JOIN TestFTHeader AS t
ON t.FTNumber = dtl.Field_Ticket_Number

This doesn't make too much sense to me though, are you sure you want an INSERT here?


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

osupratt
Posting Yak Master

238 Posts

Posted - 2010-08-16 : 15:52:28
Yes..you all are right. I should be doing an UPDATE. How do I do this from one table to another?


UPDATE TestFTHeader
SET
TestFTHeader.Shift = Field_Ticket_Detail.Misc_Text_Field_5
WHERE
TestFTHeader.FTNumber= Field_Ticket_Detail.Field_Ticket_Number
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-16 : 15:59:20
UPDATE t
SET Shift = f.Misc_Text_Field_5
FROM TestFTHeader t
JOIN Field_Ticket_Detail f
ON t.FTNumber = f.Field_Ticket_Number

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

osupratt
Posting Yak Master

238 Posts

Posted - 2010-08-16 : 17:09:25
Thanks very much. I saw this, but could not figure it out. I appreciate your help.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-16 : 18:21:58
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -