Click here to Skip to main content
15,888,303 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
SQL
CREATE PROC TASK_UPDATE @TCODE varchar(100)
AS
UPDATE CMO_TBL_TASK SET STATUS='Completed' WHERE TASKCODE IN (@TCODE)

EXEC TASK_UPDATE 'TS001 TS002'


TCODE   TNAME              TDESC                         _BY     STATUS
TS001	Client Meeting	   describing project to client	  WS	  Open
TS002	Part Management	   Module Completion	          WS	  Open
TS003	Update 	           Update values	          WS	  Progress


If i exec above procedure, 0 rows affected is showing, i dono how the alignment of passing parameters.

I have to pass all the 3 tcode values to the above stored procedure and all the three rows should be updated in a single execution. will the above sp works, if so , help me to pass tcode values or help me with other sp.

[edit]SHOUTING removed, table formatted - OriginalGriff[/edit]
Posted
Updated 21-Dec-11 23:21pm
v3
Comments
OriginalGriff 22-Dec-11 5:22am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
Amir Mahfoozi 24-Dec-11 11:01am    
How are you ? :)
hitech_s 26-Dec-11 0:19am    
separate TCODE with comas while sending

try this way .

SQL
CREATE PROC TASK_UPDATE @TCODE varchar(100)
AS
exec('UPDATE CMO_TBL_TASK SET STATUS=''Completed'' WHERE TASKCODE IN ('+@TCODE+')')
 
EXEC TASK_UPDATE '''TS001'',''TS002'',''TS003'''


OR

SQL
CREATE PROC TASK_UPDATE @TCODE varchar(100)
AS
UPDATE CMO_TBL_TASK SET STATUS='Completed' WHERE  charindex(','+cast(TASKCODE  as varchar(50))+',',@TCODE)>0
 
EXEC TASK_UPDATE 'TS001,TS002,TS003'
 
Share this answer
 
v5
Comments
patil.ravi035 22-Dec-11 6:33am    
hi above Query will work fine
EXEC TASK_UPDATE 'TS001 TS002' it does not work.'TS001 TS002' treated as one ID.
use
SQL
EXEC TASK_UPDATE 'TS001'
EXEC TASK_UPDATE 'TS002'
EXEC TASK_UPDATE 'TS003'


or use looping for changing id for each iteration.
 
Share this answer
 
Comments
[no name] 22-Dec-11 23:07pm    
this query also work fine just check it.
SQL
--Create test table
Create Table CMO_TBL_TASK (TASKCODE varchar(20), STATUS varchar(20))
insert into CMO_TBL_TASK values ('TS001','Open')
insert into CMO_TBL_TASK values ('TS002','Open')
insert into CMO_TBL_TASK values ('TS003','Progress')


--Create 
CREATE PROC TASK_UPDATE @TCODE varchar(100)
AS
UPDATE CMO_TBL_TASK SET STATUS='Completed' WHERE TASKCODE IN (SELECT DATA FROM dbo.Split(@TCODE,','))

EXEC TASK_UPDATE 'TS001,TS002'


--Use any of the split function to split the data

CREATE FUNCTION dbo.Split
(
	@RowData nvarchar(2000),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Id int identity(1,1),
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1

	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))

	Return
END

split function is picked from the following :
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
 
Share this answer
 
v2
First create a split function in your database :
http://blog.codelab.co.nz/2009/07/29/split-function-for-t-sql-using-xml/[^]

Then change your query to this :
SQL
CREATE PROC TASK_UPDATE @TCODE varchar(100)
AS
UPDATE CMO_TBL_TASK SET STATUS='Completed' WHERE TASKCODE IN ( select * from dbo.split(',', @TCODE) )


Then call it in this way :
SQL
TASK_UPDATE 'TS001,TS002'


Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900