Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'CREATE PROCEDURE' must be the only statement in the batch (Erro) [duplicate]

I'm creating this table but I'm running into a problem with the first procedure (sp_joseview)

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

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

There is a syntax error showing for the first procedure but I can't figure out what that error might be.

like image 938
Joe Morales Avatar asked Dec 07 '16 16:12

Joe Morales


2 Answers

The keyword GO separates batches in a single script. The error says that a CREATE PROCEDURE must be the "only" statement in a batch so it stands to reason you're missing a GO above a CREATE PROCEDURE somewhere.

Now look at your script, its separated into 4 broad areas

  1. create a table
  2. insert some data (and select it out again)
  3. create proc 1
  4. create proc 2

Its clear that you're missing a go between 2. and 3.

like image 166
Jamiec Avatar answered Oct 28 '22 17:10

Jamiec


The error is self-explanatory - you cannot issue a CREATE PROCEDURE statement unless it's the only statement in the batch.

In SSMS the GO keyword splits the statement into separate batches, so you need to add one after the statement before the 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
like image 27
Siyual Avatar answered Oct 28 '22 18:10

Siyual