2015-05-26 104 views
2

我有存储过程来产生,其中我传递的优惠券代码前缀混淆而产生的优惠券代码

CREATE PROCEDURE [dbo].[spCouponCode1] 
     @Prefix varchar(50), 
     @Lenght varchar(50) 
    AS 
    Begin 
    declare @maxID as bigint=0 
    declare @PrefixLenght as bigint=0 
     set @PrefixLenght=LEN(@Prefix) 
    select @maxID = isnull(max(substring(CouponCode,@PrefixLenght+1,@[email protected])),0) + 1 from Coupon where CouponCode Like @Prefix + '%' 
    select @Prefix + cast(@maxID as VARchar(100)) 
    end 

,它正在完美 问题是优惠券代码, 如果优惠券桌子上有优惠券代码,如“ FIRST0001'和'FIRSTNEW001' 这里我得到错误 - varchar到int转换失败,因为我试图解析'NEW001'到int找到最大值 是否有可能解析此'NEW001'为int,忽略起始字符

+1

使用了标记dbms! (看起来不像ANSI SQL ...) – jarlh

+0

看起来像oracle存储过程给我,但不知道 –

+0

我在做这个MS SQL 2008 –

回答

2

对于Sql Server尝试:

select @maxID = isnull(max(substring(CouponCode, patindex('%[0-9]%', CouponCode), len(CouponCode))),0) + 1 
from Coupon where CouponCode Like @Prefix + '%' 
+0

非常感谢你 –