create table testtable(a int, b char, c char, d char, e char)
go
declare @a varchar(20)= '1,2,3,4,5,6,7,8,9'
declare @b varchar(20)='a'
declare @c varchar(20)='f'
declare @d varchar(20)='g'
declare @e varchar(20)='h'
INSERT testtable(a,b,c,d,e)
SELECT t.c.value('.', 'VARCHAR(2000)'), @b, @c, @d, @e
FROM (
SELECT x = CAST('<t>' +
REPLACE(@a, ',', '</t><t>') + '</t>' AS XML)
) a
CROSS APPLY x.nodes('/t') t(c)
select * from testtable
using loops in stored procedures in sql server
-
18-07-2023 - |
Question
i have a stored procedure with 5 parameters.
@1,@2,@3,@4,@5
Five Columns
@a,@b,@c,@d,@e
One of them is of string types(@a) which contains "N" number of numeric values separated by coma(,).
ex:
@a= '1,2,3,4,5,6'
@b='a'
@c='f'
@d='g'
@e='h'
My requirement is to insert "N" number of rows .
The value of rest four parameter's will remain same for every row whereas the value of @a
parameter will be fetched by splitting @a
.
The table will look like the attached file
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow