【发布时间】:2012-12-12 21:44:19
【问题描述】:
我有 2 个带有动态 sql 的存储过程(ms sql 2005)。首先他们创建临时表(##Table)并填充数据。第二个过程从第一个过程创建的临时表中选择数据。
第一个sp:
set @cmd = 'create table ' + @tableName +
'(ORDERS_DT_ID int' +
...
',WAREHOUSE_RESULT numeric(15, 3)) ' +
' insert into ' + @tableName + ' ( ORDERS_DT_ID, ... WAREHOUSE_RESULT )' +
' select ro.ORDERS_DT_ID, ... ro.WAREHOUSE_RESULT ' +
' from WH_REMAINS_BY_ORDER_FN ( ' + cast(@orderId as varchar(10)) + ' ) as ro '
exec (@cmd)
第二个sp(例如):
set @cmd = 'select r.ORDERS_DT_ID, ... r.WAREHOUSE_RESULT' +
' from ' + @tableName + ' as r'
exec (@cmd)
其中@tableName - sp的输入参数(例如='##Table')
我使用来自 asp.net mvc 应用程序的程序。我调用第一个过程,从 MSSMS 检查临时表(表存在)然后应用程序在断点处停止(在第二个 sp 之前),接下来我调用第二个过程并且表不存在(但在第一个和第二个过程之间调用表存在)。
var result = dataContext.firstSP(table, orderId).FirstOrDefault(); // create temp table and fill data (+ return status)
var data = dataContext.secondSP( table, orderId ).AsQueryable(); // before execute we have temp table (check from MSSMS) and we have error after execute that code.
如果我们在第二个过程中检查临时表,我们可以看到该表不存在:
if object_id ( N'tempdb.dbo.' + ltrim(@tableName) ) is null
begin
exec dbo.firstSP @tableName, @orderId
end
为什么当我们运行第二个 sp 时,第一个 sp 的临时表会丢失?
【问题讨论】:
标签: c# sql-server-2005