您的位置:首页 > 产品设计 > UI/UE

SQL写存储过程时报错 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

2016-09-06 20:41 836 查看
最近在学习存储过程,从网上直接复制了这个存储过程

if exists

(select name from sysobjects where name ='up_getallstudents' and type ='p')

drop procedure up_getallstudents

--编写存储过程up_getallstudents,用于获取学生表students的所有记录 

create procedure up_getallstudents 

as

select * from T_stu_cou

然后create procedure up_getallstudents 总报错,执行之后就提示

Msg 111, Level 15, State 1, Procedure up_getallstudents, Line 9

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

然后找了一下原因,在create procedure up_getallstudents前缺少了GO,

因为这些语句需要放在单独的批处理中,在SQL Server中使用go来分批.

所以正确语句是

if exists

(select name from sysobjects where name ='up_getallstudents' and type ='p')

drop procedure up_getallstudents

--编写存储过程up_getallstudents,用于获取学生表students的所有记录 

GO

create procedure up_getallstudents 

as

select * from T_stu_cou

当然不是写一句就加GO,只要是在一个批处理就不用加GO,但是适当多加,显得清晰不容易出错,希望会帮到大家
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐