Solved

Call a SPROC from within a Select Query

Posted on 2014-09-04
2
186 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 13

Accepted Solution

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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