Solved

Call a SPROC from within a Select Query

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
kill process lock Sql server 9 65
SQL Server 2012 r2 - Calulations within stored procedure involving temp tables 6 40
Help Required 2 39
Master DB with Masterkey 1 35
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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