Send an email from SQL using the emails that result from a query - sql-server

I have a table that contains the order status and email address , If I do a select * statement this is my output
Name. Email Adress Approval code Order states. Qty tickets.
Juan Rivera. Juan #anyemail.com. Null. 1 50
Maria Rosales Maria #anyemail.com. 5567657. 1. 25 Jose Almonte. Jose#anyemail.com. Null. 1. 10
What I would like to do is run a stored procedure everynight and send an email to every user that has a null in the approval code, for example :
Dear : Jose ,
We noticed that your order does not contain an approval code , this order is scheduled to be canceled unless an approval code is entered in the next 72 hours.
Thanks you
Is this possible ?

Please refer SQL SERVER – 2008 – Configure Database Mail – Send Email From SQL Database

Related

Email sql query results of multiple email addresses daily

I am fairly green at SQL and have reached a road block. I have a Job that already runs a query and sends an email to our purchasing department agents via the agent.
Here is my sample data they receive as a text attachment:
po_num vend_num qty_needed external_email_addr
318 1 200 email#earthlink.net
318 1 910 email#earthlink.net
703 2 250 email#row.com
993 3 3600 email#cast.com
993 3 3600 email#cast.com
676 4 1 NULL
884 5 10000 email#Futures.com
118 5 2500 email#Futures.com
My goal is to automatically send each vendor one email of the qty_needed using the email address in external_email_addr field. Also, if the email address is NULL it would send me an email that this needs to be fixed.
I am not sure how complicated or simple this is but any help would be greatly appreciated.
Since the po_num is unique you will generate several mails per email address per day based on the example data you provided.
I dont have access to SQL at the moment so the syntax might need some sprucing up.
SELECT po_num,
vend_num,
qty_needed,
CASE WHEN external_email_addr ='' THEN COALESCE(external_email_addr,'defaultempty#fixthisproblem.com') ELSE external_email_ddr END AS email_address
FROM table_name

sp_send_dbmail - Multiple Rows as Body

I've got a table that is set up similar to the following:
personID|Last_Name|First_Name|feeDescription|feebalance|client1_email|client2_email
1|Test|Joe|2017 Fees|90.00|joe#test.com|joe2#test.com
1|Test|Joe|2017 Parking|40.00|joe#test.com|joe2#test.com
2|Sample|Nellie|2018 Membership|120.00|whoanellie#test.com|Null
What I would like to do is create a sql job that runs once a week and emails users with balance. I'd like to include a sent mail column so they only get the email once but would get a new email if a new fee was added. It would sent to client1_email if not null and cc to client2_email if not null.
The body would look similar to:
Dear Joe Test:
You have the following outstanding account fees with us:
2017 Fees $90.00
2017 Parking $40.00
And proceed to process the rest of the table:
Dear Nellie Sample:
You have the following outstanding account fees with us:
2018 Membership $120.00
Some personID's may have 1 fee, some 10. Is there a way to accomplish this?
If you want to simply embed a CTRL+LF(carriage return) in your body then simply use the ascii codes.
CHAR(13)+CHAR(10)
Here is a similar question.
Try it's
SELECT 'Dear ' + Last_Name + ' ' + First_Name + '
You have the following outstanding account fees with us:
' + feeDescription +' ' + feebalance
from table

how to avoid re-inserting data into sql table while re-running SSIS package that loads data from flat file to SQL table?

I have a flat file with the following data
Id,FirstName,LastName,Address,PhoneNumber
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
4,nash,gordon,charlotte NC ADDRESS,111200110
I have created a SSIS package that has flat file source , and an aggregate function that makes sure that only unique rows are inserted and not duplicate records from the flat file , and SQL table as my destination.
Everything is fine when i run the package , i am getting below output in SQL table
Id FName LName Address phoneNumber
1 ben afflick xyz Address 5014123746
4 nash gordon charlotte NC ADDRESS 111200110
3 christina smith test address 111000110
But when i add some new data to the flat file as below
Id,FirstName,LastName,Address,PhoneNumber
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
1,ben,afflick,xyz Address,5014123746
3,christina,smith,test address,111000110
4,nash,gordon,charlotte NC ADDRESS,111200110
5,abc,xyz,New York,9999988888
and re-run the package the data that is already present in the table is getting re-inserted as below
1 ben afflick xyz Address 5014123746
4 nash gordon charlotte NC ADDRESS 111200110
3 christina smith test address 111000110
1 ben afflick xyz Address 5014123746
5 abc xyz New York 9999988888
4 nash gordon charlotte NC ADDRESS 111200110
3 christina smith test address 111000110
But i DO NOT want this , i don't want data to be inserted that is already present.
I want only the newly added data to get inserted into the SQL table.
Can someone please help me to achieve this?
Another method is to load your file into a staging table in the database and then use a merge statement to insert data into your destination table.
In practice this would look like a data flow from your flat file to your staging table and then a execute sql task containing a merge statement.
You can then update any matching values as well, should you wish to.
merge into table_a
using stage_a
on stage_a.key = table_a.key
when not matched then insert (a,b,c,d) values ( a,b,c,d )
Your data flow task would look something like this. Here, the Flat file source reads the CSV file and then passes the data to Lookup transformation. This transformation will check for existing data in the destination table. If there are no matching records, then the data from CSV file will be sent to the OLE DB Destination otherwise, the data will be discarded only.
lookup Transformation link ---
http://www.codeproject.com/Tips/574437/Term-Lookup-Transformation-in-SSIS

meaning of the values of sent_status on msdb.dbo.sysmail_mailitems

I am sending emails from SQL Server, and need to map the values of the sent_status column on the msdb.dbo.sysmail_mailitems table to something more descriptive.
So far I have identified two values:
1 = 'Sent'
2 = 'Failed'
Are there any more possible values, and if so what do they represent?
sent_status, --0 new, not sent, 1 sent, 2 failure or 3 retry.
On the MSDN page for the related msdb.dbo.sysmail_allitems table, the description for sent_status says:
The status of the mail. Possible values are:
sent - The mail was sent.
unsent - Database mail is still attempting to send the message.
retrying - Database Mail failed to send the message but is
attempting to send it again.
failed - Database mail was unable to send the message.
Connecting the two views together as follows:
SELECT DISTINCT mi.sent_status, ai.sent_status
FROM
msdb.dbo.sysmail_allitems ai
FULL OUTER JOIN
msdb.dbo.sysmail_mailitems mi ON
ai.mailitem_id = mi.mailitem_id
Will yield a relationship, which can be expressed with the following CASE statement:
SELECT
CASE sent_status
WHEN 0 THEN 'Unsent'
WHEN 1 THEN 'Sent'
WHEN 2 THEN 'Failed'
WHEN 3 THEN 'Retrying'
END AS sent_status_desc
FROM msdb..sysmail_mailitems

why does "SELECT 1 from <table>" cause a LCK_M_IX on another process doing a DELETE

I have a table listing patient_clinic_visits. I have SQLSERVER 2005 backend. Access2010 frontend.
Each morning this table needed refreshing from a data dump from the hospital mainframe.
this data dump includes information on the day before (who attended, didn't, cancelled) and forward appointments for next 6 weeks.
I therefore do a "DELETE * from Patient_clinic_visits where Visit_Date > (a day) and < (a day + 1) to clean out the table for each day in turn before uploading the new data after some processing.
On most days there will be only about 100-150 records in each day. Their is one foreign key to Pat_ID in Master_Patient_Table which has NO_ACTION chain on it.
At the moment this Delete query is timing-out in the Access frontend after processing a couple of days of data (ie it works okay for Monday, Tuesday, Wednesday...)
I run sp_whoisactive and get:
00 01:53:01.926 52 [[query SELECT 1 FROM "dbo"."Patient_Clinic_Visits" ]] RAHCC_User (265ms)ASYNC_NETWORK_IO 0 0 0 NULL 51 0 0 2 suspended 0 NULL SAH0020663 RAHCC_DB Microsoft MDB RAHCC 2013-04-02 09:08:33.027 0 2013-04-02 11:01:35.033
00 00:00:27.610 53 [[query --
DELETE from Patient_Clinic_Visits WHERE clinic_date >= '26-Mar-2013' AND clinic_date < '27-Mar-2013' AND Clinic_location = 'MONC' ]] HAD\jhogan05 (27596ms)LCK_M_IX 16 0 0 52 1,074 0 0 130 suspended 2 NULL SAH0048645 RAHCC_DB Microsoft SQL Server Management Studio Express - Query 2013-04-02 11:01:07.343 0 2013-04-02 11:01:35.033
This indicates my client front end is waiting on a "SELECT 1 from Patient_Clinic_Vists" and this is blocking the procedure. Apparently this is due to an ASYNC_NETWORK_IO on the client (which can happen with Access front ends doing a table request and then not processing the data).
a) However a "SELECT 1 from Patient_Clinic_Visits" should really only return TRUE or FALSE ?? which is unlikely to fill up anything, and unclear why it would cause a block situation??
b) I can't find "SELECT 1..." in my Access frontend anywhere.. Is this perhaps part of a sequence of sub-selects made by SQLSERVER in response to a more complext select? If so how do I find the true select causing this situation in that process history?
cheers,
JonHD
a) The query "SELECT 1 from Patient_Clinic_Visits" will return either an empty result set (if the Patient_Clinic_Visits table is empty) or a result set with as many rows as the Patient_Clinic_Visits table, each row having a 1 in it. To do this, SQL Server will have to issue a query against the whole Patient_Clinic_Visits table, which (assuming default locking behavior) will cause shared (read) locks to be issues against the rows in that table.
b) (NOTE: the OP addressed this point in the comments) I might be missing something, but I don't see where you come up with the "SELECT 1 from Patient_Clinic_Vists" query based on sp_whosactive. The best way to understand the SQL being sent from your application to the database server might be to use SQL Profiler with an appropriate filter (perhaps filtering only for connections from the host running your application).

Resources