Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-09
5
Medium Priority
?
151 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

971 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