Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion from data type xml to nvarchar is not allowed. Use the CONVERT function to run this query

I'm writing following SQL procedure:

ALTER PROCEDURE [dbo].[spc_InsertSubjectToContentRelation]
(
    @pCourseGUID XML, 
    @pSubjectId XML,
    @pAssessmentIds XML,
    @pVideoIds XML
)
AS
BEGIN
SET NOCOUNT ON;

    DECLARE @CourseGUID NVARCHAR(50);
    DECLARE @SubjectId NVARCHAR(50);
    DECLARE @AssessmentIds NVARCHAR(MAX);
    DECLARE @VideoIds NVARCHAR(MAX);

    SET @CourseGUID = Convert(NVARCHAR,@pCourseGUID);
    SET @SubjectId = Convert(NVARCHAR,@pSubjectId);
    SET @AssessmentIds = Convert(NVARCHAR,@pAssessmentIds);
    SET @VideoIds = Convert(NVARCHAR,@pVideoIds);

    INSERT INTO SubjectToAssessmentAndVideoRelation VALUES (@pCourseGUID, @pAssessmentIds, @pAssessmentIds, @pVideoIds)
END

When I'm running this procedure I'm getting the error:

Implicit conversion from data type xml to nvarchar is not allowed. Use the CONVERT function to run this query. How can I resolve this?

like image 773
Jainendra Avatar asked Nov 26 '25 05:11

Jainendra


1 Answers

You run the conversions and then ignore the converted values and continue to use the original parameter values. You might have wanted this:

INSERT INTO SubjectToAssessmentAndVideoRelation VALUES (
     @CourseGUID, @AssessmentIds, @AssessmentIds, @VideoIds)

(No p after the @, and thus using the local variables rather than the parameters)

But I continue to be mystified why you declare your procedure to want xml parameters if it's going to immediately convert them to nvarchars.

like image 105
Damien_The_Unbeliever Avatar answered Nov 27 '25 19:11

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!