本文介绍了无法使用 EXEC 插入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EXEC('INSERT INTO T_MyTable('+ @Columns +')
        EXEC ('+ @UpdateString + ')'
        )

@Columns 包含逗号分隔的列名,@UpdateString 包含

Where @Columns contains comma seperated column names and @UpdateString contains

'Update T_OtherTable
Set col1 = 123,
col2 =  456,
col3 = 'nice'
OUTPUT
DELETED.col1 as Old_FirstCol
INSERTED.col1 as New_FirstCol
DELETED.col2 as Old_SecondCol
INSERTED.col2 as New_SecondCol
DELETED.col3 as Old_ThridCol
INSERTED.col3 as New_ThirdCol
Where ID = 1'

我收到不正确的语法错误.我们不能在 EXEC 内部使用 EXEC 吗?

I am getting incorrect syntax error. Can't we use EXEC inside EXEC?

推荐答案

OUTPUT 中列名后缺少逗号,

you are missing comma after column names in OUTPUT,

'Update T_OtherTable
Set col1 = 123,
col2 =  456,
col3 = ''nice''
OUTPUT
DELETED.col1 as Old_FirstCol,
INSERTED.col1 as New_FirstCol,
DELETED.col2 as Old_SecondCol,
INSERTED.col2 as New_SecondCol,
DELETED.col3 as Old_ThridCol,
INSERTED.col3 as New_ThirdCol
Where ID = 1'

也最好直接从 OUTPUT 子句向表中插入数据,然后使用嵌套的 EXEC.应该是这样的,

Also its better to directly insert data to the table from OUTPUT clause then using nested EXEC. It should be like,

'Update T_OtherTable
Set col1 = 123,
col2 =  456,
col3 = ''nice''
OUTPUT
DELETED.col1 as Old_FirstCol,
INSERTED.col1 as New_FirstCol,
DELETED.col2 as Old_SecondCol,
INSERTED.col2 as New_SecondCol,
DELETED.col3 as Old_ThridCol,
INSERTED.col3 as New_ThirdCol
INTO T_MyTable('+ @Columns +')
Where ID = 1'

这篇关于无法使用 EXEC 插入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 04:11