SQL Server存储过程增加varchar列的最大值并返回输出

时间:2015-04-07 11:02:22

标签: sql-server stored-procedures output

此我的表格D_Demande idDemandevarchar(10)

enter image description here

这是我的存储过程:

ALTER PROCEDURE [dbo].[get_maxIdDemande] 
    @maxidDemande varchar(10) output
AS
BEGIN
    declare @numDemande varchar(10)
    declare @currentDemandeDate varchar
    declare @currentnum varchar
    declare @num varchar

    select @numDemande = max([idDemande]) from [dbo].[D_DEMANDE]

    IF (@numDemande is null)
        BEGIN
            select @maxidDemande = FORMAT(getdate(),'ddMMyy')+'0001'
        END
    ELSE
        BEGIN
            set @currentDemandeDate = SUBSTRING(@numDemande,0,7)
            set @currentnum = SUBSTRING(@numDemande,7,4)
                IF(FORMAT(getdate(),'ddMMyy') = @currentDemandeDate)
                    BEGIN
                        set @num = CAST(@currentnum as int) + 1
                        select @maxidDemande = FORMAT(getdate(),'ddMMyy')+@num
                    END
                ELSE
                    BEGIN
                        select @maxidDemande = FORMAT(getdate(),'ddMMyy')+'0001'
                    END
        END
Return
END

调试这两个语句时得到0的问题:

set @currentDemandeDate = SUBSTRING(@numDemande,0,7)
set @currentnum = SUBSTRING(@numDemande,7,4)

你能帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您的问题出在您的变量声明中

ALTER PROCEDURE [dbo].[get_maxIdDemande] 

    @maxidDemande varchar(10) output
AS
BEGIN

    declare @numDemande varchar(10)
    declare @currentDemandeDate varchar   --<-- This is declared as VARCHAR(1)
    declare @currentnum varchar           --<-- This is also declared as VARCHAR(1)
    declare @num varchar                  --<--and this one too

您需要显式定义varchar变量的长度,否则它使用默认值1

应该是

declare @numDemande         varchar(10)
declare @currentDemandeDate varchar(10)
declare @currentnum         varchar(10)
declare @num                varchar(10)

程序定义

你的proc的固定版本看起来像......

ALTER PROCEDURE [dbo].[get_maxIdDemande] 
    @maxidDemande varchar(10) output
AS
BEGIN
    declare @numDemande         varchar(10)
    declare @currentDemandeDate varchar(10)
    declare @currentnum         varchar(10)
    declare @num                varchar(10)

    select @numDemande = max([idDemande]) from [dbo].[D_DEMANDE]


IF (@numDemande is null)
    BEGIN
        select @maxidDemande = FORMAT(getdate(),'ddMMyy')+'0001'
    END
ELSE
    BEGIN
        set @currentDemandeDate = SUBSTRING(@numDemande,0,7)
        set @currentnum = SUBSTRING(@numDemande,7,4)
            IF(FORMAT(getdate(),'ddMMyy') = @currentDemandeDate)
                BEGIN
                    set @num = RIGHT('0000' + CAST(CAST(@currentnum as int) + 1 AS VARCHAR(10)),4)
                    select @maxidDemande = FORMAT(getdate(),'ddMMyy')+@num
                END
            ELSE
                BEGIN
                    select @maxidDemande = FORMAT(getdate(),'ddMMyy')+'0001'
                END
    END
END

执行Proc

DECLARE @output VARCHAR(10);
EXEC [dbo].[get_maxIdDemande] @output OUTPUT
SELECT @output

使用表格中的当前数据,它应输出值0704150002