我是sql server中的begginer
我想将数据集合插入存储过程中的列
我希望传递来自c#的数据,例如此(100,'3,5,4,2,9')
,并像这样插入
ID Number
100 3
100 5
100 4
100 2
100 9
我的程序代码
ALTER PROCEDURE [dbo].[Number]
@Number CHAR(10),
AS
BEGIN
BEGIN TRY
INSERT INTO NumbersTbl
VALUES(@Number)
SELECT 1
END TRY
BEGIN CATCH
EXEC SQLError
SELECT -1
END CATCH
END
我怎么做?
谢谢
答案 0 :(得分:0)
index banner
index banner
答案 1 :(得分:0)
首先,您可以创建特殊函数来解析输入数字:
CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
RETURNS
@returnList TABLE ([Numbers] [nvarchar] (500))
AS
BEGIN
DECLARE @name NVARCHAR(255)
DECLARE @pos INT
WHILE CHARINDEX(',', @stringToSplit) > 0
BEGIN
SELECT @pos = CHARINDEX(',', @stringToSplit)
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)
INSERT INTO @returnList
SELECT @name
SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
END
INSERT INTO @returnList
SELECT @stringToSplit
RETURN
END
然后编辑您的程序:
ALTER PROCEDURE [dbo].[Number]
@ID INT,
@Number VARCHAR(MAX)
AS
BEGIN
BEGIN TRY
INSERT INTO NumbersTbl(ID, Number)
SELECT @ID, Numbers
FROM dbo.splitstring(@Number)
END TRY
BEGIN CATCH
EXEC SQLError
SELECT -1
END CATCH
END
致电:EXEC Number 100,'3,5,4,2,9'