存储过程 - TRY_CAST()

时间:2017-06-18 13:58:42

标签: sql sql-server stored-procedures

我做了一个使用TRY_CAST的查询(@VAR为十进制(18,2)) 它正在工作,但当我尝试'0.00'时,它返回null

DECLARE @FROMHEADERPH TABLE (id int identity, headername nvarchar(max))
INSERT INTO @FROMHEADERPH
SELECT LTRIM(RTRIM(column_name)) FROM tbl_hrms_header where item_group = 'PerHour'

DECLARE @PERHOUR int
SELECT @PERHOUR = COUNT(*) from @FROMHEADERPH
DECLARE @ALLDATEFORMATSBM TABLE (
[Id]        int identity,
[SBM]   nvarchar(max)
)

DECLARE @MAX int = 1
WHILE @MAX <= @PERHOUR
BEGIN
    DECLARE @WEWE nvarchar(max)
    SELECT @WEWE = headername from @FROMHEADERPH where id = @MAX


    DECLARE @q nvarchar(max)
    SET @q = N'SELECT '+ @WEWE +' FROM TemporaryProfile'
    INSERT INTO  @ALLDATEFORMATSBM(SBM)
    EXEC (@q)

    DECLARE @IFCONVERTED table(id int identity, [FIELDS] nvarchar(max));

    DECLARE @TSBM table(id int identity, [TSBM] nvarchar(max)); 
    DECLARE @VSBM nvarchar(max);

    DECLARE @MAXSBM int
    SELECT @MAXSBM = MAX(id) FROM @ALLDATEFORMATSBM
    DECLARE @COUNTSBM int;
    SELECT @COUNTSBM = MIN(id) FROM @ALLDATEFORMATSBM
    WHILE @MAXSBM >= @COUNTSBM
    BEGIN
        DECLARE @SBM nvarchar(max);
        SELECT @SBM = SBM FROM @ALLDATEFORMATSBM WHERE Id = @MAXSBM


        INSERT INTO @TSBM
        SELECT   
        CASE WHEN TRY_CAST(@SBM AS decimal(18,2)) IS NULL   
        THEN 'Cast failed'  
        ELSE 'Cast succeeded'  
        END AS Result;

        DECLARE @WEW int
        SELECT @WEW = MIN(id) FROM @TSBM;


            SELECT @VSBM = TSBM FROM @TSBM Where id = @WEW
        PRINT CAST(@MAXSBM as nvarchar(max)) + ' - ' + CAST(@COUNTSBM as nvarchar(max)) + ' # ' + @SBM + ' !@ ' + CAST(@WEW as nvarchar(max)) + ' :: ' + @VSBM
            if @VSBM = 'Cast failed'
            BEGIN
             insert into @IFCONVERTED([FIELDS]) values(@WEWE);
             BREAK;
            END
            ELSE
            BEGIN
             Print '---'
            END


        DELETE FROM @TSBM
        SET @MAXSBM = @MAXSBM - 1
    END
    DELETE FROM @ALLDATEFORMATSBM
    SET @MAX = @MAX + 1
END
SELECT * FROM @IFCONVERTED

@FROMHEADERPH有:

dailyPay | MonthLyPay | YearlyPay
Leona    | 0.00       | 0.00
Elizabeth| 0.00       | 0.00

我使用字符串填充dailypay来检查我的try_cast。它正在第一列工作,但是当它出现另外两个具有数值的列时,它就会失败。

有人可以帮我吗?我已经烧坏了。 :(

1 个答案:

答案 0 :(得分:0)

我重建了我的查询。这是我的最终查询

DECLARE @FROMHEADERPH TABLE (id int identity, headername nvarchar(max))
INSERT INTO @FROMHEADERPH
SELECT LTRIM(RTRIM(column_name)) FROM tbl_hrms_header where item_group = 'PerHour'

DECLARE @IFCONVERTED table(id int identity, [FIELDS] nvarchar(max)); --  collect all invalid fields

DECLARE @PERHOUR int
SELECT @PERHOUR = COUNT(*) from @FROMHEADERPH
DECLARE @ALLDATEFORMATSBM TABLE (
    [Id]        int identity,
    [DataStringss]   nvarchar(max)
    )
DECLARE @MAX int = 1
WHILE @MAX <= @PERHOUR
BEGIN
    DECLARE @WEWE nvarchar(max)
    SELECT @WEWE = headername from @FROMHEADERPH where id = @MAX


    --PRINT @WEWE + '-' + cast(@MAX AS NVARCHAR(MAX))
    DECLARE @q nvarchar(max)
    SET @q = N'SELECT '+ @WEWE +' FROM TemporaryProfile'
    INSERT INTO  @ALLDATEFORMATSBM([DataStringss])
    EXEC (@q)
    SELECT * FROM @ALLDATEFORMATSBM

    DECLARE @COUNTH int, @MAXH int = (SELECT MIN(Id) FROM @ALLDATEFORMATSBM)
    SELECT @COUNTH = MAX(Id) FROM @ALLDATEFORMATSBM
    PRINT CAST(@MAXH as nvarchar(max)) + '-' + CAST(@COUNTH as nvarchar(max))

    WHILE @MAXH <= @COUNTH
    BEGIN
        DECLARE @DS nvarchar(max)
        SELECT @DS = [DataStringss] FROM @ALLDATEFORMATSBM WHERE Id = @MAXH
        PRINT @DS

        DECLARE @CASTING nvarchar(max)
        SELECT @CASTING = TRY_CAST(@DS AS NUMERIC(18,2))

        IF @CASTING is null
        BEGIN
            INSERT INTO @IFCONVERTED
            VALUES(@WEWE)
            BREAK
        END
        ELSE
        BEGIN
            PRINT '----'
        END

        SET @MAXH = @MAXH + 1
    END


    DELETE FROM @ALLDATEFORMATSBM
    SET @MAX = @MAX + 1
END
    SELECT DISTINCT * FROM @IFCONVERTED