Solved

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

Posted on 2014-10-09
5
143 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:
Scott Pletcher 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

828 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