'CREATE PROCEDURE'必须是批次中唯一的声明(Erro)

时间:2016-12-07 16:33:14

标签: sql sql-server syntax-error

我正在创建此表,但我遇到了第一个过程的问题(sp_joseview)

+-----------+----------+----------------+--------+--------+
| vehicleID | peopleID |   makeModel    | regNo  | colour |
+-----------+----------+----------------+--------+--------+
|         1 |        1 | Bugatti Veyron |  12345 | White  |
|         2 |        2 | Porsche 911    |  67891 | Black  |
+-----------+----------+----------------+--------+--------+

第一个过程显示语法错误,但我无法弄清楚该错误可能是什么。

2 个答案:

答案 0 :(得分:9)

错误是不言自明的 - 您不能发出CREATE PROCEDURE语句,除非它是批处理中唯一的语句。

在SSMS中,GO关键字将语句拆分为单独的批处理,因此您需要在CREATE PROCEDURE之前的语句后添加一个:

create table josecustomer(
name varchar(50),
address varchar(300),
ssnid int,
balance bigint,
accountnumber bigint
)

insert into josecustomer values('Aman','Canada',10000,5000,100000000)
insert into josecustomer values('Shubham','USA',10001,6000,200000000)
insert into josecustomer values('Himanshu','Australia',10002,2000,300000000)
insert into josecustomer values('Jose','India',10003,3000,400000000)
insert into josecustomer values('Albert','Brazil',10004,4000,500000000)
insert into josecustomer values('Peterson','Germany',10005,7000,600000000)
insert into josecustomer values('Adam','Honkong',10006,8000,700000000)
insert into josecustomer values('William','Paris',10007,9000,800000000)

select * from josecustomer
Go  --Add a Go here

create proc sp_joseview
as begin
select * from josecustomer
end
go

create proc sp_updatejose
(@accountno bigint,@newbalance bigint)
as begin
update josecustomer 
set balance=@newbalance
where accountnumber=@accountno
end 
go

答案 1 :(得分:4)

关键字GO在单个脚本中分隔批次。该错误表示CREATE PROCEDURE必须是批处理中的“唯一”语句,因此您可能会错过某个GO以上的CREATE PROCEDURE

现在看看你的剧本,它分为4个广阔的领域

  1. 创建表格
  2. 插入一些数据(并再次选择)
  3. create proc 1
  4. create proc 2
  5. 很明显,你错过了一个介于2.和3之间的go