Solved

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

Posted on 2014-10-09
5
138 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
  • 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 250 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 69

Accepted Solution

by:
ScottPletcher earned 250 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

758 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

20 Experts available now in Live!

Get 1:1 Help Now