Sql server SQL server从SQL表执行SP并更新

Sql server SQL server从SQL表执行SP并更新,sql-server,loops,stored-procedures,sql-update,Sql Server,Loops,Stored Procedures,Sql Update,下表存储sql insert语句,我从sp运行这些语句。我还需要在最后一列中添加一个insert。我通过现有的stackoverflow问题将代码组合在一起。我需要帮助在我的代码中实现这一点,任何反馈都会有帮助 如何更新代码以更新最后一列 表: audit_sql_id audit_sql last_run_dt 1 select * from <<need to add last run_dt value>> 2

下表存储sql insert语句,我从
sp
运行这些语句。我还需要在最后一列中添加一个insert。我通过现有的stackoverflow问题将代码组合在一起。我需要帮助在我的代码中实现这一点,任何反馈都会有帮助

如何更新代码以更新最后一列

表:

audit_sql_id  audit_sql         last_run_dt
   1          select * from  <<need to add last run_dt value>>
   2          select * from  <<need to add last run_dt value>>
audit\u sql\u id audit\u sql last\u run\u dt
1从中选择*
2从中选择*
代码:

将ANSI_空值设置为ON
去
在上设置带引号的\u标识符
去
alter proc[dbo].[sp_sqlAudit]
@packagename作为varchar(255)
作为
开始
不计数;
如果@packagename='SQL\u DM\u AUDIT'
开始
declare@querys表(audit_sql_id int identity(1,1),sqlscript varchar(max))
声明@str_query varchar(max);
声明@startoop int
声明@endloop int
插入到@querys中
选择audit\u sql
从dw.dbo.audit_sql开始,使用(nolock)
选择@endloop=max(审计sql\u id),@startoop=min(审计sql\u id)
来自@querys
而@startoop<=@endloop
开始
选择@str_query=sqlscript
来自@querys
其中audit\u sql\u id=@startoop
exec(@str_query)
设置@startoop=@startoop+1
结束
结束
结束

我建议进行如下的轻微重构。不需要将整个sql语句列表放入TemDB中,只需对其进行迭代并依次获取每条语句。如果执行的话,我也会在sql中添加@debug参数

create or alter procedure dbo.sqlAudit
@packagename as varchar(255)
as
set nocount on;
declare @str_query varchar(max), @Id int
declare @AuditID table (Id int)

if @packagename='SQL_DM_AUDIT' 
begin
    insert into @AuditID (Id) /* Get list of IDs */
    select audit_sql_id
    from dw.dbo.audit_sql

    while exists(select * from @AuditID) /* Continue while there are IDs in the list */
    begin
        select top (1) @Id=Id from @AuditID /* Get an ID */

        select @str_query=audit_sql /* Get the sql for the ID */
        from dw.dbo.audit_sql
        where audit_sql_id=@Id

        delete from @AuditID where Id=@Id /* Remove this ID from the list */

        begin try
            exec (@str_query)
            if @@Error=0
            begin
                update dw.dbo.audit_sql set last_run_dt=GetDate() /* Update date for ID if run successful */
                where audit_sql_id=@Id
            end
        end try
        begin catch
            /*handle error*/
        end catch

    end
end

go

这感觉像是一场灾难。将SQL语句存储在表中然后运行几乎总是表明存在设计缺陷。此外,
sp
不应用于用户对象
sp_u
由Microsoft保留用于特殊程序;使用前缀既有可能导致程序在将来无法运行,也会带来性能成本。这看起来有点麻烦。当你说插入到
last\u run\u dt
列时,你的意思大概是
update
?假设该列是
audit\u sql
表的一部分,您应该做的是迭代该表的主键列表,获取
audit\u sql
1行(不需要nolock),执行它,处理错误,然后
update
其成功日期。
create or alter procedure dbo.sqlAudit
@packagename as varchar(255)
as
set nocount on;
declare @str_query varchar(max), @Id int
declare @AuditID table (Id int)

if @packagename='SQL_DM_AUDIT' 
begin
    insert into @AuditID (Id) /* Get list of IDs */
    select audit_sql_id
    from dw.dbo.audit_sql

    while exists(select * from @AuditID) /* Continue while there are IDs in the list */
    begin
        select top (1) @Id=Id from @AuditID /* Get an ID */

        select @str_query=audit_sql /* Get the sql for the ID */
        from dw.dbo.audit_sql
        where audit_sql_id=@Id

        delete from @AuditID where Id=@Id /* Remove this ID from the list */

        begin try
            exec (@str_query)
            if @@Error=0
            begin
                update dw.dbo.audit_sql set last_run_dt=GetDate() /* Update date for ID if run successful */
                where audit_sql_id=@Id
            end
        end try
        begin catch
            /*handle error*/
        end catch

    end
end

go