如果表存在,则期望删除并重新创建。实际不是丢弃表,而是再次将数据插入到表中。
BEGIN
PRINT N'Seeding [Proj].[UserTable]...';
SET NOCOUNT ON
--
-- BEGIN SEED DATA SECTION
--
IF OBJECT_ID('#tempdb..#SeedData') IS NOT NULL DROP TABLE #SeedData
SET NOCOUNT ON
CREATE TABLE #SeedData (
[UserName] nvarchar(50) NULL,
[CreatedById] [bigint] NULL,
[CreatedDate] [datetimeoffset](7) NULL
)
INSERT INTO #SeedData SELECT N'UserA',-1,getdate()
...
--
-- END SEED DATA SECTION
--
SET NOCOUNT OFF
INSERT INTO [Proj].[UserTable] (
[UserName],
[CreatedById],
[CreatedDate]
)
SELECT seed.[UserName]
,seed.[CreatedById]
,seed.[CreatedDate]
FROM #SeedData seed
DROP TABLE #SeedData
SET NOCOUNT OFF
END
GO
尝试的代码:
- IF OBJECT_ID('#SeedData', 'U') IS NOT NULL DROP TABLE #SeedData
- IF OBJECT_ID('tempdb..#SeedData', 'U') IS NOT NULL DROP TABLE #SeedData
答案 0 :(得分:2)
使用
IF OBJECT_ID('tempdb..#SeedData') is not null
drop table #seeddata
即使传递了table参数,我也无法重现这个问题。
测试:
create table #test
(
id int
)
select * from tempdb.sys.objects--you can see table
if object_id('tempdb..#test','u') is not null
drop table #test
select * from tempdb.sys.objects--you can't see table
答案 1 :(得分:2)
删除' #temp db ..'
前面的哈希值BEGIN
PRINT N'Seeding [Proj].[UserTable]...';
SET NOCOUNT ON
--
-- BEGIN SEED DATA SECTION
--
IF OBJECT_ID('tempdb..#SeedData') IS NOT NULL DROP TABLE #SeedData
SET NOCOUNT ON
CREATE TABLE #SeedData (
[UserName] nvarchar(50) NULL,
[CreatedById] [bigint] NULL,
[CreatedDate] [datetimeoffset](7) NULL
)
INSERT INTO #SeedData SELECT N'UserA',-1,getdate()
...
--
-- END SEED DATA SECTION
--
SET NOCOUNT OFF
INSERT INTO [Proj].[UserTable] (
[UserName],
[CreatedById],
[CreatedDate]
)
SELECT seed.[UserName]
,seed.[CreatedById]
,seed.[CreatedDate]
FROM #SeedData seed
DROP TABLE #SeedData
SET NOCOUNT OFF
END
GO
答案 2 :(得分:1)
请尝试使用drop table语句,将[Database].[Schema].[TableName]
替换为您尝试删除的对象。
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Database].[Schema].[TableName]') AND type in (N'U'))
DROP TABLE [Database].[Schema].[TableName]
GO