我有一个存储过程在表中导出一些数据。我想要确保的是即使表不存在,也会创建它。如果存在,我不希望该过程返回There is already an object named 'myFinalTable' in the database.
为了检查我是这样做的:
if not exists (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'dbo.myFinalTable')
begin
select *
into dbo.myFinalTable
from #someTempIhaveDataIn
end
然后我用
drop table dbo.myFinalTable
并运行一次。它工作,然后我再次运行它(程序),它给了我上面提到的错误(已经有一个名为..的对象)
答案 0 :(得分:1)
您的查询不正确。使用此:
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'myFinalTable'
AND TABLE_SCHEMA = N'dbo'
答案 1 :(得分:1)
试试这个
IF NOT EXISTS(SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))
BEGIN
select * into dbo.myFinalTable from #someTempIhaveDataIn
END