Solved

Call a SPROC from within a Select Query

Posted on 2014-09-04
2
185 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
2 Comments
 
LVL 12

Accepted Solution

by:
Koen Van Wielink earned 500 total points
Comment Utility
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 10

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now