我的查询有什么问题? :(
USE tempdb;
IF OBJECT_ID('dbo.OrderTotals', 'V') IS NOT NULL
DROP VIEW dbo.OrderTotals;
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL
DROP TABLE dbo.OrderDetails;
GO
CREATE TABLE dbo.OrderDetails
(
oid INT NOT NULL,
pid INT NOT NULL,
qty INT NOT NULL,
PRIMARY KEY(oid, pid)
);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES
(10248, 1, 10),
(10248, 2, 20),
(10248, 3, 30),
(10249, 1, 5),
(10249, 2, 10),
(10249, 3, 15),
(10250, 1, 20),
(10250, 2, 20),
(10250, 3, 20);
GO
如果我执行该查询,我收到此错误
Msg 102,Level 15,State 1,Line 2','。附近的语法不正确。
答案 0 :(得分:2)
你的错误在第15行',' 尝试以下(未经测试):
USE tempdb;
IF OBJECT_ID('dbo.OrderTotals', 'V') IS NOT NULL
DROP VIEW dbo.OrderTotals;
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL
DROP TABLE dbo.OrderDetails;
GO
CREATE TABLE dbo.OrderDetails
(
oid INT NOT NULL,
pid INT NOT NULL,
qty INT NOT NULL,
PRIMARY KEY(oid, pid)
);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10248, 1, 10);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10248, 2, 20);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10248, 3, 30);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10249, 1, 5);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10249, 2, 10);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10249, 3, 15);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10250, 1, 20);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10250, 2, 20);
INSERT INTO dbo.OrderDetails(oid, pid, qty) VALUES (10250, 3, 20);
GO
答案 1 :(得分:1)
在SQL Server 2008下面的SQL Server中尝试Multi Row Insert时,可能会遇到该错误。
查看下面的代码,即使它是SQL Server 2005,它也能正常工作:
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL
DROP TABLE dbo.OrderDetails;
GO
CREATE TABLE dbo.OrderDetails
(
oid INT NOT NULL,
pid INT NOT NULL,
qty INT NOT NULL,
PRIMARY KEY(oid, pid)
);
INSERT INTO dbo.OrderDetails(oid, pid, qty)
select 10248, 1, 10 union all
select 10248, 2, 20 union all
select 10248, 3, 30 union all
select 10249, 1, 5 union all
select 10249, 2, 10 union all
select 10249, 3, 15 union all
select 10250, 1, 20 union all
select 10250, 2, 20 union all
select 10250, 3, 20