SQL 服务器从 sql table 执行 SP 并更新
SQL server execute SP from sql table and update
下面的 table 存储 sql 插入语句,而我 运行 来自 sp
的插入语句。我还需要在 last_run_dt 列中添加一个插入。我通过现有的 Whosebug 问题将代码放在一起。我需要帮助在我的代码中实现它,任何反馈都会有所帮助。
如何更新我的代码以更新 last_run_dt 列?
Table:
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>>
代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc [dbo].[sp_sqlAudit]
@packagename as varchar(255)
as
begin
set nocount on;
if @packagename='SQL_DM_AUDIT'
begin
declare @queries table (audit_sql_id int identity(1,1),sqlscript varchar(max))
declare @str_query varchar(max);
declare @startloop int
declare @endloop int
insert into @queries
select audit_sql
from dw.dbo.audit_sql with(nolock)
select @endloop = max(audit_sql_id), @startloop = min(audit_sql_id)
from @queries
while @startloop < = @endloop
begin
select @str_query = sqlscript
from @queries
where audit_sql_id = @startloop
exec (@str_query)
set @startloop = @startloop + 1
end
end
end
我建议进行如下所示的轻微重构。无需将整个 sql 语句列表放入 TemDB,只需对其进行迭代并依次获取每个语句即可。如果执行
,我也会始终将 @debug 参数添加到 print
和 sql
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
下面的 table 存储 sql 插入语句,而我 运行 来自 sp
的插入语句。我还需要在 last_run_dt 列中添加一个插入。我通过现有的 Whosebug 问题将代码放在一起。我需要帮助在我的代码中实现它,任何反馈都会有所帮助。
如何更新我的代码以更新 last_run_dt 列?
Table:
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>>
代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc [dbo].[sp_sqlAudit]
@packagename as varchar(255)
as
begin
set nocount on;
if @packagename='SQL_DM_AUDIT'
begin
declare @queries table (audit_sql_id int identity(1,1),sqlscript varchar(max))
declare @str_query varchar(max);
declare @startloop int
declare @endloop int
insert into @queries
select audit_sql
from dw.dbo.audit_sql with(nolock)
select @endloop = max(audit_sql_id), @startloop = min(audit_sql_id)
from @queries
while @startloop < = @endloop
begin
select @str_query = sqlscript
from @queries
where audit_sql_id = @startloop
exec (@str_query)
set @startloop = @startloop + 1
end
end
end
我建议进行如下所示的轻微重构。无需将整个 sql 语句列表放入 TemDB,只需对其进行迭代并依次获取每个语句即可。如果执行
,我也会始终将 @debug 参数添加到print
和 sql
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