Friday, May 25, 2007

SubString in SQL Server for comma separated

CREATE PROCEDURE dbo.usp_UpdateCountryUser
(@UserID int , @CountryIds varchar(1000))
AS

BEGIN

DECLARE @intPos INT, @SubStr varchar(1000)

SET @CountryIDs=REPLACE(@CountryIDs,' ','')
SET @intPos=CHARINDEX(',',@CountryIDs)

WHILE @intPos >0
BEGIN

SET @SubStr=SubString(@CountryIDs,0,@intPos)

--delete, insert, update sql statement here

SET @CountryIDs=SUBSTRING(@CountryIDs, LEN(@SubStr) + 2, LEN(@CountryIDs) - LEN(@SubStr) + 1)

SET @IntPos = CHARINDEX(',', @CountryIDs)

END

END

No comments: