这个SQL存储过程真是前所未有!请高人指点!

来源:百度知道 编辑:UC知道 时间:2024/05/24 08:37:19
CREATE DATABASE myDB
go
use myDB
go

if exists ( select * from sys.tables where name = 'StuInfo' )

drop table StuInfo
go

create table StuInfo
(
StuID int identity(1,1) primary key , StuName char(8) , Stusex char(2), StuAge int , exam int ,

)
go
insert into StuInfo values ( '花儿乐队','男 ' ,3 , 88 )
insert into StuInfo values ( '前程无忧','女 ' ,28 , 99 )

if exists (select * from sysobjects where name = 'proc_Stu')
drop procedure proc_Stu
go

if exists (select * from sysobjects where name = 'InsertDB')

drop procedure InsertDB
--delete from StuInfo where exam = 10 or exam = 50

go
create procedure InsertDB
@StuSex char(2), @exam int
as
insert into StuInfo(StuSex , exam) values (@StuSex , @exam)
execute InsertDB '男',50 ;
go
execute

你的代码里有一些小问题,这个部分:
create procedure InsertDB
@StuSex char(2), @exam int
as
insert into StuInfo(StuSex , exam) values (@StuSex , @exam)
execute InsertDB '男',50 ;
go

SQLServer会将以上代码整个认为是一个存储过程,因为少写了一个"go".
因此就变成了在存储过程中再嵌套存储过程,所以就形成了死循环.....改为下面这样就可以了:

create procedure InsertDB
@StuSex char(2), @exam int
as
insert into StuInfo(StuSex , exam) values (@StuSex , @exam)
--!!这里加上一个"go"!!
go
---------------------
execute InsertDB '男',50 ;
go