最近在学习存储过程,从网上直接复制了这个存储过程
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,但是适当多加,显得清晰不容易出错,希望会帮到大家