如何使用表值作为参数运行存储过程

时间:2015-05-27 05:37:26

标签: sql-server-2008 stored-procedures

我有一个存储过程,用于将数据插入表中,它有6个参数

SP:

    var parser: NSXMLParser?
    let path = NSBundle.mainBundle().pathForResource("MyFile", ofType: "xml")
    if path != nil {
        parser = NSXMLParser(contentsOfURL: NSURL(fileURLWithPath: path!))
    } else {
        NSLog("Failed to find MyFile.xml")
    }

    if parser != nil {
        // Do stuff with the parser here.
    }

现在我想运行这个SP并使用我现有表中的值填充其参数,并使用相同的列数和名称

表:

CREATE PROCEDURE [dbo].[sp_tb_mis_am_node]
@pid_code varchar(10),
@id_code varchar(10),
@description varchar(50),
@disp_order int, /*** This will serve as a display order sequence ***/
@username varchar(20),
@user_key int,

我将如何执行它?

我使用的是Microsoft SQL Server 2008 R2

2 个答案:

答案 0 :(得分:1)

创建过程ExecuteProc 如 开始     声明@RowNumber INT; - 表webloan_helper的行号     SET @RowNumber = 1;

While (@RowNumber <= (SELECT COUNT(*) FROM webloan_helper))
Begin
    Declare @pid_code varchar(10);
    Declare @id_code varchar(10);
    Declare @description varchar(50);
    Declare @disp_order int;
    Declare @username varchar(20);
    Declare @user_key int;

    Set @pid_code = (Select pid_code From (Select Row_Number() Over (Order By pid_code) As RowNum, * From webloan_helper) t2 Where RowNum = @RowNumber);
    Set @id_code = (Select id_code From (Select Row_Number() Over (Order By pid_code) As RowNum, * From webloan_helper) t2 Where RowNum = @RowNumber);
    Set @description = (Select [description] From (Select Row_Number() Over (Order By pid_code) As RowNum, * From webloan_helper) t2 Where RowNum = @RowNumber);
    Set @disp_order = (Select disp_order From (Select Row_Number() Over (Order By pid_code) As RowNum, * From webloan_helper) t2 Where RowNum = @RowNumber);
    Set @username = (Select username From (Select Row_Number() Over (Order By pid_code) As RowNum, * From webloan_helper) t2 Where RowNum = @RowNumber);
    Set @user_key = (Select user_key From (Select Row_Number() Over (Order By pid_code) As RowNum, * From webloan_helper) t2 Where RowNum = @RowNumber);

    Exec sp_tb_mis_am_node @pid_code,@id_code,@description,@disp_order,@username,@user_key;
     -- Order By Should be unique column

SET @ RowNumber = @ RowNumber + 1;     端

结束

答案 1 :(得分:1)

如果重写您的存储过程 1,2 不在卡上,也没有将正文合并到您当前的方法 2 ,那么您唯一真正的选择剩下就是循环。

要循环播放,请使用cursorWHILE,例如:

DECLARE @pid_code varchar(10)
DECLARE @id_code varchar(10)
DECLARE @description varchar(50)
DECLARE @disp_order int
DECLARE @username varchar(20)
DECLARE @user_key int

DECLARE boris cursor local fast_forward FOR SELECT  [pid_code]
      ,[id_code]
      ,[description]
      ,[disp_order]
      ,[username]
      ,[user_key]
  FROM [webloan_helper].[dbo].[tbl_ledger_add]

OPEN boris
FETCH NEXT FROM boris INTO @pid_code,@id_code,@description,
                           @disp_order,@username,@user_key

WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC [sp_tb_mis_am_node] @pid_code,@id_code,@description,
                           @disp_order,@username,@user_key

    FETCH NEXT FROM boris INTO @pid_code,@id_code,@description,
                           @disp_order,@username,@user_key
END
CLOSE boris
DEALLOCATE boris

1 我会recommend, however对当前程序进行一点重写:

  

在命名过程时避免使用 sp _ 前缀。 SQL Server使用此前缀来指定系统过程。如果存在具有相同名称的系统过程,则使用前缀可能导致应用程序代码中断。

2 在任何一种情况下,您都打算将它们重新编写为基于集合,而不是使用单行。对于重写存储过程实例,这意味着需要table-valued parameter