Solved

SQL Creating email subject with field values in it

Posted on 2008-06-26
13
236 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

18 Experts available now in Live!

Get 1:1 Help Now