Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sending query to individual row using sqlmail stor

Author  Topic 

loungesoul
Starting Member

3 Posts

Posted - 2008-02-19 : 10:47:34
Good day Master Sql Jedis


I have a table that contains Accountid, customer_name, order, quantity, and email how can I send each customer data to their email using sqlmail stored procedures.The Accountid is unique.

Kind regards

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-19 : 10:51:56
When do you want email to be send? whenever some insert/update action happens?
Go to Top of Page

loungesoul
Starting Member

3 Posts

Posted - 2008-02-19 : 12:34:08
Whenever there is an insert this table is actually a view from two other tables
Go to Top of Page

talleyrand
Starting Member

35 Posts

Posted - 2008-02-19 : 12:53:19
Assumptions:
You can write the trigger yourself
You have db mail set up or can set it up

The call would then be something in the manner of

EXECUTE msdb.dbo.sp_send_dbmail 
@profile_name = 'SomeProfile'
, @recipients = 'someone@somewhere.com'
, @subject = 'Email Test'
, @body = 'Email Test'


That enough of a pointer?
Go to Top of Page

loungesoul
Starting Member

3 Posts

Posted - 2008-02-19 : 12:56:46
EXECUTE msdb.dbo.sp_send_dbmail
@profile_name = 'SomeProfile'
, @recipients = 'someone@somewhere.com'
, @subject = 'Email Test'
, @body = 'Email Test'


Thanks for the code above,I already have the email setup.The @query is not part of the code and how do i tied the Accountid with the email so as to send individual record
Go to Top of Page

talleyrand
Starting Member

35 Posts

Posted - 2008-02-19 : 19:05:29
To back up a bit, personally I'd not use the sql server mail and just hook directly to your MTA for a number of reasons.

Having said that and having a glut of time, let's start simple.

CREATE TABLE dbo.FOO
(
Accountid int, customer_name varchar(50), [order] varchar(50), quantity int, email varchar(50)
)
GO
CREATE TRIGGER dbo.FOO_update
ON dbo.FOO
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;

-- Insert statements for trigger here
SELECT I.orders, D.orders, I.email
FROM
INSERTED I
INNER JOIN
DELETED D
ON D.accountid = I.accountid
END
GO
INSERT INTO
foo
SELECT
1 as accountID
, 'bob' AS customer
, 'start' AS orders
, 50 AS qty
, 'bob@your.uncle'


At this point, we have one entry in our table and a trigger that will print the old and new state, assuming [order] is the thing that will change. Executing the following statement will result in

orders orders email
step2 start bob@your.uncle

Make the trigger fire
UPDATE
FOO
SET
orders = 'step2'
WHERE
accountID = 1

At this point, you'd expect to send an email to the value in email with an indication that their transaction is now in step2? If so, then you'll just do some TSQL string work in the trigger to make it look like

DECLARE @body varchar(255), @recipients varchar(50)
SELECT
@recipients = I.email
, @body = 'Your order is now in a status of ' + I.[order]
FROM
INSERTED I


That's all well and good, but what about this scenario?
-- create 2 new entities
INSERT INTO
foo
SELECT
2 as accountID
, 'robert' AS customer
, 'start' AS orders
, 50 AS qty
, 'robert@your.uncle'
UNION ALL
SELECT
3 as accountID
, 'alice' AS customer
, 'start' AS orders
, 50 AS qty
, 'alice@your.aunt'

-- Perform a mass update
UPDATE
foo
SET
orders = 'step 3'

That will result in
orders	orders	email
step 3 start alice@your.aunt
step 3 start robert@your.uncle
step 3 step2 bob@your.uncle


Oh crap, you just sent one email instead of 3 and didn't even know you missed the other two. So in the case of mass updates, you'll need to do cursor logic in the trigger (unless someone has an alternative) to send an email for each item. The cursor in a trigger to send email on update is just begging to degrade your system performance.

That help you see how to make it work with your data and hopefully provide a word of caution about the road you're going down?
Go to Top of Page
   

- Advertisement -