1 create procedure sp_split_string
2 (
3 @ string nvarchar (4000)
4)
5
6 begin
7 declare @ object_id nvarchar (500)
8 declare @ I int
9 declare @ Len int
10 print @ string
11 if (@ string is null) or (ltrim (@ string) = '')
12 Return
13
14 While charindex (',', @ string)> 0
15 begin
16 set @ Len = Len (@ string)
17 set @ I = charindex (',', @ string)
18 set @ object_id = left (@ string, @ i-1)
19 insert into a (ID) values (@ object_id) -- modify the SQL statement as needed.
20 set @ string = right (@ string, @ len-@ I)
21 End
22 set @ object_id = @ string
23 insert into a (ID) values (@ object_id) -- modify the SQL statement as needed.
24 end
25 go
26
27
28 -- Test
29 Create Table
30 (
31 ID int
32)
33 select * from
34 exec sp_split_string '123'
35 select * from a transmits an array string to the stored procedure (and processes the Stored Procedure string)
36