您可以使用表值参数。因此,如果您有这样的类型:
CREATE TYPE PhoneNumberType AS TABLE
( number VARCHAR(50));
GO
然后你的程序将是这样的:
CREATE PROCEDURE dbo. usp_InsertPhoneNumbers
@TVP PhoneNumberType READONLY
AS
INSERT INTO Phone(number,....)
SELECT
number,
....
FROM
@TVP
参考这里
编辑
它就像一张桌子一样工作,所以很容易做到这一点:
DECLARE @TVP PhoneNumberType
INSERT INTO @TVP
VALUES
('0412589'),('0425896'),('04789652')
EXEC usp_InsertPhoneNumbers @TVP
或者,如果您将其作为一个字符串,则需要将其拆分。这是一个简洁的拆分功能:
CREATE FUNCTION dbo.Split (@s varchar(512),@sep char(1))
RETURNS table
AS
RETURN (
WITH Pieces(pn, start, stop) AS (
SELECT 1, 1, CHARINDEX(@sep, @s)
UNION ALL
SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT pn,
SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
FROM Pieces
)
GO
你这样调用程序:
DECLARE @TVP PhoneNumberType
DECLARE @textToSplit VARCHAR(512)='0412589, 0425896, 04789652'
INSERT INTO @TVP
SELECT
split.s
FROM
dbo.Split(@textToSplit,',') AS split
EXEC usp_InsertPhoneNumbers @TVP