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
 General SQL Server Forums
 New to SQL Server Programming
 Inserting records from table1 to table2

Author  Topic 

obert
Starting Member

1 Post

Posted - 2011-04-04 : 04:38:38
Hi, am trying to insert or update my profile table with records from table ProfileOffLineData, so during the insert, the the query should check if there are no similar entries in the destination table, if there similar records it should update. I have written the following query but am getting the this error: Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "ProfileOffLineData.CustID" could not be bound.


Here is the mssql query:

if exists (select t1.CustID from Profile t1, ProfileOffLineData t2 where t1.CustID = t2.CustID )
begin
update Profile set Name = ProfileOffLineData.Name, Address_1 = ProfileOffLineData.Address_1, [User_Name]=ProfileOffLineData.[User_Name]
where Profile.CustID = ProfileOffLineData.CustID
end
else
begin
insert into Profile(CustID,Name,Address_1,[User_Name]) select CustID,Name,Address_1,[User_Name] from ProfileOffLineData
end

NeilG
Aged Yak Warrior

530 Posts

Posted - 2011-04-04 : 05:03:18
You'll need to have a from clause in the update statement as the error is basically saying your referenceing a column but it's unsure where it's located

I's suspect its this where Profile.CustID = ProfileOffLineData.CustID

Try having

UPDATE P
SET Name = POD.Name, Address_1 = POD.Address_1, [User_Name]=POD.[User_Name]
FROM
Profile P
INNER JOIN ProfileOffLineData POD ON (P.CustID = POD.CustID)

Go to Top of Page
   

- Advertisement -