Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling a Procedure when trying to create a variable within a query - TSQL

Posted on 2014-10-09
5
Medium Priority
?
148 Views
Last Modified: 2014-11-04
Hi All,
I am wondering if this is possible.
I understand how to create a variable within my query - when I am assigning a value from a table query to the variable. However I am wondering is it possible to instead of pulling the value from a query to actually pull it from a stored procedure?

For Example:
DECLARE  @Payout  Float

SELECT @Payout = SUM(revenue)
FROM
(EXEC  [dbo].[mystoredprocedure] '2014-09-01','2014-10-01')SUBQ

SELECT @Payout

Thank you,
P
0
Comment
Question by:Putoch
[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
  • 3
5 Comments
 

Author Comment

by:Putoch
ID: 40372027
I think I may have answered my own question.
By doing the following it allowed me to call the procedure.

DECLARE  @Payout  Float
EXEC @Payout  =[mystoredprocedure] '2014-09-01','2014-10-01'

hope this can help someone else.
0
 

Author Comment

by:Putoch
ID: 40372030
Actually I didn't!
That only returned all the rows from the SP. Instead of the actual value I needed

DECLARE  @Payout Float
SELECT @Payout = Payout
EXEC @Payout =[mystoredprocedur] @fromdate,@todate
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 1000 total points
ID: 40372038
Hello,

if you can user the temp table you can store the values to temp table and then assign it to your parameter i.e.

DECLARE @Payout Float

CREATE TABLE #tmpvalue
(
    TEMPpayout float
)
INSERT INTO #tmpvalue(TEMPpayout )
EXEC dbo.mystoredprocedur @fromdate,@todate

SELECT
    @Payout = TEMPpayout 
FROM 
    #tmpvalue

Open in new window

0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 40372049
As samo4fun noted, you can't do that directly.

Another possibility: add optional parameters to the proc to "tell" it to return only the total, and then use those params when you EXEC the proc:


CREATE PROCEDURE  mystoredprocedure
    @...existing params...,
    @return_revenue_total_only bit = 0,
    @revenue_total float = 0 OUTPUT
AS
...existing proc code...
IF @return_revenue_total_only = 1
    SELECT @revenue_total = SUM(revenue) FROM (...existing SELECT to return result set from proc...)
ELSE
    ...existing SELECT to return result set from proc...
GO



DECLARE @Payout  Float
EXEC  [dbo].[mystoredprocedure] '2014-09-01','2014-10-01', @return_revenue_total_only = 1,
    @revenue_total = @Payout OUTPUT
0
 

Author Comment

by:Putoch
ID: 40423056
thank you both for the feedback. I ended up using a global variable and that seemed to do the trick.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

722 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