Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


exec stored proc inside a function


exec stored proc inside a function

Author
Message
londoner-737512
londoner-737512
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 61
Aim:
a procedure returns 30 column results.
i only want 3 of them and dont want to create a temp table with a 2nd select on it. (insert into #T exec myproc)
plan was to create a function that executes this proc and selects the columns i want.
in reference to the msdn site i found that it is possible but i do get an error on compilation.
"Incorrect syntax near the keyword 'EXECUTE'."


==================================================
http://msdn.microsoft.com/en-us/library/ms186755.aspx

-The following statements are valid in a function:
........
EXECUTE statements calling extended stored procedures.

==================================================

CREATE FUNCTION dbo.f_GetAnalysis
(
@group varchar(25),
@mode varchar(12)
)
RETURNS table
AS
RETURN
EXEC dbo.usp_GetAnalysis @mode , @group
go


Any ideas appreciated.
Gianluca Sartori
Gianluca Sartori
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6484 Visits: 13322
If you just want to query the stored procedure results, leave the function alone, way too complicated.

You could set up a loopback linked server (ilnked server pointing to the server itself) and the run something like:


SELECT theColumnsYouNeed
FROM OPENQUERY(LOOPBACK, 'EXEC dbo.usp_GetAnalysis @mode = ''mode'' , @group = ''group'' ')



As I said, don't try to put it in the function, because it would simply ignore any data modification instruction contained in the stored procedure, WITHOUT raising any error. It's surprising, but it's so. Functions are not allowed to modify data.

Hope this helps.
Gianluca

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
londoner-737512
londoner-737512
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 61
thanks - never knew about this technique.

i will have to take your word for it as i need to get the DBA's to set this up for me.

i'll will post result once tested.

:-)
Nabha
Nabha
SSChasing Mays
SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)SSChasing Mays (617 reputation)

Group: General Forum Members
Points: 617 Visits: 1814
AS per BOL,
CREATE FUNCTION [ owner_name. ] function_name
( [ { @parameter_name [AS] scalar_parameter_data_type [ = default ] } [ ,...n ] ] )

RETURNS @return_variable TABLE < table_type_definition >

[ WITH < function_option > [ [,] ...n ] ]

[ AS ]

BEGIN
function_body
RETURN
END



In multi-statement table-valued functions, function_body is a series of Transact-SQL statements that populate a table return variable.


The following statements are allowed in the body of a multi-statement function.


May be after RETURN statement that part is not considered as function body.

I think you may have to create a temp table/table variable and fill the data and then return it.

---------------------------------------------------------------------------------
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search