Solved

SQL Creating email subject with field values in it

Posted on 2008-06-26
13
245 Views
Last Modified: 2012-06-27
I want to make the subject contain the @OrderNo and @Product,

e.g. Email Alert for Order No: @OrderNo , Product:@Product ,

i.e. Email Alert for Order No: 911283 , Product: GE101

what i was thinking was to declare @SubjectText  and somehow create the text that way and then the @subject parameter as

@subject = @SubjectTest


DECLARE @Cost FLOAT
DECLARE @sql VARCHAR(1000)
DECLARE @Product CHAR(20)
DECLARE @OrderNo CHAR(10)
DECLARE @OrderLineNo CHAR(5)
 
SELECT @OrderNo = [order_no]from inserted with (NOLOCK)
SELECT @OrderLineNo = [order_line_no]from inserted with (NOLOCK)
SELECT @Product = [product] from scheme.opdetm with (NOLOCK)where [order_no]=@OrderNo and [order_line_no]=@OrderLineNo
SELECT @Cost = [cost]from inserted with (NOLOCK)
SELECT @sql = 
'SELECT ' 
+ cast(i.order_no as varchar(100)) + ' OrderNo ,' 
+ cast(i.order_line_no as varchar(100)) + ' OrderLineNo ,'
+ '''' + replace(d.product , '''', '''''') + ''' product '
from inserted i with (NOLOCK)
JOIN scheme.opdetm d with (NOLOCK)
ON i.order_no = d.order_no
AND i.order_line_no = d.order_line_no
 
IF @Cost=0 and @Product <> 'CAT001' and @Product <> 'CAT002'
 
BEGIN 
 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name = '1',
@recipients = 'eoin.conway@meteorelectrical.com',
@subject = 'subject',
@body = 'body',
@query = @sql,
@execute_query_database = 'demo'
 
END

Open in new window

0
Comment
Question by:meteorelec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 13

Expert Comment

by:rickchild
ID: 21876476
DECLARE @Subject varchar(1000)

SET @SubjectTemp = 'Email Alert for Order No: ' +@OrderNo+ ' , Product: ' +@Product


EXEC msdb.dbo.sp_send_dbmail
@profile_name = '1',
@recipients = 'eoin.conway@meteorelectrical.com',
@subject = @SubjectTemp,
@body = 'body',
@query = @sql,
@execute_query_database = 'demo'
0
 
LVL 13

Expert Comment

by:rickchild
ID: 21876482
Sorry:

DECLARE @SubjectTemp varchar(1000)

SET @SubjectTemp = 'Email Alert for Order No: ' +@OrderNo+ ' , Product: ' +@Product


EXEC msdb.dbo.sp_send_dbmail
@profile_name = '1',
@recipients = 'eoin.conway@meteorelectrical.com',
@subject = @SubjectTemp,
@body = 'body',
@query = @sql,
@execute_query_database = 'demo'
0
 
LVL 13

Accepted Solution

by:
rickchild earned 500 total points
ID: 21876546
So to put that in your code
DECLARE @Cost FLOAT
DECLARE @sql VARCHAR(1000)
DECLARE @Product CHAR(20)
DECLARE @OrderNo CHAR(10)
DECLARE @OrderLineNo CHAR(5)
DECLARE @SubjectTemp varchar(1000)
 
SELECT @OrderNo = [order_no]from inserted with (NOLOCK)
SELECT @OrderLineNo = [order_line_no]from inserted with (NOLOCK)
SELECT @Product = [product] from scheme.opdetm with (NOLOCK)where [order_no]=@OrderNo and [order_line_no]=@OrderLineNo
SELECT @Cost = [cost]from inserted with (NOLOCK)
SELECT @sql = 
'SELECT ' 
+ cast(i.order_no as varchar(100)) + ' OrderNo ,' 
+ cast(i.order_line_no as varchar(100)) + ' OrderLineNo ,'
+ '''' + replace(d.product , '''', '''''') + ''' product '
from inserted i with (NOLOCK)
JOIN scheme.opdetm d with (NOLOCK)
ON i.order_no = d.order_no
AND i.order_line_no = d.order_line_no
 
SET @SubjectTemp = 'Email Alert for Order No: ' +@OrderNo+ ' , Product: ' +@Product
 
IF @Cost=0 and @Product <> 'CAT001' and @Product <> 'CAT002'
 
BEGIN 
 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name = '1',
@recipients = 'eoin.conway@meteorelectrical.com',
@subject = @SubjectTemp,
@body = 'body',
@query = @sql,
@execute_query_database = 'demo'
 
END

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:chapmandew
ID: 21876554
You do realize that if there are >1 rows inserted, then your trigger is only going to capture the last value into your variables, right?
0
 
LVL 2

Author Comment

by:meteorelec
ID: 21876890
chapmandew,
Yes thats true how do i get around that?

Also
SET @SubjectTemp = 'Email Alert for Order No: ' +@OrderNo+ ' , Product: ' +@Product

it works but the problem is, is that i'm gettinglots of spacing after the order no

i.e.
Email Alert for Order No: 999993     , Product: CAT001              


How would i trim that spacing away?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21876923
to trim:

lrtrim(rtrim(fieldname))

to get around the other values, you'll have to use a cursor:

http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=2739031&SiteID=17
0
 
LVL 2

Author Comment

by:meteorelec
ID: 21877073
I tried this but it wasn't working

SELECT @SubjectTemp = 'Email Alert for Order No: ' +LTRIM(RTRIM(@OrderNo)+ ' , Product: ' +@Product
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21877091
I would focus on getting the cursor right....
0
 
LVL 2

Author Comment

by:meteorelec
ID: 21877229
No i won't require a cursor for this particlar piece of code, as there will only be single inserts.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21877244
OK.

change this

DECLARE @OrderNo CHAR(10)

to this

DECLARE @OrderNo VARCHAR(10)

and try it again.
0
 
LVL 2

Author Comment

by:meteorelec
ID: 21877293
Thanks a million,
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21877307
welcome.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21877351
whoa...no points for the help?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question