方法一:利用字符串截取拆分数组字符串.
脚本:
-->-- =============================================
-- Description: <存储过程数组参数>
-- 方法一:利用字符串截取拆分数组字符串
-- =============================================
CREATE PROCEDURE SP_Parameterarray
@Username VARCHAR(10),
@StrArray VARCHAR(800)
AS
DECLARE @P INT
DECLARE @C INT
DECLARE @Nums INT
SET @P=1
SET @C=1
BEGIN TRANSACTION
Set NOCOUNT ON
CREATE table #t_info
(
Username VARCHAR(10),
Nums INT
)
SET @C=CHARINDEX(',',@StrArray,@P+1)
SET @Nums=CAST(SUBSTRING(@StrArray,@P,@C-@P) AS INT)
INSERT INTO #t_info(Username,Nums) VALUES(@Username,@Nums)
SET @P=@C
WHILE (@P+1< LEN(@StrArray))
BEGIN
SET @C=CHARINDEX(',@P+1)
IF(@C> 0)
BEGIN
SET @Nums=CAST (SUBSTRING(@StrArray,@P+1,@C-@P-1) AS INT)
INSERT INTO #t_info(Username,@Nums)
SET @P=@C
END
ELSE
BREAK
END
SET @Nums=CAST(SUBSTRING(@StrArray,LEN(@StrArray)-@P) AS INT)
INSERT INTO #t_info(Username,@Nums)
SELECT * FROM #t_info
DROP table #t_info
SET NOCOUNT OFF
IF @@ERROR=0
BEGIN
COMMIT TRANSACTION
END
ELSE
BEGIN
RolLBACK TRANSACTION
END
GO
方法二:利用OpenXML 效率更高
脚本:
-->-- =============================================
-- Description: <存储过程数组参数>
-- 方法二:利用OpenXML 效率更高
-- @XML_Array='<Array><PA Nums="1"></PA><PA Nums="2"></PA><PA Nums="3"></PA><PA Nums="4"></PA></Array>'
-- =============================================
CREATE PROCEDURE [dbo].[SP_Parameterarray2]
@Username VARCHAR(10),
@XML_Array nvarchar(500)
AS
BEGIN
IF (@XML_Array IS NulL OR LEN(LTRIM(RTRIM(@XML_Array))) = 0)
RETURN
CREATE table #t_info(Username VARCHAR(10),Nums INT)
DECLARE @IDHandel INT
EXEC sp_xml_preparedocument @IDHandel OUTPUT,@XML_Array
INSERT INTO #t_info(Username,Nums)
SELECT @Username,N.Nums
FROM OPENXML(@IDHandel,'/Array/PA') WITH(Nums INT) AS N
WHERE N.[Nums] IS NOT NulL
EXEC sp_xml_removedocument @IDHandel
SELECT * FROM #t_info
DROP table #t_info
END
总结以上是内存溢出为你收集整理的SQLSERVER 存储过程传递数组参数全部内容,希望文章能够帮你解决SQLSERVER 存储过程传递数组参数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)