|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, March 04, 2011 8:33 AM
Points: 8,
Visits: 27
|
|
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
Hi, you should use a stored procedure, becouse by design it is impossible modify the database context outside the function. Recently I found a workaround using external .NET function, I described well in my site. The post is in Italian, but you can find useful informations:
http://www.maurodalfreddo.it/archives/97/eseguire-stored-procedures-in-una-funzione-udf-sql-server
Mauro
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 04, 2011 7:06 AM
Points: 2,
Visits: 2
|
|
Almost loathe to answer it, but I never know what the needs are.
It is possible. Perhaps you're trying to avoid the poorly performing table variables in 2005+ and use a reporting tool that won't submit proc calls or something. Who knows.
You shouldn't call procedures through functions, and this approach REALLY isn't advised, but one method of doing it (there are several) using only sql, in an imaginary scenario, without using any table variables is:
1) Create your stored procedure, accepting whatever paramters are needed. In the stored procedure, implement a "tidyup" routine for previous result sets. Output the data to a perm table. The stored procedure then acts as a specific result set refresh mechanism. A status table could store the last refresh dates, status etc. 2) Create a table function. Use command shell (ewww) to call the stored procedure through osql, returning the current status of that result set. 3) Run a report against the perm table, to retrieve data, or include the function to refresh it.
Did it once on my dev box, just for a laugh and it does work. Don't think any of it is best practise, but on principle I had to prove it could be done ;)
And yes, depending on the complexity and size of the result set, you will most likely notice a significant improvement in speed, over table variables for derived data, plus the result set is reusable if stored, thus potentially cutting overheads.
If you focus on those points and ignore the huge gaping holes *cough security cough* and complete lack of best practise, it's great!
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 04, 2011 7:06 AM
Points: 2,
Visits: 2
|
|
I'm curious - I assume the reason you haven't done a flat conversion of the stored procedure to a function is because of data volumes and and the horrific performance issues with table variables through 2005 or something?
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, September 28, 2012 4:47 AM
Points: 1,
Visits: 10
|
|
Hi , I want to ask a question , Is it possible executing dynamic sql and store values in a temp table with WITH clause like below code?
DECLARE @sqltxt nvarchar(max) ;
SET @sqltxt='select col112,col221 from sometable';
WITH temptable (col1,col2) as (exec sp_executesql @sqltxt ) select * from temptable
Or how can I achive this task with sql? Thanks.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 12:22 AM
Points: 2,370,
Visits: 3,250
|
|
Yasemin Örnek (9/19/2012) Hi , I want to ask a question , Is it possible executing dynamic sql and store values in a temp table with WITH clause like below code?
DECLARE @sqltxt nvarchar(max) ;
SET @sqltxt='select col112,col221 from sometable';
WITH temptable (col1,col2) as (exec sp_executesql @sqltxt ) select * from temptable
Or how can I achive this task with sql? Thanks.
First of all, you shouldn't hijack other peoples' threads when you can easily start a new one of your own. Secondly, I shouldn't encourage you in doing so by answering your questions. 
But since I answered before realizing this, I'll say I'm pretty sure that your syntax will not work. Try it!
You could try do something like this:
DECLARE @sqltxt nvarchar(max) ;
SET @sqltxt='INSERT INTO temptable (col1,col2) select col112,col221 from sometable';
exec sp_executesql @sqltxt
select * from temptable
That will work with permanent or temporary tables, but not table variables (they will not be in the context of the executed dynamic SQL).
No loops! No CURSORs! No RBAR! Hoo-uh!
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
Need to UNPIVOT? Why not CROSS APPLY VALUES instead? Since random numbers are too important to be left to chance, let's generate some! Are you too recursively challenged? Splitting strings based on patterns can be fast!
|
|
|
|