Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my stored procedure

SQL
create procedure sptest(@roleid int, @privileges varchar(50))
 as
 begin
 insert into users(Roleid, Roles) values(roleid=@roleid , privileges=@privileges)
 end


@privileges contains R,W,D

We need to store it in Database like below
SQL
Roleid    Prv
1         R
1         W
1         D


How can we do that...?
Can i use Loop or something else..
Posted
Updated 26-May-14 23:01pm
v2
Comments
CHill60 27-May-14 4:58am    
Well for starters that procedure will never work as the syntax is incorrect. You are also only passing the privileges so is it safe to assume that Roleid is an autoincrement column?
Venkat Kumar chigulla 27-May-14 5:01am    
Roleid is not autoincrement... I changed the code now. Sorry

Breaking it isn't that simple, but Using comma separated value parameter strings in SQL IN clauses[^] does what you want with a temporary table.
 
Share this answer
 
Yes you could split the list in SQL and then work on a loop inserting each item.

There is a similar thing being done here: Parse delimited string in a Stored procedure[^]
 
Share this answer
 
v2
There is no built in function in SQL to split the comma separaed values. You have to create user defined function which split the comma separated data..

Please refer following link containing the details of User Defined function used to split the data..

Split function in SQL[^]

Once you created the user defined function to split data in SQL, you can insert values into your table as following

SQL
CREATE PROCEDURE procTest(@roleID INT, @privileges VARCHAR(50))
 AS
 BEGIN
  INSERT INTO tblRoles(RoleID, Roles)
  SELECT @roleID, value FROM dbo.[NAME-OF-USER-DEFINED-SPLIT-FUNCTION](@privileges,',')
 END
 
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