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 2005 Forums
 Transact-SQL (2005)
 Updating a group of records in SQL

Author  Topic 

kjmcsd
Starting Member

4 Posts

Posted - 2010-04-14 : 15:21:19
I am confused about how to update a group of records with SQL inside a stored proc.

Let's say I have 3 columns.

ID int

Name varchar(100)

Confirm bit



I want to update the confirm column for 10 names with one call to the stored proc. How do I do it? What do I need to pass to the stored proc?

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-14 : 15:27:33
pass list as a string of IN() ?

quote:
Originally posted by kjmcsd

I am confused about how to update a group of records with SQL inside a stored proc.

Let's say I have 3 columns.

ID int

Name varchar(100)

Confirm bit



I want to update the confirm column for 10 names with one call to the stored proc. How do I do it? What do I need to pass to the stored proc?


Go to Top of Page

kjmcsd
Starting Member

4 Posts

Posted - 2010-04-14 : 15:35:42
I am trying to understand. list as a string IN()?
Go to Top of Page

kjmcsd
Starting Member

4 Posts

Posted - 2010-04-14 : 16:06:38
Ok I think I understand now due to research of SQL IN. I do not think that will work.

Can you post your suggestion using SQL?
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-14 : 16:44:35
sure here is an example:


create procedure updatetest
@name varchar(500),
@CONFIRM varchar(1)
as
DECLARE @SQLSTRING NVARCHAR(500);
SET @SQLSTRING = N'UPDATE TEST1 SET CONFIRM = '+@CONFIRM+' where name in ('+@name+')';
EXECUTE SP_EXECUTESQL @SQLSTRING;
--end of proc

EXECUTE updatetest '''a'',''b'',''c''', '0'


quote:
Originally posted by kjmcsd

Ok I think I understand now due to research of SQL IN. I do not think that will work.

Can you post your suggestion using SQL?

Go to Top of Page

kjmcsd
Starting Member

4 Posts

Posted - 2010-04-15 : 09:30:05
My fault I should have added this. Each confirm value for each name could be different. In that case that would not work either.
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-15 : 11:53:55
Lets be a little Cheesy:


create procedure updatetest
@name varchar(MAX),
@CONFIRM varchar(MAX)
as
DECLARE @SQLSTRING NVARCHAR(MAX);
SET @SQLSTRING = N'UPDATE TEST1 SET CONFIRM = '+@CONFIRM+' where name in ('+@name+')';
EXECUTE SP_EXECUTESQL @SQLSTRING;
GO
--end of proc

EXECUTE updatetest '''a'',''b'',''c''', 'CASE name WHEN ''a'' then 1 when ''b'' then 0 when ''c'' then 1 end'



quote:
Originally posted by kjmcsd

My fault I should have added this. Each confirm value for each name could be different. In that case that would not work either.

Go to Top of Page
   

- Advertisement -