开发者

SQL Stored Procedure Parameters being Corrupted?

开发者 https://www.devze.com 2023-02-18 22:31 出处:网络
I have a stored procedure that for now is just suppose to validate your string input as a valid date. I\'ve tried running this outside of a procedure and it works flawlessly, but as soon as I stick it

I have a stored procedure that for now is just suppose to validate your string input as a valid date. I've tried running this outside of a procedure and it works flawlessly, but as soon as I stick it in a procedure, the parameters get changed to zero.

My procedure is as follows:

CREATE PROCEDURE spDateRange
@DateMin varchar = NULL,
@DateMax varchar = NULL
AS
PRINT @DateMin;
PRINT @开发者_Go百科DateMax;
IF @DateMin IS NOT NULL AND @DateMax IS NOT NULL
    BEGIN
    PRINT @DateMin;
    IF(ISDATE(@DateMin)=1 AND ISDATE(@DateMax)=1)
        PRINT 'Valid Date';
    ELSE
        RAISERROR('Parameters not date format', 10, 1);
    END
ELSE
    RAISERROR ('Invalid parameters', 10, 1);

And my execute statement is:

EXECUTE spDateRange @DateMin='01/11/2011', @DateMax='01/12/2011';

And my output is:

0
0
0
Parameters not date format


You need to give an explicit length for the varchar parameters. It defaults to 1 character.

char(10) should be enough for valid dates in dd/mm/yyyy format but I guess you might want longer as this is validating potentially invalid dates!

CREATE PROCEDURE spDateRange
@DateMin varchar(30) = NULL,
@DateMax varchar(30) = NULL
AS
0

精彩评论

暂无评论...
验证码 换一张
取 消