“执行存储过程时,将数据类型nvarchar转换为bigint时出错”

时间:2017-05-02 07:47:18

标签: sql-server stored-procedures

我正在尝试执行一个存储过程,该过程输入从查询中选择的3个参数。前两个存储过程参数应该是intbigint,但sql不接受它并告诉我它不能将类型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

3 个答案:

答案 0 :(得分:1)

问题是您正在尝试将字符串值推送到实际为BIGINT的列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) 中,因此SQL不允许转换。

只是一个示例代码,向您展示问题

.question-instructions {
 background-color: #3eb6a9;
 margin: 0px 0px 0px;
}

.question-line {
 margin: 0px auto;
 white-space: nowrap;
 text-align: left;
}

.question-block {
 margin: 10px 0 30px;
 align-content: center;
 vertical-align: middle;
}
.question-block h4{
  display: inline-block;
}
.block {
 float: left;
}

希望这有帮助,尝试更改表的数据类型并尝试一下。

答案 1 :(得分:1)

也许你的数据有Enter字符或Tab字符,所以使用如下代码:

   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
        );

答案 2 :(得分:1)

您正在将PatientRiskAssessmentQuestionsID值传递给@PatientRiskAssessmentQuestionsID nvarchar(100)参数

然后你在下一个代码中使用它

select 1 from PatientRiskAssessmentQuestionsList x 
        where x.NetworkRiskAssessmentListID = NetworkRiskAssessmentListID 
        and x.PatientRiskAssessmentQuestionsID = @PatientRiskAssessmentQuestionsID

所以AND条件中的问题,这里

and x.PatientRiskAssessmentQuestionsID = 'PatientRiskAssessmentQuestionsID'

x.PatientRiskAssessmentQuestionsID为bigint

和@PatientRiskAssessmentQuestionsID为nvarchar (100)

其价值 [PatientRiskAssessmentQuestionsID]

所以解决这个问题,一切都会好的。