CREATE PROC USP_STRING(@STR VARCHAR(500))
AS
BEGIN
SET @STR=@STR+','
DECLARE @SP INT=1
DECLARE @PP INT=1
SELECT @SP= CHARINDEX( ' , ' ,@STR,@PP )
CREATE TABLE #TEMP (ID INT IDENTITY,NAME VARCHAR(500))
WHILE (@SP>0)
BEGIN
INSERT INTO #TEMP
SELECT SUBSTRING (@STR,@PP,@SP-@PP)
SET @PP=@SP+1
SELECT @SP = CHARINDEX ( ' , ' ,@STR,@PP )
END
SELECT * FROM #TEMP
END
EXEC USP_STRING 'MANJU,RAMU,ANJI,HARI,BABU,DEVI'
OUTPUT
NAME ID
MANJU 1
RAMU 2
ANJI 3
HARI 4
BABU 5
DEVI 6
AS
BEGIN
SET @STR=@STR+','
DECLARE @SP INT=1
DECLARE @PP INT=1
SELECT @SP= CHARINDEX( ' , ' ,@STR,@PP )
CREATE TABLE #TEMP (ID INT IDENTITY,NAME VARCHAR(500))
WHILE (@SP>0)
BEGIN
INSERT INTO #TEMP
SELECT SUBSTRING (@STR,@PP,@SP-@PP)
SET @PP=@SP+1
SELECT @SP = CHARINDEX ( ' , ' ,@STR,@PP )
END
SELECT * FROM #TEMP
END
EXEC USP_STRING 'MANJU,RAMU,ANJI,HARI,BABU,DEVI'
OUTPUT
NAME ID
MANJU 1
RAMU 2
ANJI 3
HARI 4
BABU 5
DEVI 6
No comments:
Post a Comment