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 2000 Forums
 SQL Server Development (2000)
 Simple update-question

Author  Topic 

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2004-09-10 : 06:42:38
I need to update two money-fields in one of my tables where the second value is always the same as the first but in a different currency. Can I do like this:

UPDATE table SET Volume = SUM(Something), Volume_euro = Volume * @Rate
FROM ....

or must I do like this:

UPDATE table SET Volume = SUM(Something), Volume_euro = SUM(Something) * @Rate
FROM ....

Or doesn't it matter? The SUM(Something) is actually a CASE-statement with 6 different options...

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-09-10 : 07:22:50
You'd have to do the second option, either that or do two update statements...
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-09-10 : 07:52:19
Actually neither will work.
Try it with a temp table to get the syntax right.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2004-09-10 : 08:01:28
Umh...what...? I'm using option 2 as we speak and it's working like charm...I'm just asking because it would make my code shorter and more readable, I'll post the full statement if you like...

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-09-10 : 08:14:27
To clarify, the first statement won't work as aliased column names cannot be used within the same query as the optimiser cannot work out what you are refering to at the point of compiling the query... The second option doesn't use the alias so will work no problem...
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2004-09-10 : 08:19:12
You're confusing me here, where are the alias?? "Volume" and "Volume_euro" are both reald fieldnames in the table "table" (which naturally has another name). What I saw as the problem with this is that the update to Volume_euro actually has to happen *after* Volume has been updated to give the right result. I guess a simple test would be appropriate here...

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2004-09-10 : 08:23:01
I guess I should have guessed this by myself, the following test shows that option 1 doesn't work at all:

DECLARE @myTable table (Volume int, Volume_euro int)
INSERT INTO @myTable SELECT 100, 200

SELECT * FROM @myTable
UPDATE @myTable SET Volume = 200, Volume_euro = Volume * 2
SELECT * FROM @myTable

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-09-10 : 08:23:30
Sorry, I got that wrong...

You cannot use the first statement due to the fact that you cannot be sure which update will happen first...
Go to Top of Page
   

- Advertisement -