[英]How can I send array to stored procedure? What is the best data format if I send it from JS?
如何將數組發送到存儲過程? 我想將其從JS發送到存儲過程。 最佳的數據格式是什么?
使用MS SQL,您應該能夠以逗號分隔的形式使用它。
過去,我曾使用XML將數據數組傳遞到SQL Server,並且它與XML數據類型和XQuery配合得很好。
SQL 2008支持表值參數-因此您可以將數據表作為參數傳遞給sproc。 之前提到的常見方法已經提到過-通過CSV或XML參數。
這是一個使用CSV輸入參數存儲的proc的簡單示例。
create proc dbo.ListCustomers
(
@CustomerIDs varchar(MAX)
)
as
begin
-- convert paramter to xml
declare @XmlStr varchar(MAX)
set @XmlStr = '<R><I ID="' + replace(@CustomerIDs, ',', '"></I><I ID="') + '"></I></R>'
-- table variable that holds the ID's
declare @IdTable table (ID int)
-- load the XML document and insert id's to @IdTable
declare @XmlDoc int
exec sp_xml_preparedocument @XmlDoc out, @XmlStr
insert into @IdTable select ID from openxml(@XmlDoc, '/R/I',1) with (ID int)
exec sp_xml_removedocument @XmlDoc
-- use @IdTable in your query
select c.*
from tblCustomer c
join @IdTable as I on c.CustomerID = I.ID
end
go
-- usage:
-- exec ListCustomers '3823,3838,3845,3925,4051'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.