Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
SQL
  UPDATE mbTbl
  SET mbTbl.Status=erpTbl.[Status],mbTbl.ModifiedDate=GETDATE(),erpTbl.IsSent=1
  from openquery (LINKED, 'select * from mobileapp.student_status_tbl') as mbTbl
  INNER JOIN
  OPENROWSET('SQLNCLI',
'server=xxx.xx.x.xxxxx;Uid=xxxxx;Pwd=xxxx123;
database=[yd];
Persist Security Info=False',
'select * from AlahlidbN.dbo.mstudentstatus') as erpTbl
  ON erpTbl.Student_id=mbTbl.Student_PId
  WHERE erpTbl.IsSent=0  and erpTbl.Stage=mbTbl.Stage_


What I have tried:

gave error like that

The multi-part identifier "erpTbl.IsSent" could not be bound.
Posted
Updated 30-Sep-19 4:29am
Comments
ZurdoDev 30-Sep-19 8:44am    
I don't believe you can update fields from more than one table at the same time like this.
ZurdoDev 30-Sep-19 8:45am    
You need 2 update statements. Possibly create a temp table to store the ids that you need to work with but you can't update 2 tables with a single update statement.
Aitzaz Ahsan 30-Sep-19 8:54am    
Thanks a lot i got your point.

I'd suggest to use linked server[^]. Then you'll be able to act with databases as on single server ;)

For further details, please see:
Create Linked Servers (SQL Server Database Engine) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Comments
phil.o 30-Sep-19 6:36am    
5'd!
Maciej Los 30-Sep-19 6:37am    
Thank you, Phil.
ZurdoDev 30-Sep-19 8:44am    
It actually looks like they are trying to update fields in 2 different tables with the same update statement. Can't do that as far as I know.

mbTbl.Status=erpTbl.[Status],...,erpTbl.IsSent=1
Aitzaz Ahsan 30-Sep-19 8:53am    
yes i m trying to update two table fields using single query
Maciej Los 1-Oct-19 2:18am    
So, you can't update 2 tables in a single sql query. ZurdoDev is right.
As mentioned in comments, the update statement will only update 1 table at a time. Hence
SQL
Update table SET field = value


You will need to do 2 different update statements.

One option is to store in a temp table all the ids of the fields that you need to touch so that you can do 2 updates and make sure you get all of the relevant fields. However, depending on your logic, a temp table may not be required.

Also, you should likely do this in a transaction so that if one update fails, both are rolled back.
 
Share this answer
 
Comments
Maciej Los 1-Oct-19 2:18am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900