【问题标题】:Stored procedure not returning data inexpected format存储过程不返回数据意外格式
【发布时间】:2021-01-19 11:17:10
【问题描述】:

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

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

我做错了什么,谁能帮忙?

【问题讨论】:

  • Ww 无法运行您的 SQL,因此我们无法复制该问题。花点时间给我们minimal reproducible example 以及您的预期结果。
  • 不过,猜测是因为您“忘记”在 col4col5col6 列之后放置 ,''

标签: html sql sql-server stored-procedures


【解决方案1】:

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

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

【讨论】:

  • 你们是我的守护天使,非常感谢你们,是省略号了。谢谢朋友!
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 2018-08-20
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多