我有以下代码。在这里我将丢弃并在同一会话中重新创建相同的临时表。但是第二次创建表时不会丢失表。它引发了一个错误。任何人都有理由帮助我吗?提前致谢。
IF OBJECT_ID('TEMPDB..#Table1','U') IS NOT NULL
BEGIN
DROP TABLE #Table1
END
CREATE TABLE #Table1
([seller_name] varchar(3), [id_seller] int, [id_buyer] int)
INSERT INTO #Table1
([seller_name], [id_seller], [id_buyer])
VALUES
('abc', 1005, 1006),
('ddd', 1009, 1186),
('ccc', 1006, 1001),
('ccc', 1006, 1002),
('ddd', 1009, 1006),
('tyu', 1001, 1186)
GO
IF OBJECT_ID('TEMPDB..#Table1','U') IS NOT NULL --Not entering into `IF` block
BEGIN
DROP TABLE #Table1
END
CREATE TABLE #Table1
([seller_name] varchar(3), [id_seller] int, [id_buyer] int, [id_buyr] int)
INSERT INTO #Table1
([seller_name], [id_seller], [id_buyer],[id_buyr])
VALUES
('abc', 1005,8,1006),
('ddd', 1009,8,1186),
('ccc', 1006,8,1001),
('ccc', 1006,8,1002),
('ddd', 1009,8,1006),
('tyu', 1001,8,1186)
去
答案 0 :(得分:1)
在第二次创建表时添加GO语句。它的工作原理
IF OBJECT_ID('TEMPDB..#Table1','U') IS NOT NULL
BEGIN
DROP TABLE #Table1
END
GO
CREATE TABLE #Table1
([seller_name] varchar(3), [id_seller] int, [id_buyer] int, [id_buyr] int)
INSERT INTO #Table1
([seller_name], [id_seller], [id_buyer],[id_buyr])
VALUES
('abc', 1005,8,1006),
('ddd', 1009,8,1186),
('ccc', 1006,8,1001),
('ccc', 1006,8,1002),
('ddd', 1009,8,1006),
('tyu', 1001,8,1186)
;
答案 1 :(得分:1)
试试这个
IF OBJECT_ID('TEMPDB..#Table1','U') IS NOT NULL
BEGIN
DROP TABLE #Table1
END
GO
CREATE TABLE #Table1
([seller_name] varchar(3), [id_seller] int, [id_buyer] int)
INSERT INTO #Table1
([seller_name], [id_seller], [id_buyer])
VALUES
('abc', 1005, 1006),
('ddd', 1009, 1186),
('ccc', 1006, 1001),
('ccc', 1006, 1002),
('ddd', 1009, 1006),
('tyu', 1001, 1186)
GO
IF OBJECT_ID('TEMPDB..#Table1','U') IS NOT NULL
BEGIN
DROP TABLE #Table1
END
GO
CREATE TABLE #Table1
([seller_name] varchar(3), [id_seller] int, [id_buyer] int, [id_buyr] int)
INSERT INTO #Table1
([seller_name], [id_seller], [id_buyer],[id_buyr])
VALUES
('abc', 1005,8,1006),
('ddd', 1009,8,1186),
('ccc', 1006,8,1001),
('ccc', 1006,8,1002),
('ddd', 1009,8,1006),
('tyu', 1001,8,1186)
GO