Sql Server 存储过程中的 变量作用域区间
Declare @PropItemIDs NVarchar(4000)
Set @PropItemIDs = '1,2,3'
Declare @SQL NVarchar(4000)
Set @SQL = 'Insert Into #Table_NeedBuyPropItem
select PropID ,Attack,Recovery,LightGest,Hedge,HitTarget,Damage,Endure,Renew,ProductTime
From TXBT_RT_Prop With(NoLock)Where PropID in ( ' + @PropItemIDs + ')'
Exec (@SQL)
执行上面这段语句后,再 Select * From #Table_NeedBuyPropItem.你会发现找不到这个临时表.
但是这样写:
select Row_Number() Over(Order By PropID ASC) As RowNum ,PropID ,Attack,Recovery,LightGest,Hedge,HitTarget,Damage,Endure,Renew,ProductTime
Into #Table_NeedBuyPropItem
From TXBT_RT_Prop With(NoLock)
Where 1 = 2
Declare @SQL NVarchar(4000)
Set @SQL = '
Insert Into #Table_NeedBuyPropItem
select PropID ,Attack,Recovery,LightGest,Hedge,HitTarget,Damage,Endure,Renew,ProductTime
From TXBT_RT_Prop With(NoLock)
Where PropID in ( ' + @PropItemIDs + ')'
Exec (@SQL)
再执行 Select * From #Table_NeedBuyPropItem.你会发现OK了.
具体原理就是....用一段语言代码来表示:
刚开始的做法是:
if (2==2)
{
int i;
i=1
}
print i;
改变后的做法是:
int i;
if (2==2)
{
i=1;
}
print i;