Thursday, June 19, 2008

Split Text In Stored Procedure and Insert in to the Table

User Selects the Select All check box and clicks Submit. All the IDs of selected check boxes are concatenated and send to the Stored Procedures.Stored Procedure accepts Two Patameters UserName and IdStringId String is in the format ~71~5~88~1043~9.
The special character '~' is used to concatenate the Ids.
Stored procedure splits the IdString and get each seperate id and update the database with the UserNameThe Table(CH_APPLIED_INFO) will be updated as

UserName JobID
Sabin 71
Sabin 5
Sabin 88
Sabin 1043
Sabin 9

This SP is communicative / Userfriendly. It accepts two parameters, UserName, which is a string and an IdString which is a '~' Concatenated String of JOB IDs the user have been selectd through the Check boxes in the Job listing Page.//Format of the IdString is ~71~5~88~1043~9

CREATE PROCEDURE dbo.SplitAndInsert
(
@userName varchar(100),
@idString varchar(100)
)
AS
SET NOCOUNT ON
DECLARE @splitstring varchar(100)
DECLARE @substring varchar(50)
Set @splitstring = @idString
WHILE (CHARINDEX('~',@splitstring ,1)<>0)
BEGIN
SET @substring = substring(@splitstring ,1,CHARINDEX('~',@splitstring ,1)-1)-- Find Substring up to Separator
PRINT @substringif @substring <> 0
INSERT INTO CH_APPLIED_INFO(USER_NAME, JOB_ID)
VALUES (@userName,@substring)SET @splitstring = substring(@splitstring ,Len(@substring)+2,Len(@splitstring ))
-- SET The Original String after the Split END
SET NOCOUNT OFF
RETURN

No comments: