Solved

SQL Creating email subject with field values in it

Posted on 2008-06-26
13
238 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
  • 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
 
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now