2013-04-29 175 views
1

當試圖從表中檢索字符串時,在此簡單SQL語句中出現此錯誤。將varchar值'my返回值'轉換爲數據類型int時轉換失敗

Msg 245,Level 16,State 1,Procedure prViewRequirements,Line 18 轉換varchar值'Cardiac Assessment Questionnaire by Dr.'時轉換失敗到數據類型int。

/****** Object: StoredProcedure [dbo].[prViewRequirements] Script Date: 04/24/2013 15:44:49 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[prViewRequirements] 
    @WFRouteID int 

AS 

DECLARE 
@DocumentDescription VARCHAR(100) 

SELECT @DocumentDescription = DocumentDescription 
      FROM tbFollowOnTracking 
      WHERE WFRouteID = @WFRouteID 
      AND IsActive = 1 


      IF (@@ERROR <> 0) 
       GOTO ERRSP  

     RETURN @DocumentDescription 

ERRSP: 
    RETURN -1 

有沒有人知道爲什麼?

+0

哪條線是線18? – Patashu 2013-04-29 06:06:56

+0

第18行是 - WHERE WFRouteID = @WFRouteID – 2013-04-29 06:11:25

回答

3

你試圖返回一個varchar而不是int。

RETURN @DocumentDescription 

請要麼做

select @DocumentDescription 

或使用輸出參數(推薦)

ALTER PROCEDURE [dbo].[prViewRequirements] 
    @WFRouteID int 
    , @DocumentDescription varchar(100) = null output 

更新 - 這裏是整個過程:

alter procedure dbo.prViewRequirements 
    @WFRouteID int 
    , @DocumentDescription varchar(100) = null output 
AS 

select @DocumentDescription = '' -- Init 

select @DocumentDescription = DocumentDescription 
from tbFollowOnTracking 
where WFRouteID = @WFRouteID 
and IsActive = 1 

return 0 
go 

/* Examples 
declare @DocumentDescription varchar(100) = '' 

exec dbo.prViewRequirements @WFRouteID = 10, @DocumentDescription = @DocumentDescription output 

select @DocumentDescription 
*/ 
+0

我一直對每個結果返回NULL。 – 2013-04-29 06:18:48

+0

它爲我工作。請仔細複製並粘貼關鍵字OUTPUT。 – 2013-04-29 06:26:56

0

試試這個 -

ALTER PROCEDURE [dbo].[prViewRequirements] 

     @WFRouteID INT 
    , @DocumentDescription VARCHAR(100) OUTPUT 

AS BEGIN 

    SELECT @DocumentDescription = t.DocumentDescription 
    FROM dbo.tbFollowOnTracking t 
    WHERE t.WFRouteID = @WFRouteID 
     AND t.IsActive = 1 

    IF @DocumentDescription IS NULL 
     RETURN -1  

    RETURN 0 

END 
+1

我仍然得到與此代碼相同的錯誤 – 2013-04-29 06:20:03

+0

對不起,有點小錯誤。請嘗試更新的答案。 – Devart 2013-04-29 06:24:52

0

嘗試:

alter procedure dbo.prViewRequirements 
    @WFRouteID int 
    , @DocumentDescription varchar(100) = null output 
AS 

BEGIN 
select @DocumentDescription = '' -- Init 

select @DocumentDescription = DocumentDescription 
from tbFollowOnTracking 
where WFRouteID = @WFRouteID 
and IsActive = 1 

END 

執行PROC如下所示

DECLARE @res varchar(100) 
exec dbo.prViewRequirements @WFRouteID,@[email protected] OUTPUT 
select @res 
相關問題