2017-05-02 179 views
0

我試圖執行一個存儲過程,該過程輸入從查詢中選擇的3個參數。前兩個存儲過程參數應該是intbigint,但sql不接受它並告訴我它不能將nvarchar類型轉換爲bigint。「將數據類型nvarchar轉換爲bigint時出錯」執行存儲過程時

因此,我將參數類型更改爲nvarchar,但現在在存儲過程中執行查詢時出現此錯誤。我試圖將nvarchar轉換爲bigint,但即使參數值是數字,它也不起作用。

這裏是我如何執行存儲過程:在外觀傳遞

[dbo].[InsertMultiChoiceList] [PatientRiskAssessmentQuestionsID], NetworkRiskAssessmentQuestionsID, Answer 

的參數,如這些:

230124| 118 |COPD (Chronic Obstructive Pulmonary Disease), Congestive Heart Failure (CHF), Sleep Apnea 

這裏我的存儲過程的定義:

ALTER PROCEDURE [dbo].[InsertMultiChoiceList] 
    @PatientRiskAssessmentQuestionsID nvarchar(100), 
    @NetworkRiskAssessmentQuestionsID nvarchar(100), 
    @answer varchar(max) 
AS 
BEGIN 
    DECLARE @XML AS XML 
    DECLARE @Delimiter AS CHAR(1) =',' 

    SET @XML = CAST(('<X>'+REPLACE(@answer , @Delimiter ,'</X><X>')+'</X>') AS XML) 

    DECLARE @temp TABLE (Answer Varchar(max)) 

    INSERT INTO @temp 
     SELECT N.value('.', 'Varchar(max)') AS Answer 
     FROM @XML.nodes('X') AS T(N) 

    INSERT INTO [dbo].[PatientRiskAssessmentQuestionsList](NetworkRiskAssessmentListID, PatientRiskAssessmentQuestionsID) 
     SELECT 
      [dbo].[fnc_GetNetworkRiskAssessmentList](LTRIM(RTRIM(q.Answer)), @NetworkRiskAssessmentQuestionsID, 'List') AS NetworkRiskAssessmentListID, 
      @PatientRiskAssessmentQuestionsID 
     FROM 
      (SELECT Answer FROM @temp) q 
     WHERE 
      NOT EXISTS (SELECT 1 
         FROM PatientRiskAssessmentQuestionsList x 
         WHERE x.NetworkRiskAssessmentListID = NetworkRiskAssessmentListID 
          AND x.PatientRiskAssessmentQuestionsID = @PatientRiskAssessmentQuestionsID); 
END 

下面是PatientRiskAssessmentQuestionsList表的結構

enter image description here

下面是fnc_GetNetworkRiskAssessmentList

ALTER function [dbo].[fnc_GetNetworkRiskAssessmentList] 
    (@text varchar(max), 
    @networkriskquestionid bigint, 
    @type varchar(20) 
    ) 
RETURNS BIGINT 
AS 
BEGIN 
    declare @id bigint 
    declare @questionid bigint 
    declare @count int 

    set @id = null 
    set @questionid = null 
    set @count = 0 

    if(@type = 'List') 
    begin 
     select @count = Count(*) 
     from NetworkRiskAssessmentList mc 
     where mc.Answer = @text 
      and mc.NetworkRiskAssessmentQuestionsID = @networkriskquestionid 

     if @count > 0 
     begin 
      select top(1) @questionid = mc.NetworkRiskAssessmentListID 
      from NetworkRiskAssessmentList mc 
      where mc.Answer = @text 
       and mc.NetworkRiskAssessmentQuestionsID = @networkriskquestionid 

      set @id = @questionid 
     end 
    end 

    return @questionid 
end 
+0

[PatientRiskAssessmentQuestionsList]表的結構是什麼? –

+0

檢查所有的比較陳述。他們中的任何一個人,您都在比較'BIGINT'和'NVARCHAR'? – Squirrel

+0

@Squirrel否,腳本中沒有比較陳述。即使將存儲過程參數類型更改爲bigint,也會拋出錯誤\ –

回答

0

腳本的問題是,你正在試圖推動一個字符串值插入列NetworkRiskAssessmentListID這實際上是BIGINT因此SQL不允許轉換。

只是一個示例代碼,向您展示問題

CREATE TABLE #Test 
(
Patient BIGINT, 
Network BIGINT 
) 

GO 

DECLARE @Patient NVARCHAR(100) 
DECLARE @Network NVARCHAR(100) 

SET @Patient = '1234' 
SET @Network = 'List' 

INSERT INTO #Test VALUES (@Patient,@Network) 

希望這有助於,請嘗試更改表的數據類型,並給它一試。

0

也許你的數據已經輸入的字符或製表符因此使用類似的代碼如下:

DECLARE @XML AS XML 

     DECLARE @Delimiter AS CHAR(1) =',' 

     SET @XML = CAST(('<X>'+REPLACE(@answer , @Delimiter ,'</X><X>')+'</X>') AS XML) 

     DECLARE @temp TABLE (Answer Varchar(max)) 

     INSERT INTO @temp 
     SELECT N.value('.', 'Varchar(max)') AS Answer FROM @XML.nodes('X') AS T(N) 

     insert into [dbo].[PatientRiskAssessmentQuestionsList](NetworkRiskAssessmentListID, PatientRiskAssessmentQuestionsID) 
     select 
     [dbo].[fnc_GetNetworkRiskAssessmentList](LTRIM(RTRIM(q.Answer)), @NetworkRiskAssessmentQuestionsID, 'List') as NetworkRiskAssessmentListID, 
     @PatientRiskAssessmentQuestionsID 
     from 
     (select cast(replace(replace(Answer, char(13), ''), char(10), '') as bigint) as Answer from @temp) q 
     where not exists 
     (
     select 1 from PatientRiskAssessmentQuestionsList x 
     where x.NetworkRiskAssessmentListID = NetworkRiskAssessmentListID and x.PatientRiskAssessmentQuestionsID = @PatientRiskAssessmentQuestionsID 
     ); 
0

你傳入PatientRiskAssessmentQuestionsID價值@PatientRiskAssessmentQuestionsID nvarchar(100)參數

那麼你在接下來的代碼中使用

所以問題在AND條件下,這裏

and x.PatientRiskAssessmentQuestionsID = 'PatientRiskAssessmentQuestionsID' 

x.PatientRiskAssessmentQuestionsID是bigint

和@PatientRiskAssessmentQuestionsID是nvarchar (100)

其值 [PatientRiskAssessmentQuestionsID]

所以解決這個問題之一,並且一切都會確定。

+0

我該如何做比較,因爲我無法將nvarchar轉換爲bigint類型。 –

相關問題