有一个创建Excel文件的过程,选择结果保存在其中。该文件附在信件上并通过邮件发送。但是问题在于该文件已创建并发送为空。手动操作一切正常,但不能同时使用。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_sendMail]
@SID INT,
@EMAIL VARCHAR(512),
@CUR INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@MAILID INT,
@TABLEHTML VARCHAR(MAX),
@CMD SYSNAME,
@FILENAME NVARCHAR(MAX),
@SQL NVARCHAR(MAX),
@PID INT,
@DATE_START DATE,
@DATE_END DATE
SELECT
@PID = PATIENTS_ID,
@DATE_START = DATE_START,
@DATE_END = DATE_END
FROM
TABLE
WHERE
ID = @SID
EXEC SP_EXECUTESQL N'sp_configure ''show advanced options'', 1; reconfigure; '
EXEC ('RECONFIGURE')
EXEC SP_EXECUTESQL N'sp_configure ''xp_cmdshell'', 1; reconfigure;'
EXEC ('RECONFIGURE')
SET @FILENAME = 'D:\' + CAST(@SID AS VARCHAR) + '_' + REPLACE(CONVERT(VARCHAR, GETDATE(), 104), '.', '') + '_' + CAST(@CUR AS VARCHAR) + '.xlsx'
SET @CMD = 'copy D:\Example.xlsx ' + @FILENAME
EXEC MASTER..XP_CMDSHELL @CMD;
SET @SQL = 'INSERT INTO OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0;Database=' + @FILENAME + ';HDR=YES'',
''SELECT DATE, CODE, LABEL, CNT, CU, SCO, DISCOUNT FROM [Sheet1$]'')
SELECT
CONVERT(VARCHAR, ORDER.DATE, 104) DATE,
EMPLOYEE.CODE CODE,
EMPLOYEE.LABEL LABEL,
CAST(ORDER.CNT AS INT) CNT,
CAST(ORDER.PRICE AS MONEY) CU,
CAST(ORDER.PRICE_TO_PAY AS MONEY) SCO,
CAST(ORDER.DISCOUNT AS INT) DISCOUNT
FROM
ORDER
JOIN
EMPLOYEE ON EMPLOYEE._ID = ORDER._ID
WHERE
(CONVERT(DATE, ORDER.DATE) >= CONVERT(DATE, @DATE_START)
AND CONVERT(DATE, ORDER.DATE) <= CONVERT(DATE, @DATE_END))
AND (ORDER.ID in (@PID))
ORDER BY ORDER.DATE'
EXEC SP_EXECUTESQL
@SQL,
N'@DATE_START DATE, @DATE_END DATE, @PATID INT',
@DATE_START = @DATE_START,
@DATE_END = @DATE_END,
@PID = @PID
SET @TABLEHTML =
N'<H1>Hello!</H1>' +
N'<span>num: </span>' + CAST(@SID AS VARCHAR(MAX)) +
N'<span>, email: </span>' + CAST(@EMAIL AS VARCHAR(MAX)) +
N'<span> sent! </span>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'site.com',
@recipients = 'admin@site.com',
@subject = 'subj',
@body = @tableHTML,
@body_format = 'HTML',
@file_attachments = @FILENAME,
@mailitem_id = @mailid output
IF (@MAILID > 0 AND @@ERROR = 0)
UPDATE TABLE SET [SIGN] = 1, [SENT] = GETDATE(), SENDER = @CUR WHERE ID = @SID
EXEC SP_EXECUTESQL N'sp_configure ''show advanced options'', 1; reconfigure; '
EXEC ('RECONFIGURE')
EXEC SP_EXECUTESQL N'sp_configure ''xp_cmdshell'', 0; reconfigure;'
EXEC ('RECONFIGURE')
END
答案 0 :(得分:0)
是的, JGFMK 是正确的! 如果执行此代码,则将执行openrowset:
;With Numbered as (
select *,ROW_NUMBER() OVER (
PARTITION BY CUST_ID
ORDER BY LAST_ACTIVITY_DATE desc) rn
from Account
)
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,LAST_ACTIVITY_DATE
from Numbered
where rn=1
如何使其根据条件起作用?