Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I am having Values like

C#
string a,b;
   a= "1,2,3,4";
   b="admin";



I am passing a and b to SP and I want to save in DB like

HTML
a   b
1  admin
2  admin
3  admin
4  admin



how can we do it?
Posted
Comments
Prasad_Kulkarni 15-Mar-12 3:18am    
Not clear enough, please elaborate

1 solution

SQL doesn't have a concept of arrays, so this gets a bit fiddly.
Pass a list as a string parameter:
SQL
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '2,3,177,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0 
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO myTable (a, b) VALUES (@VALUE, 'admin')
END
 
Share this answer
 
Comments
palraj001 15-Mar-12 5:48am    
Hi Thanks for the reply. by executing ur code i dint get 177 values.
OriginalGriff 15-Mar-12 6:01am    
Pardon? Would you like to try explaining that in a little more detail?
Or, and BTW: drop the txtspeak - it is not popular on this site... :laugh:
palraj001 15-Mar-12 6:06am    
ok fine.

when executing your code inserted values in the table "my table" is
2 admin
3 admin.

---


177 admin

above record is not inserted.
OriginalGriff 15-Mar-12 6:22am    
Did you remember the terminating ','?
palraj001 15-Mar-12 6:30am    
yes. I forgot that. thanks . it helped me.

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