Solved

SQL Creating email subject with field values in it

Posted on 2008-06-26
13
248 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 192
Addition to SQL for dynamic fields 6 62
Format Date fields 11 60
Whats wrong in this query - Select * from tableA,tableA 11 58
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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