Passing c# DataTable as a parameter to stored procedure in MS SQL Server 2008 [closed]

拥有回忆 提交于 2019-12-18 17:30:20

问题


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 ?


回答1:


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

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.



来源:https://stackoverflow.com/questions/18080022/passing-c-sharp-datatable-as-a-parameter-to-stored-procedure-in-ms-sql-server-20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!