自定义 PRINT 语句

Customizing the PRINT statement

我的代码中有 2 个 PRINT 语句,如下所示:

DECLARE @Msg VARCHAR(300)= 'Delete Email addresses that have been in the table for more than 30 days.';
PRINT @Msg;

-- If the email address has been in the table for more than 30 days, it should be removed
DELETE
--SELECT *
FROM CLNT_SVC_GOLD_SURVEY_EMAILS
WHERE DATE_ADDED < DATEADD(day, -30, GETDATE())


DECLARE @Msg1 VARCHAR(300)= 'Append data to the final table if it doesnt already exist.';
PRINT @Msg1;

-- Copy all records from the staging table where no existing email address exists in the final table
INSERT INTO CLNT_SVC_GOLD_SURVEY_EMAILS(ACCOUNT_FULL_NAME, ACCOUNT_NUMBER, CUSTOMER_FULL_NAME, CONTACT_EMAIL, DATE_ADDED)
SELECT 
A.ACCOUNT_FULL_NAME,
A.ACCOUNT_NUMBER,
A.CUSTOMER_FULL_NAME,
A.CONTACT_EMAIL,
CONVERT(DATE, GETDATE()) as DATE_ADDED
FROM CLNT_SVC_GOLD_SURVEY A
LEFT JOIN CLNT_SVC_GOLD_SURVEY_EMAILS B
  ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER
  AND A.CONTACT_EMAIL = B.CONTACT_EMAIL
WHERE B.DATE_ADDED IS NULL
  AND A.ACCOUNT_FULL_NAME NOT LIKE '%UNKNOWN%'

输出如下所示:

Delete Email addresses that have been in the table for more than 30 days.

(0 row(s) affected)
Append data to the final table if it doesnt already exist.

(0 row(s) affected)

我有几个问题。第一,有没有什么办法可以将记录数整合到自定义消息中,并从自动消息中去除?即:

Delete Email addresses that have been in the table for more than 30 days. This step affected 0 row(s).

Append data to the final table if it doesnt already exist. This step affected 0 row(s).

其次,如何获得打印撇号的自定义打印消息?我不得不把它从“doesnt”这个词中删除,因为我无法让它工作。

您需要将 NOCOUNT 转为 ON,这意味着您不会收到 (0 row(s) affected) 消息,然后 PRINT 之后 [=19] =] 语句完成并将 @@ROWCOUNT 的值合并到打印的消息中:

SET NOCOUNT ON;

DELETE
FROM dbo.CLNT_SVC_GOLD_SURVEY_EMAILS
WHERE DATE_ADDED < DATEADD(day, -30, GETDATE());

PRINT CONCAT(N'Delete Email addresses that have been in the table for more than 30 days. This step affected ', @@ROWCOUNT,N' row(s).');