0

我的查询有什么问题?:(

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

如果我执行该查询,我会收到此错误

消息 102,级别 15,状态 1,第 2 行 ',' 附近的语法不正确。

4

2 回答 2

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
于 2013-07-03T09:09:23.080 回答
1

当您在低于 SQL Server 2008 的 SQL Server 中尝试多行插入时,您可能会遇到该错误。

查看下面的代码,即使它是 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
于 2013-07-03T09:18:53.200 回答