1

I've got 2 PRINT statements in my code that look like this:

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%'

The output looks like this:

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)

I've got a couple questions. First, is there any way to integrate the number of records into the custom message and remove them from the automated message? i.e.:

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

Second, how do you get a custom print message to print an apostrophe? I had to remove it from the word "doesnt" because I couldn't get it to work.

3
  • Does @@ROWCOUNT already know, or do I need to define it? I apologize, I used to be closer to an Intermediate user but I haven't touched it in about a year. Commented Jun 1, 2022 at 15:07
  • 1
    When wanting to include a single quote in a string literal you have to escape it with another single quote. So you string would be something like 'it doesn''t already' Commented Jun 1, 2022 at 15:08
  • 1
    @SeanLange - Thanks! That was easier than expected. :-) Commented Jun 1, 2022 at 15:21

1 Answer 1

3

You need to turn NOCOUNT to ON, meaning you don't get the (0 row(s) affected) messages and then PRINT after the statement completed and incorporate the value of @@ROWCOUNT into printed message:

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).');
Sign up to request clarification or add additional context in comments.

3 Comments

"after the statement completed" in this case being immediately after, otherwise @@ROWCOUNT will not be correct
Had to actually set NOCOUNT to ON, but that was just a quick google to fix it.
Yep, sorry, brain was in reverse there, @JohnnyBones , it's already in holiday mode as it's a 4 day weekend here. Fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.