2015-11-02 69 views
0

所以我得到以下錯誤消息時查詢MS-SQL服務器:獲取味精102,期待「(」或選擇MS-SQL

**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', '[email protected]', 'animmalkingdomaz.com', '18562','testing'); 
('12','Pratts Pets', '5237 W Glendale Ave', 'Glendale', 'AZ', '850699999', '6025641234', '[email protected]', 'prattspets.com', '89456.00','testing'); 
('13','Pet Club Maricopa', '21445 N John Wayne Ave', 'Maricopa', 'AZ', '851389999', '5205809900', '[email protected]', 'thepetclub.net', '9999.00','testing'); 
('14','Wildside Pets', '1042 N Higley Rd', 'Mesa', 'AZ', '852059999', '8503457621', '[email protected]', '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'; 

任何幫助,將理解

回答

0

個的VALUES條款後的行應該用逗號分隔,而不是分號;

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

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

;應該作爲一個語句結束。

0

要SQLSERVER它diferent許多行的插入同時

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') 
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', '[email protected]', 'animmalkingdomaz.com', '18562','testing'), 
('12','Pratts Pets', '5237 W Glendale Ave', 'Glendale', 'AZ', '850699999', '6025641234', '[email protected]', 'prattspets.com', '89456.00','testing'), 
('13','Pet Club Maricopa', '21445 N John Wayne Ave', 'Maricopa', 'AZ', '851389999', '5205809900', '[email protected]', 'thepetclub.net', '9999.00','testing'), 
('14','Wildside Pets', '1042 N Higley Rd', 'Mesa', 'AZ', '852059999', '8503457621', '[email protected]', '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';