Solved

Using EXEC within a Select Statemet in SQL?

Posted on 2011-02-22
2
275 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
2 Comments
 
LVL 32

Expert Comment

by:ewangoya
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

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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