參考下面sp_executesql單個輸出引數,@i變數為動態列名,列名字尾n或是c,為了比較更新前後值是否發生了變化,分別做了兩次select取值,程**冗餘和產生不容忽視效能的問題。:
set@sql
= n '
select @n = ['+
convert(
nvarchar(
max),
@i)
+'n] from #inserted
'execute sp_executesql
@sql,
n '
@n decimal(18,0) output
',
@nn output;
set@sql
= n '
select @o = ['+
convert(
nvarchar(
max),
@i)
+'n] from #deleted
'execute sp_executesql
@sql,
n '
@o decimal(18,0) output
',
@on output;
if (
isnull(
@nn,
0) <>
isnull(
@on,
0))
--execute [dbo].[usp_audit_insert] ...
set@sql
= n '
select @n = ['+
convert(
nvarchar(
max),
@i)
+'c] from #inserted
'execute sp_executesql
@sql,
n '
@n decimal(18,0) output
',
@nc output;
set@sql
= n '
select @o = ['+
convert(
nvarchar(
max),
@i)
+'c] from #deleted
'execute sp_executesql
@sql,
n '
@o decimal(18,0) output
',
@oc output;
if (
isnull(
@nc,
0) <>
isnull(
@oc,
0))
--execute [dbo].[usp_audit_insert] ...
sp_executesql可以實現返回多個輸出引數,改進**:
declare
@sql
nvarchar(
max),
@fname
nvarchar(
50)declare
@nndecimal(
18,0),
@ondecimal(
18,0),
@ncdecimal(
18,0),
@ocdecimal(
18,0)
set@sql
= n '
select @nn = ['+
convert(
nvarchar(
max),
@i)
+'n],@nc = ['+
convert(
nvarchar(
max),
@i)
+'c] from #inserted
'execute sp_executesql
@sql,
n '
@nn decimal(18,0) output,@nc decimal(18,0) output
',
@nn output,
@nc output;
set@sql
= n '
select @on = ['+
convert(
nvarchar(
max),
@i)
+'n],@oc = ['+
convert(
nvarchar(
max),
@i)
+'c] from #deleted
'execute sp_executesql
@sql,
n '
@on decimal(18,0) output,@oc decimal(18,0) output
',
@on output,
@oc output;
if (
isnull(
@nn,
0) <>
isnull(
@on,
0))--execute [dbo].[usp_audit_insert] ...
if (
isnull(
@nc,
0) <>
isnull(
@oc,
0))--execute [dbo].[usp_audit_insert] ...
更多相關:
sp executesql返回多個值
declare sql nvarchar 2000 declare cc int declare ss int select sql n select c select count from yg select s select sum b id from yg execute sp execute...
sp executesql返回多個輸出引數
參考下面sp executesql單個輸出引數,i變數為動態列名,列名字尾n或是c,為了比較更新前後值是否發生了變化,分別做了兩次select取值,程 冗餘和產生不容忽視效能的問題。view code set sql n select n convert nvarchar max i n from ...
sp executesql介紹和使用
sp executesql介紹和使用 execute相信大家都用的用熟了,簡寫為exec,除了用來執行儲存過程,一般都用來執行動態sql sp executesql,sql2005中引入的新的系統儲存過程,也是用來處理動態sql的,如 exec sp executesql sql,n count i...