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,但是适当多加,显得清晰不容易出错,希望会帮到大家
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,但是适当多加,显得清晰不容易出错,希望会帮到大家
相关文章推荐
- SQL写存储过程时报错 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
- must be the first statement in a query batch
- java问题——Constructor call must be the first statement in a constructor
- Java异常解决:Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- In order to run a trace against SQL Server you must be a member of sysadmin fixed server role or have the ALTER TRACE permission.
- 有关Constructor call must be the first statement in a constructor的问题
- This is probably a good time to review the order in which SELECT statement clauses are to be specified. Table 10.2 lists all the clauses we have learned thus far, in the order they must be used.
- [Error]Namespace declaration statement has to be the very first statement in the script
- 错误:Namespace declaration statement has to be the very first statement in the script
- Django:ExtendsNode: extends 'base/base.html'> must be the first tag in the template.
- Why this line xmlns:android=“http://schemas.android.com/apk/res/android” must be the first in the la
- Unexpected XML declaration. The XML declaration must be the first node in the document and no white
- Fixing the "There is already an open DataReader associated with this Command which must be closed first." exception in Entity Framework
- 创建存储过程错误(已解决):Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server ver
- org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the qury 问题的解决