我試圖執行一個存儲過程,該過程輸入從查詢中選擇的3個參數。前兩個存儲過程參數應該是int
或bigint
,但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
表的結構
下面是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
[PatientRiskAssessmentQuestionsList]表的結構是什麼? –
檢查所有的比較陳述。他們中的任何一個人,您都在比較'BIGINT'和'NVARCHAR'? – Squirrel
@Squirrel否,腳本中沒有比較陳述。即使將存儲過程參數類型更改爲bigint,也會拋出錯誤\ –