【发布时间】:2014-05-03 16:04:09
【问题描述】:
我有两个存储过程:SP1 和 SP2。它们都包含临时表#Temp,但两个存储过程的表结构不同。
还有第三个存储过程,SP3 从 SP1 和 SP2 调用,它更新 #Temp 中的列。
SP3 是这样的:
if @CallingSP = SP1
begin update #Temp
set ColumnA= 'abc'
end
if @CallingSP = SP2
begin update #Temp
set ColumnB= 'xyz'
end
现在,列“ColumnA”仅存在于 SP1 中,而“ColumnB”仅存在于 SP2 中。因此,当我从 SP1 执行 SP3 时,我收到“ColumnB”的无效列错误。
我目前正在做的是创建另一组存储过程并在 SP3 中执行它们,如下所示:
if @CallingSP = SP1
begin exec SP4
end
if @CallingSP = SP2
begin exec SP5
end
还有其他不需要我创建 SP4 和 SP5 的解决方法吗?
【问题讨论】:
-
不要使用临时表。使用表变量。 . .
declare temp table (. . . ). -
使用 sp_executesql 并构建查询字符串来执行
标签: sql sql-server