Passing c# DataTable as a parameter to stored procedure in MS SQL Server 2008(将 c# DataTable 作为参数传递给 MS SQL Server 2008 中的存储过程)
问题描述
我想将 DataDable 作为参数传递给 存储过程,将以下列合并:
I want to pass a DataDable to a stored procedure as parameter cointaning the columns below:
Supp_Id int
Del_Methode_Id int
Ord_Ammount int
Promo_Id int
Discount_Ammount Money
现在我想在存储过程中使用这个数据表并想在它上面声明一个游标.并使用该游标将值按顺序插入数据库表中.
Now I want to use this datatable in stored procedure and want to declare a cursor on it. And use that cursor to insert values into the database table sequentially.
请告诉我如何在存储过程中声明数据表参数,然后在该参数上使用游标?
Please tell me how to declare datatable parameter in stored procedure and then using cursor on that parameter ?
推荐答案
首先需要创建一个类型:
First you need to create a type:
CREATE TYPE dbo.whatever AS TABLE
(
Supp_Id int,
Del_Methode_Id int,
Ord_Amount int,
Promo_Id int,
Discount_Amount Money
);
现在您的存储过程可以将其声明为只读输入参数:
Now your stored procedure can declare this as a read only input parameter:
CREATE PROCEDURE dbo.do_whatever
@datatable dbo.whatever READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.destination_table(column_list)
SELECT column_list FROM @datatable;
END
GO
为什么要在此处使用游标,或者认为您需要一个游标,我不确定.您可以在 INSERT...SELECT
中添加一个 ORDER BY
子句,如果您认为这会很有用(并且有一些有意义的排序依据),但否则,如果您在这里真的很想要一个游标,您应该能够像对任何其他表一样针对 @datatable
声明一个游标.
Why you want to use a cursor here, or think you need one, I'm not sure. You can add an ORDER BY
clause to the INSERT...SELECT
if you think that will be useful (and there is something meaningful to order by), but otherwise if you really really want a cursor here you should be able to declare one against @datatable
just as you would for any other table.
这篇关于将 c# DataTable 作为参数传递给 MS SQL Server 2008 中的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 c# DataTable 作为参数传递给 MS SQL Server 2008 中的存储过程


- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01