获取msg 102,期待'('或在ms-sql中选择

时间:2015-11-02 02:39:15

标签: sql-server tsql

因此,在查询ms-sql server时收到以下错误消息:

**Msg 102, Level 15, State 1, Line 40
Incorrect syntax near '11'.
Msg 102, Level 15, State 1, Line 78
Incorrect syntax near '61'.**

它表示期待'('或选择,它当然表示这是指示值

使用以下语法

    use master;
GO

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'tks15')
DROP DATABASE tks15;
GO

IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name=N'tks15')
CREATE DATABASE tks15;
GO

use tks15;
GO

IF OBJECT_ID (N'dbo.petstore', N'U') IS NOT NULL
DROP TABLE dbo.petstore;
GO
CREATE TABLE dbo.petstore
(
pst_id SMALLINT NOT NULL identity(1,1),
pst_name VARCHAR(30) NOT NULL,
pst_street VARCHAR(30) NOT NULL,
pst_city VARCHAR(30) NOT NULL,
pst_state CHAR(2) NOT NULL default 'AZ',
pst_zip int NOT NULL check (pst_zip > 0 and pst_zip <=999999999), 
pst_phone bigint NOT NULL,
pst_email VARCHAR(100) NOT NULL,
pst_url VARCHAR(100) NOT NULL,
pst_ytd_sales DECIMAL(10,2) NOT NULL check (pst_ytd_sales > 0),
pst_notes VARCHAR(255) NULL,
primary key(pst_id)
);

SELECT * FROM information_schema.tables;

insert into dbo.petstore
(pst_id,pst_name, pst_street, pst_city, pst_state, pst_zip, pst_phone, pst_email, pst_url, pst_ytd_sales, pst_notes)
values
('10','Carols Critters', '2401 W Pensacola St', 'Little Rock', 'AZ', '323019999', '8503457621', 'carolcritters.com', 'carolscrittters.com', '1000.00','testing');
('11','Puppies and Love', '4658 E Cactus Rd', 'Phoenix', 'AZ', '850059999', '6027789564', 'help@animalkingdomaz.com', 'animmalkingdomaz.com', '18562','testing');
('12','Pratts Pets', '5237 W Glendale Ave', 'Glendale', 'AZ', '850699999', '6025641234', 'pratt@prattspets.com', 'prattspets.com', '89456.00','testing');
('13','Pet Club Maricopa', '21445 N John Wayne Ave', 'Maricopa', 'AZ', '851389999', '5205809900', 'pets@thepetclub.net', 'thepetclub.net', '9999.00','testing');
('14','Wildside Pets', '1042 N Higley Rd', 'Mesa', 'AZ', '852059999', '8503457621', 'help@wildsidepetsmesa.com', 'wildsidepets.com', '899000','testing');

select * from dbo.petstore;

IF OBJECT_ID (N'dbo.pet', N'U') IS NOT NULL
DROP TABLE dbo.pet;
GO

CREATE TABLE dbo.pet
(
pet_id SMALLINT not null identity(1,1),
pst_id SMALLINT NOT NULL,
pet_type VARCHAR(45) NOT NULL,
pet_sex CHAR(1) NOT NULL CHECK (pet_sex IN('m', 'f')),
pet_cost DECIMAL(6,2) NOT NULL check (pet_cost > 0),
pet_price DECIMAL(6,2) NOT NULL check (pet_price > 0),
pet_age SMALLINT NOT NULL check (pet_age >= 0 and pet_age <=10500),
pet_color VARCHAR(30) NOT NULL,
pet_sale_date DATE NOT NULL,
pet_vaccine CHAR(1) NOT NULL check (pet_vaccine IN('y','n')),
pet_neuter CHAR(1) NOT NULL check (pet_neuter IN('y','n')),
pet_notes VARCHAR(255) NULL,
PRIMARY KEY (pet_id),
CONSTRAINT fk_pet_petstore
FOREIGN KEY (pst_id)
REFERENCES dbo.petstore (pst_id)
ON DELETE CASCADE 
ON UPDATE CASCADE 
);

SELECT * FROM information_schema.tables;
insert into dbo.pet
(pet_id, pst_id, pet_type, pet_sex, pet_cost, pet_price, pet_age, pet_color, pet_sale_date, pet_vaccine, pet_neuter, pet_notes)
values
('60','10','Bird','m','16.00','8.00','5','yellow','2015-08-26','y','y');
('61','11','Dog','m','49.99','32.23','6','Brown','2015-11-01','y','y');
('62','12','Cat','f','25.25','15.99','4','White','2015-10-31','y','y');
('63','13','Rabbit','m','49.99','37.99','1','White','2015-11-29','n','n');
('64','14','Turtle','f','55.23','46.67','6','Green','2015-09-11','n','n');

EXEC sp_help 'dbo.pet';

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:0)

VALUES子句后面的行应使用逗号,而不是分号;分隔:

insert into dbo.petstore(..)
values
(...),
(...),
(...);

insert into dbo.pet(..)
values
(...),
(...),
(...);

;应该用作语句终止符。

答案 1 :(得分:0)

对于SqlServer,不同的是同时插入多行

insert into dbo.pet
(pet_id, pst_id, pet_type, pet_sex, pet_cost, pet_price, pet_age, pet_color, pet_sale_date, pet_vaccine, pet_neuter, pet_notes)
select
('60','10','Bird','m','16.00','8.00','5','yellow','2015-08-26','y','y')
union select
('61','11','Dog','m','49.99','32.23','6','Brown','2015-11-01','y','y')
union select
('62','12','Cat','f','25.25','15.99','4','White','2015-10-31','y','y')
union select
('63','13','Rabbit','m','49.99','37.99','1','White','2015-11-29','n','n')
union select
('64','14','Turtle','f','55.23','46.67','6','Green','2015-09-11','n','n')

答案 2 :(得分:0)

在ISNERT子句中,值应以和;分隔。在您可以指定的最后一个值上;

correct code should be:
    use master;
GO

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'tks15')
DROP DATABASE tks15;
GO

IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name=N'tks15')
CREATE DATABASE tks15;
GO

use tks15;
GO

IF OBJECT_ID (N'dbo.petstore', N'U') IS NOT NULL
DROP TABLE dbo.petstore;
GO
CREATE TABLE dbo.petstore
(
pst_id SMALLINT NOT NULL identity(1,1),
pst_name VARCHAR(30) NOT NULL,
pst_street VARCHAR(30) NOT NULL,
pst_city VARCHAR(30) NOT NULL,
pst_state CHAR(2) NOT NULL default 'AZ',
pst_zip int NOT NULL check (pst_zip > 0 and pst_zip <=999999999), 
pst_phone bigint NOT NULL,
pst_email VARCHAR(100) NOT NULL,
pst_url VARCHAR(100) NOT NULL,
pst_ytd_sales DECIMAL(10,2) NOT NULL check (pst_ytd_sales > 0),
pst_notes VARCHAR(255) NULL,
primary key(pst_id)
);

SELECT * FROM information_schema.tables;

insert into dbo.petstore
(pst_id,pst_name, pst_street, pst_city, pst_state, pst_zip, pst_phone, pst_email, pst_url, pst_ytd_sales, pst_notes)
values
('10','Carols Critters', '2401 W Pensacola St', 'Little Rock', 'AZ', '323019999', '8503457621', 'carolcritters.com', 'carolscrittters.com', '1000.00','testing'),
('11','Puppies and Love', '4658 E Cactus Rd', 'Phoenix', 'AZ', '850059999', '6027789564', 'help@animalkingdomaz.com', 'animmalkingdomaz.com', '18562','testing'),
('12','Pratts Pets', '5237 W Glendale Ave', 'Glendale', 'AZ', '850699999', '6025641234', 'pratt@prattspets.com', 'prattspets.com', '89456.00','testing'),
('13','Pet Club Maricopa', '21445 N John Wayne Ave', 'Maricopa', 'AZ', '851389999', '5205809900', 'pets@thepetclub.net', 'thepetclub.net', '9999.00','testing'),
('14','Wildside Pets', '1042 N Higley Rd', 'Mesa', 'AZ', '852059999', '8503457621', 'help@wildsidepetsmesa.com', 'wildsidepets.com', '899000','testing');

select * from dbo.petstore;

IF OBJECT_ID (N'dbo.pet', N'U') IS NOT NULL
DROP TABLE dbo.pet;
GO

CREATE TABLE dbo.pet
(
pet_id SMALLINT not null identity(1,1),
pst_id SMALLINT NOT NULL,
pet_type VARCHAR(45) NOT NULL,
pet_sex CHAR(1) NOT NULL CHECK (pet_sex IN('m', 'f')),
pet_cost DECIMAL(6,2) NOT NULL check (pet_cost > 0),
pet_price DECIMAL(6,2) NOT NULL check (pet_price > 0),
pet_age SMALLINT NOT NULL check (pet_age >= 0 and pet_age <=10500),
pet_color VARCHAR(30) NOT NULL,
pet_sale_date DATE NOT NULL,
pet_vaccine CHAR(1) NOT NULL check (pet_vaccine IN('y','n')),
pet_neuter CHAR(1) NOT NULL check (pet_neuter IN('y','n')),
pet_notes VARCHAR(255) NULL,
PRIMARY KEY (pet_id),
CONSTRAINT fk_pet_petstore
FOREIGN KEY (pst_id)
REFERENCES dbo.petstore (pst_id)
ON DELETE CASCADE 
ON UPDATE CASCADE 
);

SELECT * FROM information_schema.tables;
insert into dbo.pet
(pet_id, pst_id, pet_type, pet_sex, pet_cost, pet_price, pet_age, pet_color, pet_sale_date, pet_vaccine, pet_neuter, pet_notes)
values
('60','10','Bird','m','16.00','8.00','5','yellow','2015-08-26','y','y'),
('61','11','Dog','m','49.99','32.23','6','Brown','2015-11-01','y','y'),
('62','12','Cat','f','25.25','15.99','4','White','2015-10-31','y','y'),
('63','13','Rabbit','m','49.99','37.99','1','White','2015-11-29','n','n'),
('64','14','Turtle','f','55.23','46.67','6','Green','2015-09-11','n','n');

EXEC sp_help 'dbo.pet';