[英]Adding a collection of User-Defined Table Types in a stored procedure to SQL Server
I am wondering if anyone can help me or point in the correct direction as I trying to figure out is it possible to put collection of User-Defined Table types inside another I am trying to bulk insert a collection of parent and child tables into a SQL Server database. 我想知道是否有人可以帮助我或指出正确的方向,因为我试图找出是否可以将用户定义表类型的集合放到另一个表中?我正在尝试将父表和子表的集合批量插入到SQL中服务器数据库。 I can do this individually, but I am wondering can this be done with 100 orders in one go, rather than sending many inserts in a loop and having to split the list orders into individual orders then split the order and it order detail into two separate data tables.
我可以单独执行此操作,但我想知道是否可以一次性处理100个订单,而不是循环发送许多插入内容,而不必将列表订单拆分为单个订单,然后将订单及其订单详细信息拆分为两个单独的订单数据表。
I can not seem to figure out how to adjust the below, to send a collection of orders rather than one at a time. 我似乎无法弄清楚如何调整以下内容,而不是一次发送一组订单。 Without having to split them but also ensuring it will still insert the correct child rows to the correct parent record.
不必拆分它们,但还可以确保仍将正确的子行插入正确的父记录。 As one order can have unlimited amount of lines detail lines.
作为一个订单,可以有无限数量的行明细行。
User-Defined Table Types 用户定义的表类型
CREATE TYPE dbo.ordertype
AS TABLE
(
orderdate datetime,
ordernumber varchar(50) not null,
totalprice decimal(18,0) not null,
);
CREATE TYPE dbo.orderdetailtype
AS TABLE
(
productname varchar(50) not null,
price decimal(18,0) not null,
qty int not null
);
The stored procedure: 存储过程:
CREATE PROCEDURE [dbo].[sp_tes]
@ordertype ordertype ReadONLY,
@orderdetailtype orderdetailtype ReadONLY
AS
BEGIN
SET NOCOUNT ON
DECLARE @orderid INT
INSERT INTO dbo.orders (orderdate, ordernumber, totalprice )
SELECT orderdate, ordernumber, totalprice
FROM @ordertype;
SET @orderid = SCOPE_IDENTITY()
SELECT @orderid
INSERT INTO dbo.orderdetails( productname, price, qty )
SELECT productname, price, qty
FROM @orderdetailtype;
return @orderid
END
The call using c# 使用C#的调用
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_tes";
cmd.Parameters.Add("@ordertype", SqlDbType.Structured).Value = order;
cmd.Parameters.Add("@orderdetailtype", SqlDbType.Structured).Value = orderdetails;
SqlParameter returnParameter = cmd.Parameters.Add("@Returns", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
}
You need a connecting field for both table-types. 两种表类型都需要一个连接字段。 Maybe the order number?
也许订单号? Use the OUTPUT clause to return the new ids in a table variable and join it to the second parameter table.
使用OUTPUT子句在表变量中返回新的ID,并将其连接到第二个参数表。
CREATE TYPE dbo.ordertype
AS TABLE
(
orderdate datetime,
ordernumber varchar(50) not null,
totalprice decimal(18, 2) not null
)
CREATE TYPE dbo.orderdetailtype
AS TABLE
(
orderNumber varchar(50) not null,
productname varchar(50) not null,
price decimal(18, 2) not null,
qty int not null
)
CREATE TABLE dbo.orders
(
orderid INT IDENTITY(1, 1) not null,
orderdate datetime,
ordernumber varchar(50) not null,
totalprice decimal(18, 2) not null
)
CREATE TABLE dbo.orderdetails
(
orderid INT not null,
productname varchar(50) not null,
price decimal(18, 2) not null,
qty int not null
)
GO
CREATE PROCEDURE [dbo].[sp_tes]
@ordertype dbo.ordertype READONLY,
@orderdetailtype dbo.orderdetailtype READONLY
AS
BEGIN
SET NOCOUNT ON
DECLARE
@newIds AS TABLE (
ordernumber VARCHAR(50),
orderId INT
)
INSERT
INTO dbo.orders (orderdate, ordernumber, totalprice)
OUTPUT inserted.ordernumber, inserted.orderid INTO @newIds
SELECT orderdate, ordernumber, totalprice
FROM @ordertype
INSERT
INTO dbo.orderdetails(orderId, productname, price, qty)
SELECT T1.orderId, T2.productname, T2.price, T2.qty
FROM @newIds T1
INNER JOIN @orderdetailtype T2
ON T1.orderNumber = T2.orderNumber
SELECT *
FROM @newIds
END
GO
DECLARE
@ordertype dbo.ordertype,
@orderdetailtype dbo.orderdetailtype
INSERT
INTO @ordertype
SELECT GETDATE(), '2016/0001', 22.48
UNION
SELECT GETDATE(), '2016/0002', 19.98
INSERT
INTO @orderdetailtype
SELECT '2016/0001', 'Sql Guide', 9.99, 1
UNION
SELECT '2016/0001', 'Introducing Microsoft SQL Server 2016', 12.49, 1
UNION
SELECT '2016/0002', 'Sql Guide', 9.99, 2
EXEC [dbo].[sp_tes] @ordertype, @orderdetailtype
SELECT *
FROM dbo.orders
SELECT *
FROM dbo.orderdetails
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.