?
Solved

How to call a stored procedure from a function

Posted on 2003-03-13
8
Medium Priority
?
10,053 Views
Last Modified: 2007-12-19
I have a function called check_sitrep_tree_func which calls a stored procedure called "Up" (which just displays something). When i execute the stored proc (exec Up) it works fine. But, when I call it through a function, I get the following error

Server: Msg 2812, Level 16, State 62, Procedure check_sitrep_tree_func, Line 8
Could not find stored procedure 'exec Up'. Any help would be greatly appreciated.

Thanks.

=========
ALTER Function check_sitrep_tree_func(@a varchar(20))
returns varchar(20)
as
BEGIN

     DECLARE @str varchar(2000)
     set @str = 'exec Up'  
     exec @str

return @a
END
0
Comment
Question by:jl1884
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 8129230
You need parentheses around dynamic SQL execution:

EXEC(@str)

However, you will not be able to use dynamic execution or, I think, even EXEC a SP from a function.
0
 
LVL 4

Expert Comment

by:xxg4813
ID: 8129239
So sorry to tell u the sad truth that "Only functions and extended stored procedures can be executed from within a function.".

U may rewrite it using stored procedure.


0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 8131126
try this one:

create FUNCTION usd_prod( @prodid varchar(10) )
RETURNS @prodTab TABLE

 (
   
   ProductName   nvarchar(40)
   
  )
AS
BEGIN
DECLARE @InsertString VARCHAR(1500)
Daclare @prodid int
SET @InsertString ='osql -STEST2 -E -Q"exec usp_test ' + @prodid + '" -dNorthwind'
exec master..xp_cmdshell  @InsertString
insert into @prodTab
select * from t
  RETURN
END
/*
SELECT *
FROM usd_prod ('1')
*/
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 8135250
I agree with above, but wanted only to point out a "small" difference:

DECLARE @str varchar(2000)
     set @str = 'exec Up'  
     exec (@str)

DECLARE @str varchar(2000)
     set @str = 'Up'  
     exec (@str)

DECLARE @str varchar(2000)
     set @str = 'Up'  
     exec @str

Both above will work. in fact, Exec(@sql) assumes a SQL, while exec @sql assumes a stored procedure (name)

Now, regarding your problem, you might transform your stored procedure into a function also, which you should be able to use in a function.

CHeers
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 8135431
--comment:
--remove the declaration of @prodid   in the function:

CREATE FUNCTION usd_prod( @prodid varchar(10) )
RETURNS @prodTab TABLE
            (
              ProductName   nvarchar(40)
             )
AS
BEGIN
DECLARE @InsertString VARCHAR(1500)

SET @InsertString ='osql -STEST2 -E -Q"exec usp_test ' + @prodid + '" -dNorthwind'
exec master..xp_cmdshell  @InsertString
insert into @prodTab
select * from t
 RETURN
END
/*
SELECT *
FROM usd_prod ('1')
*/
0
 

Expert Comment

by:CleanupPing
ID: 9275900
jl1884:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 12

Expert Comment

by:monosodiumg
ID: 11183221
No comment has been added to this question in more than 268 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: ScottPletcher http:#8129230, angelIII http:#8135250

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

monosodiumg
EE Cleanup Volunteer
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

770 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