关于SQL2000 自动创建自动单号的问题--存储过程(200)

  • 主题发起人 骷髅头
  • 开始时间

骷髅头

Unregistered / Unconfirmed
GUEST, unregistred user!
t_ProductType表ProductType ProductTypeJM纸杯类 ZBL玩具类 WJL笔类 BL日用品类 RYPLt_ProductInfo表ProductID ProudctType ..........WJL0000001 玩具类WJL0000002 玩具类BL00000001 笔类 RYPL000001 日用类... ...CREATE procedure Add_ProductInfo --向登记票号表中插入数据,自动生成票号,并将票号返回给程序 @ProductType varchar(30), @ProductUnit varchar(30), @ProductPrice money, @ProductName varchar(50), @ProductBarCode varchar(50), @ProductItemNo varchar(50), @ProductModel char(10), @ProductID char(10) output asbegin Declare @temp char(10) --提取游标中的数据 --声明一个游标用于提取字段中最大票号 Declare @tmpCol char(10) Declare @i int Declare @j int set @i= (select len( ProductTypeJm) as 'mLength' from t_ProductType where ProductType=@productType) set @j=10-@i set @tmpCol=right('000000001',@j) set @ProductID =(select ProductTypeJm from t_ProductType where ProductType=@productType)+ @tmpCol declare fetch_MaxPh Cursor LOCAL Scroll for select Max(ProductID) from t_ProductInfo if CURSOR_STATUS('local','fetch_Maxph')= -1 --如果游标未打开,打开游标 Open fetch_Maxph Fetch first from fetch_Maxph into @temp Close fetch_Maxph Declare @Id Int if @temp is NUll begin set @ProductID = @ProductID+@tmpCol end else begin set @id =Convert(int,right(@temp,@j))+power(10,@j)+1 set @tmpCol= Convert(char(10),@id) set @ProductID =@ProductID + right(@tmpCol,@j) -- if @id <9 -- set @ProductID = @ProductID +'000000000'+Convert(char(10),@id+1) -- else if @id <99 -- set @ProductID = @ProductID+'0'+Convert(char(10),@id+1) -- else if @id<999 -- set @ProductID = @ProductID + Convert(char(10),@id+1) end insert into t_ProductInfo values (@ProductID,@ProductType,@ProductUnit,@ProductPrice,@ProductName,@ProductBarCode,@ProductItemNo,@ProductModel)endGO问题是数据可以创建,但同类型的编号却没有增加
 

骷髅头

Unregistered / Unconfirmed
GUEST, unregistred user!
select Max(ProductID) from t_ProductInfo 改成select Max(ProductID) from t_ProductInfo where ProductType=@ProductType也是不行的,在SQL2000里调试了,但没单步调试(按钮是灰色)
 
Z

zbdzjx

Unregistered / Unconfirmed
GUEST, unregistred user!
经过测试,发现你的程序中有两个错误:1、set @ProductID =(select ProductTypeJm from t_ProductType where ProductType=@productType)+ @tmpCol假设我们查询的是“玩具类”。在两个括号中,返回的是“WJL”,而@tmpcol为0000001,则@ProductID为“WJL0000001”。按你后面的代码,这里就不要加@tmpcol。2、set @ProductID = @ProductID+@tmpCol和set @ProductID =@ProductID + right(@tmpCol,@j)通过上面1中的分析,你的@ProductID为“WJL0000001”,那上面两句代码中,@ProductID无论加上什么,结果都是“WJL0000001”,因为@ProductID的类型是char(10)。修改方法:1、针对上面的错误1,改成“set @ProductID =(select ProductTypeJm from t_ProductType where ProductType=@productType)”,后面不用加@tmpCol,这样,返回的就是“WJL ”。2、针对上面的错误2,改成“set @ProductID = rtrim(@ProductID)+@tmpCol”和“set @ProductID =rtrim(@ProductID) + right(@tmpCol,@j)”,之所以要加rtrim的原因是:在上面的修改1中,返回的@ProductID后面有7个空格,如果不去掉,就不正确了。
 
X

xuhongxinghome

Unregistered / Unconfirmed
GUEST, unregistred user!
如果是要求返回 WJL0000001 ..WJL9999999 这样的流水号的话,最好用函数解决set @ProductID =DBO.函数名(@productType)函数中处理增量问题,流水号为 BIGINT 类型,假设定义为 LSH DECLARE @LSH BIGINT,@KIND_ID VARCHAR(10)select @LSH=LSH from t_ProductType where ProductType=@productTypeUPDATE t_ProductType SET LSH=ISNULL(@LSH,0)+1SET @KIND_ID=@productType+RIGHT('0000000000'+LTRIM(STR(ISNULL(@LSH,0),7,0)),7)RETURN @KIND_ID好像函数中不允许UPDATE,改成存储过程既可EXEC 存储过程名 @productType,@ProductID OUTPUT
 

骷髅头

Unregistered / Unconfirmed
GUEST, unregistred user!
to zbdzjx 谢谢你的帮助,但是我在机子测试了,结果ProductID 还是0000001没有增加
 

骷髅头

Unregistered / Unconfirmed
GUEST, unregistred user!
to xuhongxinghome 谢谢你帮助ProductID=ProductTypeJm+0000001ProductTypeJm为产品类型简称(比如:WJL 字符数不一样的,但限,不大于7位,这个在t_ProductType限制 )
 
X

xuhongxinghome

Unregistered / Unconfirmed
GUEST, unregistred user!
ProductTypeJm 不大于7位,@ProductID char(10) 10-7=3 ,1000个就超界了,不行的,应该按照无穷大的思路做,我吃过类似的苦头,老升级受不了
 

骷髅头

Unregistered / Unconfirmed
GUEST, unregistred user!
to xuhongxinghome 非常感谢您的参与,同类型种品是超不过1000的,不同的类型的简称是不一样的,若类型简码在1-7位之间变动呢
 
X

xuhongxinghome

Unregistered / Unconfirmed
GUEST, unregistred user!
接着上面的:CREATE PROCEDURE [DBO].[GET_PRODUCT_ID] @productType VARCHAR(7),@KIND_ID VARCHAR(10) OUTPUT AS DECLARE @LSH BIGINT,@KIND_ID VARCHAR(10),@JM_LEN INT,@MAX_KIND_LEN INTSET @MAX_KIND_LEN =10,@JM_LEN=LEN(@productType) -- 准备未来改动 @MAX_KIND_LENselect @LSH=LSH from t_ProductType where ProductType=@productTypeSET @LSH=ISNULL(@LSH,0)UPDATE t_ProductType SET LSH=ISNULL(@LSH,0)+1SET @KIND_ID=@productType+RIGHT('0000000000'+LTRIM(STR(ISNULL(@LSH,0),10,0)),@MAX_KIND_LEN-@JM_LEN)GO调用: EXEC GET_PRODUCT_ID @productType,@ProductID OUTPUT再加上对极端的边界条件的处理,大概就差不多了,可能的边界条件是 @productType=‘’@MAX_KIND_LEN-@JM_LEN<=0,@LSH>MAX_KIND_LEN-@JM_LEN 个'9'
 

骷髅头

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 
顶部