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
 challenging query!!

Author  Topic 

cqldba303
Starting Member

16 Posts

Posted - 2012-02-28 : 12:04:22
I have two tables: table1 and table2

I have following columns in table1: id,name

I have following columns in table2:p_key,desc

I need to update table1 based on condition from both table1 and table2.

How should i do this?

for example I wanted to do:

update table1 set id=1 where table2.desc='solution'

but this update statement is giving me error?

what should be correct statement?

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-28 : 12:42:36
what's the relationship between the tables?

Generally you issue a statement like this

UPDATE t1 SET
[ID] = 1
FROM
table1 AS t1
JOIN table2 AS t2 ON t2.<COLUMN> = t1.<COLUMN>
WHERE
t2.[desc] = 'solution'


Basically -- write a SELECT statement to verify that the rows are correct, then just change the SELECT part to an UPDATE.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-28 : 12:45:13
quote:
Originally posted by cqldba303

I have two tables: table1 and table2

I have following columns in table1: id,name

I have following columns in table2:p_key,desc

I need to update table1 based on condition from both table1 and table2.

How should i do this?

for example I wanted to do:

update table1 set id=1 where table2.desc='solution'

but this update statement is giving me error?

what should be correct statement?


so are you basically stating that there's no relationship between tables?
your above update doesnt make much sense

you would be better off explaining with some data what exactly you're looking at

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-28 : 13:56:18
UPDATE table1
SET id = 1
FROM table2
WHERE desc = 'solution'

???

Which I hop you see, that even if it works, makes no sense at all

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -