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 2012 Forums
 Transact-SQL (2012)
 How to Change the Value in a Field to a Variable

Author  Topic 

Palermo
Starting Member

25 Posts

Posted - 2013-10-16 : 17:48:01
I want to search or set, whichever way works, for a specific name in a field then change it to the value I have stored in a variable. If I wanted to search for the name 'Ric Flair' in a table and replace it with the variable Harely Race, how would I do that? Needless to say this doesn't work :

DECLARE @newname nchar(20)
SET @newname = 'Harely Race';
FROM dbo.Wrestlers
WHERE Grappler = 'Ric Flair';

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-16 : 17:54:23
UPDATE dbo.Wrestlers
SET Grappler = @newname
WHERE Grappler = 'Ric Flair'

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Palermo
Starting Member

25 Posts

Posted - 2013-10-16 : 17:56:30
Thanks but I know how to do it using the UPDATE command, I want to know how to do it using a variable.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-16 : 17:59:53
I'm confused. What I posted IS using a variable.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-16 : 18:02:03
Let me be more clear as I only provided the UPDATE statement, assumed you knew you still need to declare/set the variable:

DECLARE @newname nchar(20)
SET @newname = 'Harely Race';

UPDATE dbo.Wrestlers
SET Grappler = @newname
WHERE Grappler = 'Ric Flair'

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Palermo
Starting Member

25 Posts

Posted - 2013-10-16 : 18:05:34
Thanks that did it!

Yes I appreciate it makes no sense whatsoever to use a variable instead of the UPDATE command by itself but it's for coursework and the notes state look up how to use a variable in Transact-SQL.
Go to Top of Page
   

- Advertisement -