?
Solved

Call a SPROC from within a Select Query

Posted on 2014-09-04
2
Medium Priority
?
193 Views
Last Modified: 2014-09-04
I have a select statement that I need to call a SPROC and return a value as one of the values

I have a SPROC called DB_ext.dbo.uspCalcAvgLeadTime that I need to pass a product Item Number (iv2.itemnmbr)

Below is the statement I'm trying to use, what am I doing wrong?

Thanks as always

select iv2.itemnmbr
      ,iv1.itemdesc
      ,iv2.ORDRPNTQTY
      , exec DB_ext.dbo.uspCalcAvgLeadTime(iv2.itemnmbr) as CalcAverageLeadTime
      ,iv3.planningleadtime
      , iv1.itemtype
from iv00101 iv1
join iv00102 iv2 on iv2.itemnmbr=iv1.itemnmbr
      and iv2.locncode='LOCAL'
left join iv00103 iv3 on iv3.itemnmbr=iv1.itemnmbr
      and iv3.vendorid=iv2.primvndr
where iv2.ORDRPNTQTY>0
      and iv1.itemtype<>'2'
0
Comment
Question by:jdr0606
[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
2 Comments
 
LVL 13

Accepted Solution

by:
Koen Van Wielink earned 2000 total points
ID: 40305151
Stored procedures cannot be called from within a select statement. If you have to use a stored procedure, you have to call it separately, from within a cursor for example. The idea would be to store your normal select values in a temp table with an empty column for the value returned by the stored procedure. After that, you run a cursor over your temp table and update each row with the value returned by the stored procedure. This is not very efficient however.
You might want to consider changing your procedure to a function instead. Functions can be put inside a select clause.
0
 
LVL 11

Expert Comment

by:HuaMinChen
ID: 40305161
One SP cannot be called within Select statement, or you should change the SP into a function and retry.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

771 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