在 SQL 存储过程中使用用户定义的表 - 无效数据类型
using user defined tables in SQL Stored Procedure - invalid data type
我一直在努力让定义的数据 table 被接受为存储过程参数之一的变量。
第一次使用用户定义的 table,所以我可能犯了语法错误
这样我就可以从 c# 程序发送数据 table 而不是单独插入每一行。
存储过程
CREATE PROCEDURE [dbo].[Upload_AddBulkProducts]
@uploadedTable CSV_ADDProducts readonly
AS
INSERT INTO [dbo].[Products]
SELECT * FROM @uploadedTable
GO
错误 = 未定义的变量
The error i get when trying to create the stored procedure
用户定义Table
CREATE TYPE [dbo].[CSV_ADDProducts] AS TABLE(
[Product Item] [nvarchar](50) NULL,
[Product SKU] [bigint] NULL,
[Product Name] [nvarchar](max) NULL,
[Product Active] [nchar](10) NULL,
[Product Selling Price] [money] NULL,
[Product Description] [nvarchar](max) NULL,
[Product Purchase Description] [nvarchar](max) NULL,
[Product VAT Code ID] [bigint] NOT NULL,
[Product Last Update] [datetime] NULL
)
GO
在 T-SQL 的上下文中,您的代码是有效的。可以创建类型,也可以创建过程。
然后,在 C# 代码中使用以下内容:
DataTable dt = new DataTable();
dt.Columns.Add("Product Item", typeof (string));
//add other columns here
SqlParameter param = new SqlParameter("@uploadedTable", SqlDbType.Structured)
{
TypeName = "dbo.userdefinedtabletype",
Value = dt
};
sqlComm.Parameters.Add(param);
我一直在努力让定义的数据 table 被接受为存储过程参数之一的变量。
第一次使用用户定义的 table,所以我可能犯了语法错误
这样我就可以从 c# 程序发送数据 table 而不是单独插入每一行。
存储过程
CREATE PROCEDURE [dbo].[Upload_AddBulkProducts]
@uploadedTable CSV_ADDProducts readonly
AS
INSERT INTO [dbo].[Products]
SELECT * FROM @uploadedTable
GO
错误 = 未定义的变量 The error i get when trying to create the stored procedure
用户定义Table
CREATE TYPE [dbo].[CSV_ADDProducts] AS TABLE(
[Product Item] [nvarchar](50) NULL,
[Product SKU] [bigint] NULL,
[Product Name] [nvarchar](max) NULL,
[Product Active] [nchar](10) NULL,
[Product Selling Price] [money] NULL,
[Product Description] [nvarchar](max) NULL,
[Product Purchase Description] [nvarchar](max) NULL,
[Product VAT Code ID] [bigint] NOT NULL,
[Product Last Update] [datetime] NULL
)
GO
在 T-SQL 的上下文中,您的代码是有效的。可以创建类型,也可以创建过程。
然后,在 C# 代码中使用以下内容:
DataTable dt = new DataTable();
dt.Columns.Add("Product Item", typeof (string));
//add other columns here
SqlParameter param = new SqlParameter("@uploadedTable", SqlDbType.Structured)
{
TypeName = "dbo.userdefinedtabletype",
Value = dt
};
sqlComm.Parameters.Add(param);