存储过程不返回数据非预期格式

Stored procedure not returning data inexpected format

我有一个存储过程完全符合我的要求,但演示文稿不是我所期望的,也不是我想要的。这是代码:

DECLARE @debitos TABLE (col1 varchar(100), col2 varchar(100), col3 varchar(100), col4 varchar(100), col5 varchar(100), col6 varchar(100));

 --Insert the debitos that met the criteria

INSERT INTO @debitos
select no, nome, nrdoc, (CONCAT(CONVERT (money, edeb), ' €')), (FORMAT (dataven, 'dd-MM-yy')), (DATEDIFF(d,dataven,getdate())) from cc
where ultdoc not like '%recibo%' and cmdesc like '%N/Factura%' and (DATEADD(day, 0, GETDATE()) - dataven > 30) and deb > 0 and datalc > DATEADD(year,-1,GETDATE())
order by dataven asc

 --Create a row check to determine whether to send the email or not

DECLARE @rows int;
SET @rows = (SELECT COUNT(*) FROM @debitos)

 --Set the body elements

DECLARE @message varchar(1000);
-- declare the xml data to pass to the HTML body
DECLARE @xml NVARCHAR(MAX);
-- body will hold the HTML formatted table in the email
DECLARE @body NVARCHAR(MAX);

 --Create the columns that will hold each row of data as xml

SET @xml = CAST(( SELECT col1 AS 'td','',col2 AS 'td','', col3 AS 'td','', col4 AS 'td', col5 AS 'td', col6 AS 'td'
FROM @debitos
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

 --Set the HTML for the body

SET @body ='<html><body><H3> * FACTURAS VENCIDAS HÁ MAIS DE 30 DIAS * </H3>
<table border = 1> 
<tr>
<th> N. Cliente </th> <th> Cliente </th> <th> N. Factura </th> <th> Valor </th> <th> Data de Venc. </th> <th> Dias Venc. </th> </tr>'

 --Stitch everything together, appending the HTML table

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

SET NOCOUNT ON 

 --Check if any debitos met the criteria

IF @rows > 0 
BEGIN

 --Send the email and append the data table to the body

EXEC dbo.uspSendEmail 'Débitos de Clientes', 'mail@carro.ts', @body, NULL, 'mail@carro.ts','mail@carro.ts'
SET NOCOUNT OFF

我有一个工作安排结果的交付,但最后两列值出现在第 4 列内,数据全部被打乱,像这样:

686.43 €02-10-20109

我做错了什么,有人可以帮忙吗?

完全是在摸黑,但我注意到您在 FOR XML PATH 部分的某些列之后没有 ,'',这意味着节点将合并为一个。使用换行符和白色 space 确实可以帮助您发现或多或少的印刷错误(因为它们是简单的疏忽)。你拥有的是:

SET @xml = CAST(( SELECT col1 AS 'td','',col2 AS 'td','', col3 AS 'td','', col4 AS 'td', col5 AS 'td', col6 AS 'td'
FROM @debitos
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

但它应该是以下内容(为便于阅读而添加了格式):

SET @xml = CAST((SELECT col1 AS td, '',
                        col2 AS td, '',
                        col3 AS td, '',
                        col4 AS td, '',
                        col5 AS td, '',
                        col6 AS td, ''
                 FROM @debitos
                 FOR XML PATH('tr'), ELEMENTS) AS nvarchar(MAX));