针对表中的所有记录运行存储过程

时间:2015-05-06 18:38:30

标签: sql-server-2008 loops stored-procedures cursor

我正在尝试创建一个SSIS包,我从Oracle源中提取数据,插入目标表并针对操作数据的存储过程运行该数据。

存储过程具有以下变量:

@ShiftID Varchar (50),
@ProcedureID int,
@Registered int,
@Performed  int,
@Deferred  int,
@Collected  int,
@QNS int,
@Deleted int,
@Intent int,
@APH int,
@ResultError int output,
@ResultMessage varchar(1024) output

我希望运行该程序的表包含以下列:

Shift_ID
ProcedureID
Registered
Performed
Deferred
Collected
QNS
Deleted
APH

现在我在表格中有大约500条记录,我想对所有记录运行此程序,但我不确定如何执行此任务。

如何将变量分配给表中的特定列,然后将其循环或光标放在整个表中?

谢谢,

1 个答案:

答案 0 :(得分:0)

您可以在Staging表上插入触发器之后使用它来插入ssis包中的行。

喜欢这个 -

CREATE TRIGGER [dbo].[TriggerName] ON [dbo].[yourTableName]
AFTER INSERT
AS
DECLARE @ShiftID VARCHAR(50)
    ,@ProcedureID INT
    ,@Registered INT
    ,@Performed INT
    ,@Deferred INT
    ,@Collected INT
    ,@QNS INT
    ,@Deleted INT
    ,@Intent INT
    ,@APH INT

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT @ShiftID = ins.ShiftID
        ,@ProcedureID = ins.ProcedureID
        ,@Registered = ins.Registered
        ,@Performed = ins.Performed
        ,@Deferred = ins.Deferred
        ,@Collected = ins.Collected
        ,@Pincode = ins.Pincode
        ,@QNS = ins.QNS
        ,@Deleted = ins.Deleted
        ,@Intent = ins.Intent
        ,@APH = ins.APH
    FROM inserted ins

    -- TODO: Set parameter values here.
    EXECUTE [your Proc Name] @ShiftID
        ,@ProcedureID
        ,@Registered
        ,@Performed
        ,@Deferred
        ,@Collected
        ,@QNS
        ,@Deleted
        ,@Intent
        ,@APH
        -- Insert statements for trigger here
END
GO