将值传递到动态存储过程

时间:2011-06-28 11:24:13

标签: sql-server tsql

我正在尝试构建类似下面的sproc。但是我似乎无法让触发器知道我的范围变量:

CREATE PROCEDURE delete_me
    @Name VARCHAR(25),
    @Value DECIMAL(13,4),
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @sql NVARCHAR(MAX) = '
      CREATE TRIGGER temptrig ON TheTable FOR INSERT
      AS 
      DECLARE @Name VARCHAR(25)
      INSERT AnotherTable
      VALUES (@@IDENTITY, @Name)'

    EXEC sp_executeSQL @SQL

    INSERT INTO TheTable
    (
        Value
    )
    VALUES
    (
        @value,
    )

    DROP TRIGGER temptrig

    SELECT SCOPE_IDENTITY()
END 

如何让触发器了解@Name?

3 个答案:

答案 0 :(得分:3)

如果两个用户同时运行此过程,您是否考虑过此处会发生什么?为什么你需要为此创建一个临时触发器?为什么不直接插入到AnotherTable中,如果您要做的就是之后放下触发器?

DECLARE @sql NVARCHAR(MAX) = N'
      CREATE TRIGGER temptrig ON TheTable FOR INSERT
      AS 
          INSERT AnotherTable
          VALUES (@@IDENTITY, ''' + @Name + ''')';

修改

摆脱触发器,你可以这样做:

CREATE PROCEDURE dbo.delete_me
    @Name  VARCHAR(25),
    @Value DECIMAL(13,4),
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO TheTable
    (
        Value
    )
    VALUES
    (
        @value
    );

    INSERT INTO AnotherTable(columns) 
        SELECT SCOPE_IDENTITY(), @Name);
END

答案 1 :(得分:1)

如果你试图插入两个表并返回一个结果集,你可以在 ONE INSERT STATEMENT !! 中完成所有这些,见这里:

CREATE PROCEDURE delete_me
    @Name VARCHAR(25),
    @Value DECIMAL(13,4),
AS
BEGIN
    SET NOCOUNT ON;

    --insert row into TheTable
    INSERT INTO TheTable  
    (
        Value
    )
    --insert row into AnotherTable
    OUTPUT INSERTED.IdentityColumn, INSERTED.col2...
        INTO AnotherTable
    --return result set
    OUTPUT INSERTED.IdentityColumn
    VALUES
    (
        @value,
    )

END 
GO

工作示例代码脚本:

DECLARE @TheTable     table (RowID int identity ( 1,1) primary key, RowValue1 varchar(5), RowValue2 varchar(5))
DECLARE @AnotherTable table (RowID int, RowValue1 varchar(5), RowValue2 varchar(5))

--insert row into TheTable
INSERT INTO @TheTable  
(
    RowValue1,RowValue2
)
--insert row into AnotherTable
OUTPUT INSERTED.RowID, INSERTED.RowValue1, INSERTED.RowValue2
    INTO @AnotherTable
--return result set
OUTPUT 'From Insert' AS WOW, INSERTED.RowID
VALUES
(
    'aaa','bbb'
)


SELECT * FROM @TheTable
SELECT * FROM @AnotherTable

输出:

WOW         RowID
----------- -----------
From Insert 1

(1 row(s) affected)

RowID       RowValue1 RowValue2
----------- --------- ---------
1           aaa       bbb

(1 row(s) affected)

RowID       RowValue1 RowValue2
----------- --------- ---------
1           aaa       bbb

(1 row(s) affected)

编辑仅供参考,这也是有效的:

DECLARE @MyIdentity int --create a local variable to store the identity

INSERT INTO table1 (... ) values (... ) --generate the identity value

SET @MyIdentity=SCOPE_IDENTITY --capture the identity value

INSERT INTO table2 (... ) values (...@MyIdentity ) --use stored identity value

SELECT @MyIdentity --return a result set of the stored identity value

答案 2 :(得分:0)

这是你想要完成的吗?

   CREATE PROCEDURE delete_me
       @Name VARCHAR(25),
       @Value DECIMAL(13,4),
   AS
   BEGIN
        SET NOCOUNT ON;

       DECLARE @sql NVARCHAR(MAX) = '
         CREATE TRIGGER temptrig ON TheTable FOR INSERT
         AS 
         INSERT AnotherTable
         VALUES (@@IDENTITY, ''' + @Name + ''')'

       EXEC sp_executeSQL @SQL

       INSERT INTO TheTable
       (
            Value
       )
       VALUES
       (
           @value,
       )

       DROP TRIGGER temptrig

       SELECT SCOPE_IDENTITY()
   END