下面的程序将运行在SQL Server 2005中确定,但在2008年... Invalid object name 'vTemp'
SQL Server 2008中不能使用动态SQL创建的视图手续
Create PROCEDURE [dbo].[bulksaverecord]
as
begin
if exists (select * from sysobjects where id=object_id('vTemp'))
begin
drop view vTemp
end
declare @sql nvarchar(max)
set @sql='CREATE VIEW vTemp AS SELECT top 0 FromType,TypeID,AssessUser,
TypeTimeType,AssessTime,ItemRoleKey,UserName,SourceKey,
PartID,PartName,PositionName,ItemID,ItemName,ItemType,ItemScript,
ItemDataSourceName,ItemField,ItemKPI,ItemFun,ItemFunLimit,ItemKill,
AllKill,ItemMark,ItemPower,MarkScript,PowerScript,FromKey
FROM AssessMark'
exec sp_executesql @sql
if exists (select * from sysobjects where id=object_id('vTemp'))
begin
exec ('SELECT * FROM vTemp')
SELECT * FROM vTemp -- throws the error here
end
end
未能看来,视图中创建在PROCEDURE中使用动态SQL,在DYNAMIC SQL中将有一个范围,即使VIEW实际上已创建。
如果你想要一个临时表,为什么你不使用一个?视图并不意味着是模拟对象。 – HLGEM 2013-03-25 19:31:29