标题:关于存储过程添加出错?
取消只看楼主
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
 问题点数:0 回复次数:7 
关于存储过程添加出错?
本人编写了一段存储过程如下,对BookStateInfo表的添加出错了.
错误信息:---------------------------
Debugger Exception Notification
---------------------------
Project AppName.exe raised exception class EOleException with message '无法将 NULL 值插入列 'BookID',表 'lib.dbo.BookStateInfo';该列不允许空值。INSERT 失败。'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
我存储过程中已经对BookID做了处理了为什么还会出现这样的错误,我就很纳闷,希望哪位好心人帮我看一下到底错在哪里
create proc Insert_BookInfo
@ISDN varchar(10),
@BookName varchar(60),
@Author varchar(20),
@price float,
@Press varchar(50),
@PublishDate datetime,
@indate datetime,
@PressAddress varchar(50),
@PressTel varchar(20),
@BookState varchar(8),
@TotalStock integer
as
begin tran
declare @State varchar(10),@BookID varchar(13),@i integer,@BookStock integer,@tempBookID varchar(3)
set @BookStock=@TotalStock
set @i=1
set @State='归还'
while (@TotalStock>=@i)
begin
set @tempBookID=(convert(varchar(3),(select max(convert(int,right(BookID,len(BookID)-10)))+1
from BookStateInfo
where ISDN=@ISDN
)) )
while(len(@tempBookID)<3)
set @tempBookID='0'+@tempBookID
set @BookID=@ISDN+'-'+@tempBookID
insert into BookStateInfo(ISDN,BookID,State)
values(@ISDN,@BookID,@State)
set @i=@i+1
end
insert into BookInfo(ISDN,BookName,Author,price,Press,PublishDate,indate,PressAddress,PressTel,BookState,BookStock,TotalStock)
values(@ISDN,@BookName,@Author,@price,@Press,@PublishDate,@indate,@PressAddress,@PressTel,@BookState,@BookStock,@TotalStock)
commit
GO
搜索更多相关主题的帖子: class continue message stopped 
2006-04-18 14:13
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 

语法上是没错误了,不过功能上还是有点问题


2006-04-18 14:34
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 

表BookStateInfo只能添加一条记录


2006-04-18 14:40
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 

我现在把@BookID的长度改成14后,再调一下,当@TotalStock输入大于等于2时,BookStateInfo表中只有出现两条记录,本来就应该出现@TotalStock条记录


2006-04-18 22:42
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 
还是一样没变化.仍旧输出两条记录

2006-04-19 08:08
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 
版主!为什么set @tempBookID=(convert(varchar(3),(select isnull(max(convert(int,right(BookID,len(BookID)-10))),0)+1
from BookStateInfo
where ISDN=@ISDN
)) )
而不是set @tempBookID=(convert(varchar(3),(select isnull(max(convert(int,right(BookID,len(BookID)-10))),0)
from BookStateInfo
where ISDN=@ISDN
)+1) )
我觉得应该先把值取出来再加1嘛.

2006-04-19 08:14
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 
比如说@ISDN为MS-0000004
@BookID的两条为MS-0000004-000
MS-0000004-001
其它无论我添加什么样的@ISDN@BookID都只出现末尾为....-000和.....-001

2006-04-19 09:49
missking
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-4-8
得分:0 

原先我把@tempBookID长度设置为2,而本来应该是3,所以只有一条记录.......-00,后来我把长度改成3后变成......-000和.....-001两条记录了.
还有其它存储过程跟这个类似,它就能正确添加记录.
create proc Insert_BookStateInfo
@ISDN varchar(10),
@TotalStock integer
as
begin tran
declare @State varchar(10),@BookID varchar(14),@i integer,@tempBookID varchar(3)
set @i=0
set @State='归还'
while (@TotalStock>@i)
begin
set @tempBookID=(convert(varchar(3),(select max(convert(int,right(BookID,len(BookID)-10)))+1
from BookStateInfo
where ISDN=@ISDN )) )
while (len(@tempBookID)<3)
set @tempBookID='0'+@tempBookID
set @BookID=@ISDN+'-'+@tempBookID
insert into BookStateInfo(ISDN,BookID,State)
values(@ISDN,@BookID,@State)
set @i=@i+1
end
update BookInfo
set TotalStock=isnull(TotalStock,0)+@TotalStock
from BookInfo as a ,BookStateInfo as b
where a.ISDN=b.ISDN and b.ISDN=@ISDN
update BookInfo
set BookStock=isnull(BookStock,0)+@TotalStock
from BookInfo as a ,BookStateInfo as b
where a.ISDN=b.ISDN and b.ISDN=@ISDN
commit


GO


2006-04-19 10:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-58308-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.578118 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved