请详解一个存储过程,请“详解”!谢谢(100分)

C

cjj2

Unregistered / Unconfirmed
GUEST, unregistred user!
CREATE PROCEDURE Gp_InsertA
(
@Parid [varchar](25),
@DbName [varchar](30),
@szFlag [char](2),
@UserCode [varchar](26),
@FullName [varchar](66),
@Name [varchar](30),
@Total00 [numeric](18,4),
@Comment [varchar](256),
@PinYinCode [varchar](66)
)
AS
declare @nReturntype int,@EtypeId_1 [varchar](25),@nSoncount int,@nSonnum int,@leveal smallint


exec @nReturntype=Gp_createId @ParId,@dbname,@EtypeId_1 out,@nSonnum out,@nSoncount out

if @nReturntype=-1 return -1
if @nReturntype=-2 return -2

select * from [atype]
where typeId=@EtypeId_1 --or usercode=@usercode and deleted<>1

if @@Rowcount<>0
begin
-- print '商品已存在'+@etypeid_1
return -3
end

begin tran insertproc

select @leveal=leveal from atype where typeid=@Parid
select @leveal=@leveal+1
INSERT INTO [atype]
( [typeId],
[Parid],
[leveal],
[soncount],
[sonnum],
[FullName],
[Name],
[UserCode],
[total00],
[sysflag],
[Comment],
[PinYinCode]
)

VALUES
( @EtypeId_1,
@Parid,
@leveal,
0,
0,
@FullName,
@Name,
@UserCode,
@Total00,
@szFlag,
@Comment,
@PinYinCode
)

if @@rowcount=0
begin
rollback tran insertproc
return -1
end else
begin
update [atype]
set sonnum=@nSonnum+1,soncount=@nSoncount+1
where typeid=@Parid
end

commit tran insertproc

越详细越好!
另外问一个问题:存储过程中的set nocount on是什么意思?

 
此时无人应答?
 
CREATE PROCEDURE Gp_InsertA
(
@Parid [varchar](25),
@DbName [varchar](30),
@szFlag [char](2),
@UserCode [varchar](26),
@FullName [varchar](66),
@Name [varchar](30),
@Total00 [numeric](18,4),
@Comment [varchar](256),
@PinYinCode [varchar](66)
) --定义一个存储过程括号里面是传入参数
AS
declare @nReturntype int,@EtypeId_1 [varchar](25),@nSoncount int,@nSonnum int,@leveal smallint
--声明一些在存储过程中用到的临时变量

exec @nReturntype=Gp_createId @ParId,@dbname,@EtypeId_1 out,@nSonnum out,@nSoncount out
--执行Gp_createId 这个存储过程,返回的结果代码给@nReturntype
if @nReturntype=-1 return -1
if @nReturntype=-2 return -2
--根据不同结果返回不同的结果代码 return 执行后,退出过程
select * from [atype]
where typeId=@EtypeId_1 --or usercode=@usercode and deleted<>1
--select 语句没有什么说的
if @@Rowcount<>0 --如果查询的记录条数大于0
begin
-- print '商品已存在'+@etypeid_1
return -3 --输出提示信息,返回错误代码
end

begin tran insertproc --开始一个事务处理似乎前面要加上 go 语句

select @leveal=leveal from atype where typeid=@Parid -- 一个select 赋值语句
select @leveal=@leveal+1 --此处的Select=set
INSERT INTO [atype]
( [typeId],
[Parid],
[leveal],
[soncount],
[sonnum],
[FullName],
[Name],
[UserCode],
[total00],
[sysflag],
[Comment],
[PinYinCode]
)

VALUES
( @EtypeId_1,
@Parid,
@leveal,
0,
0,
@FullName,
@Name,
@UserCode,
@Total00,
@szFlag,
@Comment,
@PinYinCode
)
--insert 语句没有什么说的
if @@rowcount=0 --如果插入失败(因为返回的受影响的记录数为0)
begin
rollback tran insertproc --回滚从begintrans开始的动作sql语句
return -1 --返回错误代码
end else
begin
update [atype]
set sonnum=@nSonnum+1,soncount=@nSoncount+1
where typeid=@Parid --一个update语句没有什么好说的
end

commit tran insertproc --提交事务,实现对数据记录的物理修改
 
补充一下,set nocount on表示对查询或更新记录的时候,不返回记录的条数
 
CREATE PROCEDURE Gp_InsertA //创建一个存储过程
(
@Parid [varchar](25),
@DbName [varchar](30),
@szFlag [char](2),
@UserCode [varchar](26),
@FullName [varchar](66),
@Name [varchar](30),
@Total00 [numeric](18,4),
@Comment [varchar](256),
@PinYinCode [varchar](66)
) //定义输入参数
AS
declare @nReturntype int,@EtypeId_1 [varchar](25),@nSoncount int,@nSonnum int,@leveal smallint
//定义一些变量

exec @nReturntype=Gp_createId @ParId,@dbname,@EtypeId_1 out,@nSonnum out,@nSoncount out
//执行另外一个存储过程,返回结果为@nReturntype,上句中的out指那个参数是输出的

if @nReturntype=-1 return -1
if @nReturntype=-2 return -2
//根据返回结果做相应动作
select * from [atype]
where typeId=@EtypeId_1 --or usercode=@usercode and deleted<>1
//从atype表中select相应数据

if @@Rowcount<>0 // 如果返回记录数为0
begin
-- print '商品已存在'+@etypeid_1 // --为注释
return -3
end

begin tran insertproc //开始一个事务处理

select @leveal=leveal from atype where typeid=@Parid
select @leveal=@leveal+1
INSERT INTO [atype]
( [typeId],
[Parid],
[leveal],
[soncount],
[sonnum],
[FullName],
[Name],
[UserCode],
[total00],
[sysflag],
[Comment],
[PinYinCode]
)

VALUES
( @EtypeId_1,
@Parid,
@leveal,
0,
0,
@FullName,
@Name,
@UserCode,
@Total00,
@szFlag,
@Comment,
@PinYinCode
)
//往插入atype里插入数据
if @@rowcount=0 //如果插入记录数为0
begin
rollback tran insertproc //回滚事务
return -1
end else
begin
update [atype]
set sonnum=@nSonnum+1,soncount=@nSoncount+1
where typeid=@Parid //修改atype表 
end

commit tran insertproc        //提交事务  

越详细越好!
另外问一个问题:存储过程中的set nocount on是什么意思?
//set nocount on 是使返回的结果中不包含有关受 Transact-SQL 语句
影响的行数的信息,主要作用是当存储过程中有很多中间数据的时候,
    可以先set nocount on,即不计数,等到需要计算受影响行数时,再
set nocount off
 
谢谢两位
 
多人接受答案了。
 
顶部