2016-12-06 96 views
0

sm_Value是一個TEXT列,我需要將其轉換爲INT在SQL中將文本轉換爲INT

作爲一個事實,即只有兩個人會亂用這個表中,列將是數值(幽默我)

我收到此錯誤信息:

消息206,級別16,狀態2,過程User_Password_Date_Interval_Check,10號線
操作數類型衝突:文本與詮釋

不相容我曾嘗試:

SELECT CONVERT(INT, sm_Value)) 
FROM Setting_Misc 

但我仍然得到錯誤。

IF EXISTS (SELECT * FROM sysobjects 
      WHERE type = 'P' AND name = 'User_Password_Date_Interval_Check') 
BEGIN 
    PRINT 'Dropping Procedure User_Password_Date_Interval_Check' 
    DROP Procedure User_Password_Date_Interval_Check 
END 
GO 

PRINT 'Creating Procedure User_Password_Date_Interval_Check' 
GO 

CREATE Procedure User_Password_Date_Interval_Check 
    @ua_pk uniqueidentifier 
AS 
    DECLARE @PasswordExpireDays INT 

    SELECT 
     CONVERT(INT, CONVERT(VARCHAR(MAX), sm_Value)) 
    FROM Setting_Misc 

    SELECT 
     @PasswordExpireDays = COALESCE(sm_Value,0) 
    FROM 
     Setting_Misc AS sm 
    INNER JOIN 
     Syndicates As syn ON sm.syn_fk = syn.syn_pk 
    INNER JOIN 
     Company As c ON c.syn_fk = syn.syn_pk 
    INNER JOIN 
     User_Accounts As ua ON ua.c_fk = c.c_pk 
    WHERE 
     sm.sm_Setting = 'PasswordExpireDays' 
GO 

GRANT EXEC ON User_Password_Date_Interval_Check TO WEB 
+1

你爲什麼先把它轉換成varchar(max)? – scsimon

+0

[將數字轉換成文本數據類型轉換爲int](http://stackoverflow.com/questions/15346002/convert-number-stored-as-text-data-type-to-int) – dfundako

+0

'@PasswordExpireDays '似乎是你的錯誤是如此只是將它轉換爲'@PasswordExpireDays = COALESCE(CONVERT(INT,CONVERT(varchar(max),sm_Value)),0)' – scsimon

回答

1

您必須在設置變量的位置進行轉換。

IF EXISTS (SELECT * FROM sysobjects 
      WHERE type = 'P' AND name = 'User_Password_Date_Interval_Check') 
BEGIN 
    PRINT 'Dropping Procedure User_Password_Date_Interval_Check' 
    DROP Procedure User_Password_Date_Interval_Check 
END 
GO 

PRINT 'Creating Procedure User_Password_Date_Interval_Check' 
GO 

CREATE Procedure User_Password_Date_Interval_Check 
    @ua_pk uniqueidentifier 
AS 
    DECLARE @PasswordExpireDays INT 
    --This conversion doesn't cascade down to the next SELECT statement 
    SELECT 
     CONVERT(INT, CONVERT(VARCHAR(MAX), sm_Value)) 
    FROM Setting_Misc 

    SELECT 
     --Here is what has to change 
     [email protected] = COALESCE(sm_Value,0) 
      @PasswordExpireDays = CONVERT(INT, CONVERT(VARCHAR(MAX), sm_Value)) 
    FROM 
     Setting_Misc AS sm 
    INNER JOIN 
     Syndicates As syn ON sm.syn_fk = syn.syn_pk 
    INNER JOIN 
     Company As c ON c.syn_fk = syn.syn_pk 
    INNER JOIN 
     User_Accounts As ua ON ua.c_fk = c.c_pk 
    WHERE 
     sm.sm_Setting = 'PasswordExpireDays' 
GO 

GRANT EXEC ON User_Password_Date_Interval_Check TO WEB 
+0

你。是。一個。天賜良機。謝謝! –

+1

哈我不會那麼遠@KevinFischer,但很樂意幫助 – scsimon

+0

,只是爲了官方目的,它的工作原理。沒有錯誤 –