Solved

Using EXEC within a Select Statemet in SQL?

Posted on 2011-02-22
2
278 Views
Last Modified: 2012-05-11
I'm using SQL 2008 RS.  Is it possible to perform a function like the code below?  Obviously the code does not work but I think it makes it clear what I'm trying to do.
Set @ChkItemCheckItemsXpath='/DailyData/Checks/Check/Seats/Seat/CheckItemRecord'


Select case when [GUEST_CHECK_NAME] = 'KJ Chardonnay' then 
		(EXEC msdb.dbo.sp_send_dbmail  @profile_name='MFMail',
		@recipients='5554545@txt.att.net',
		@subject='Void Alert',
		@body='GuestCheckName Test' )
    
		End
		
	 from OPENXML(@hDoc,@ChkItemCheckItemsXpath)  With                                    
       (   
        [GUEST_CHECK_NAME] [varchar](23) 'GuestCheckName'           
          ) XD

Open in new window

0
Comment
Question by:mattkovo
[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
2 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34956699

You can not call a stored procedure this way within a select statement.

You need to break this down into simpler statements
 Have a statement that assigns the values for the email variables and another statement to send the email
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 34957456
I suspect this is what you intended:
SET @ChkItemCheckItemsXpath = '/DailyData/Checks/Check/Seats/Seat/CheckItemRecord'


IF EXISTS ( SELECT  1
            FROM    OPENXML(@hDoc,@ChkItemCheckItemsXpath)  WITH  (
         [GUEST_CHECK_NAME] [varchar](23) 'GuestCheckName'           
          ) XD
            WHERE   [GUEST_CHECK_NAME] = 'KJ Chardonnay' ) 
    EXEC msdb.dbo.sp_send_dbmail 
			@profile_name = 'MFMail', 
			@recipients = '5554545@txt.att.net', 
			@subject = 'Void Alert', 
			@body = 'GuestCheckName Test'

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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