Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Is this possible?

Author  Topic 

jholovacs
Posting Yak Master

163 Posts

Posted - 2009-05-14 : 11:05:06
I know I can insert data into a table by using INSERT...EXEC

Is it possible to do a SELECT of one returned column from and EXEC call, something like:
SELECT j.current_execution_status FROM EXEC('msdb..sp_help_job @job_name = N''Expired subscription clean up'', @job_aspect = ''JOB''') j

Clearly this does not work, but I can't help but think a tweak of it would.



SELECT TOP 1
w.[name]
FROM
dbo.women w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-14 : 11:13:19
yup. its possible, see below

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx
Go to Top of Page

jholovacs
Posting Yak Master

163 Posts

Posted - 2009-05-14 : 11:21:28
eh... this pretty much say it's not possible, and recommends an openrowset or temp table.



SELECT TOP 1
w.[name]
FROM
dbo.women w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-14 : 11:39:04
yup. otherwise you've make it a udf rather than as a procedure.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-05-14 : 11:39:23
Could you make the proc a table-valued function instead?

Jim
Go to Top of Page

jholovacs
Posting Yak Master

163 Posts

Posted - 2009-05-14 : 13:04:32
bleh. Yeah, I could, but that's more work than just inserting into a table and selecting from that table. I was hoping for something a bit more... graceful. Creating a new function means I have more to clean up afterwords.



SELECT TOP 1
w.[name]
FROM
dbo.women w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-14 : 14:13:15
cleanup? didnt understand that
Go to Top of Page

jholovacs
Posting Yak Master

163 Posts

Posted - 2009-05-20 : 12:45:32
Using INSERT INTO...EXEC requires a table to be defined beforehand, and as a general rule I like to clean up after myself in a proc or function, especially if once I've gathered what I'm looking for I will no longer need it.



SELECT TOP 1
w.[name]
FROM
dbo.women w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC
Go to Top of Page
   

- Advertisement -