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)
 Update Query with variable declaration

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-29 : 13:50:41
I have three tables to update, and want to be able to set a variable value in one spot rather than having to do it three places.

Right now, let's say I have to update a title in three tables by Number. I'm doing the following, but want to know if I can set variable values in one spot, then use that in the query. So, the next time an update needs to happen, I just change the Title and the Number (two changes, rather than six changes).

--Title variable here
--Number variable here

UPDATE WInfo
SET [Title] = 'Equipment'
WHERE [Number] like '01'
Go

--Now do Dictionary
Go
UPDATE Dictionary
SET [Title] = 'Capital'
WHERE [Number] like '01'
Go

--Now do WTable
Go
UPDATE WBS_Table
SET [WBS Title] = 'Capital'
WHERE [Number] like '01'
Go

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-08-29 : 13:52:09
Yes you can do that. That's what variables are for!

Tara
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2005-08-29 : 16:04:01
DECLARE
@Title varchar(30),
@Number varchar(5) --weird to have a number as varchar...

SELECT @Title = 'Equipment', @Number = '01'

UPDATE WInfo
SET [Title] = @Title
WHERE [Number] like @Number

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

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-29 : 16:34:16
Thanks Lumbago!
That's exactly what I finally got to work!
Go to Top of Page
   

- Advertisement -