Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Call Stored Procedure from a Function Expand / Collapse
Author
Message
Posted Monday, December 8, 2008 9:49 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 19, 2013 11:03 AM
Points: 374, Visits: 280
Has anyone tried calling a stored procdure from a user defined function. I have a procedure as Proc_XXX with 7 parameters if i do exec Proc_XXX(null,null,null,null,null,'1/1/1998','1/1/2007') i am getting the result set. Is it possible to have the same results when calling thru a function .

call Proc_XXX(null,null,null,null,null,'1/1/1998','1/1/2007')

any help on this will be greatly appreciated. TIA



Post #615647
Posted Monday, December 8, 2008 10:05 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 4:15 AM
Points: 20,581, Visits: 9,619
Can't be done... and shouldn't be done (by design).

What exactly do you want to do?
Post #615663
Posted Monday, December 8, 2008 10:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:00 PM
Points: 7,105, Visits: 15,443
Ninja's_RGR'us (12/8/2008)
Can't be done... and shouldn't be done (by design).

What exactly do you want to do?


Can't be done in T-SQL functions. You might be able to get away with it through CLR (depending on what is being done). That being said - totally agree about the SHOULDN'T part...


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #615670
Posted Monday, December 8, 2008 10:21 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 29, 2013 1:14 PM
Points: 28, Visits: 168
ishaan99 (12/8/2008)
Has anyone tried calling a stored procdure from a user defined function. I have a procedure as Proc_XXX with 7 parameters if i do exec Proc_XXX(null,null,null,null,null,'1/1/1998','1/1/2007') i am getting the result set. Is it possible to have the same results when calling thru a function .

call Proc_XXX(null,null,null,null,null,'1/1/1998','1/1/2007')

any help on this will be greatly appreciated. TIA


Not sure how you feel about calling functions from functions but you could create this

Func_XXX(.....) Insert current proc code here making the proc a proper function
Proc_XXX(.....) Select * From Func_XXX(....)
Use Func_XXX from your other function.
Post #615933
Posted Monday, December 8, 2008 10:35 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 9:14 AM
Points: 2,112, Visits: 5,492
laubenth (12/8/2008)
ishaan99 (12/8/2008)
Has anyone tried calling a stored procdure from a user defined function. I have a procedure as Proc_XXX with 7 parameters if i do exec Proc_XXX(null,null,null,null,null,'1/1/1998','1/1/2007') i am getting the result set. Is it possible to have the same results when calling thru a function .

call Proc_XXX(null,null,null,null,null,'1/1/1998','1/1/2007')

any help on this will be greatly appreciated. TIA


Not sure how you feel about calling functions from functions but you could create this

Func_XXX(.....) Insert current proc code here making the proc a proper function
Proc_XXX(.....) Select * From Func_XXX(....)
Use Func_XXX from your other function.


If the code contains any insert/update/delete statements to permanent tables (e.g. not table variables and not temporary tables), it isn’t possible. Inside a function you can not modify data and can not execute stored procedure.

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #615936
Posted Tuesday, December 9, 2008 4:19 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 4:15 AM
Points: 20,581, Visits: 9,619
Exactly... that's because the servers needs to assume that the state of the DB and data will not change by calling the function (again, by design because the server would basically need to recompile the queries after each call to the function... which happens on every rows in the select). If you need to do such modifications, it needs to be done inside a proc.
Post #616014
Posted Tuesday, December 9, 2008 5:34 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 4:11 AM
Points: 2,397, Visits: 3,416
It CAN be done if you use OPENROWSET with a loopback linked server.
If you should is another question.



N 56°04'39.16"
E 12°55'05.25"
Post #616051
Posted Tuesday, December 9, 2008 6:01 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 4:15 AM
Points: 20,581, Visits: 9,619
... and you can open fire on the server run with a shotgun and call your service rep for a free replace of the servers too.

I wouldn't try that either!


MS designed it that way for a very specific reason, I can't recomend it enough to not do that!
Post #616063
Posted Wednesday, December 10, 2008 12:21 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 4:11 AM
Points: 2,397, Visits: 3,416
I have a function where I call "sp_who2" and take database and username as parameters to the function.
Works great.



N 56°04'39.16"
E 12°55'05.25"
Post #616708
Posted Wednesday, December 10, 2008 12:33 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 4:15 AM
Points: 20,581, Visits: 9,619
I know... untill you start using sps that do DML, then you canget seriously screwed if you're not carefull... unsupported, I find another way!
Post #616716
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse