Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Conversion failed when converting the nvarchar value to data type int when returning nvarchar(max) from stored procedure

$
0
0

Thank you for your help!!! 

I've created a stored procedure to return results as xml.  I'm having trouble figuring out why I'm getting the error message "Conversion failed when converting the nvarchar value '<tr>.....' to data type int.    It seems like the system doesn't know that I'm returning a string... Or, I'm doing something that I just don't see.

ALTER PROCEDURE [dbo].[p_getTestResults]
--*****************************************************************
	@xml NVARCHAR(MAX) OUTPUT
AS
BEGIN

	CREATE TABLE #Temp 
	( 
	  [TestNameId]  int,
	  [MaxTestDate]  [DateTime],
	  [Name] [nvarchar](50),
	  [Duration]  [varchar](10)
	)


	DECLARE @body NVARCHAR(MAX)

	;WITH T1 AS
	(
	SELECT DISTINCT
		Test.TestNameId
		, replace(str(Test.TotalTime/3600,len(ltrim(Test.TotalTime/3600))+abs(sign(Test.TotalTime/359999)-1)) + ':' + str((Test.TotalTime/60)%60,2)+':' + str(Test.TotalTime%60,2),' ','0') as Duration
		,MaxTestDate = MAX(TestDate) OVER (PARTITION BY TM.TestNameId)
		,TestDate
		,TM.Name
		,Test.TotalTime
		,RowId = ROW_NUMBER() OVER
			(
			PARTITION BY
				TM.TestNameId
			ORDER BY
				TestDate DESC
			)
	FROM
		Test
		inner join TestName TM on Test.TestNameID = TM.TestNameID
		where not Test.TestNameID in (24,25,26,27)
	)
	INSERT INTO #Temp
	SELECT
		T1.TestNameId
		,T1.MaxTestDate
		,T1.[Name]
		,T1.Duration
	FROM
		T1
	WHERE
		T1.RowId = 1
	ORDER BY
		T1.TestNameId 

	
	SET @body ='<html><body><H3>TEST RESULTS INFO</H3><table border = 1> <tr><th> TestNameId </th> <th> MaxTestDate </th> <th> Name </th> <th> Duration </th></tr>'   

	SET @xml = CAST(( 
			   SELECT CAST(TestNameId AS NVARCHAR(4)) as 'td'
			 , ''
			 , CAST([MaxTestDate] AS NVARCHAR(11)) AS 'td'
			 , ''
			 , [Name] AS 'td'
			 , ''
			 , CAST([Duration] AS NVARCHAR(10)) AS 'td'
			   FROM  #Temp 
			   ORDER BY TestNameId
			   FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

	SET @body = @body + @xml +'</table></body></html>'

	DROP TABLE #Temp
	
  RETURN @xml
END



closl


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>