Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using EXEC within a Select Statemet in SQL?

Posted on 2011-02-22
2
Medium Priority
?
282 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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

604 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