Using results from query as stored procedure parameters and combining results
0
votes
0
answers
105
views
I have a standard select query which returns a set of results :-
lang-SQL
SELECT
c.CASEID,
c.Event,
c.ACTION,
c.CYCLE,
c.PROPERTYTYPE,
c.COUNTRYCODE,
c.STATUSCODE,
c.CRITERIA,
c.DATE,
c.TITLE,
c.DESCRIPTION
FROM CASES c
WHERE c.DATE = GETDATE()
and c.EVENT in (1, 2, 3, 4)
I'm trying to use some of the fields from each row of the query as parameters for a stored procedure which returns a single row of 4 columns :-
Amount1, Amount 2, Cost, Currency.
The stored procedure would run as :-
lang-SQL
exec AmountsCalc c.CASEID, 105, 96, c.ACTION, c.EVENT, c.CYCLE
This would return :-
| Amount1 | Amount2 | Amount3 |Cost|Currency|
|:---- |:---- | :-----| :-----| :-----|
| 10.00| 20.00 | 30.00|40.00 |GBP |
I'd like to combine the stored procedure result with each row from the original query where the c.CASEID is the link between each row of the results and SP, to use in an SSRS report.
I have spent a lot of time looking for an answer and have come up with a solution, but it doesn't seem like an efficient solution and I was hoping there might be a better one but I'm at the limits of my experience.
My solution is to have a dataset in SSRS with the original query, pass the caseid to a parameter and then have another dataset query that runs the stored procedure for each caseid in the parameter. Then use a lookup expression between the first dataset and the stored procedure dataset.
As the query for the stored procedure is using almost an identical select query as the original, it seems like there might be a better way than running the query twice. The query I’m using to retrieve the data from the stored procedure is :-
lang-SQL
DECLARE @CASEFEESCURSOR CURSOR,
@CASEID INT,
@Event int,
@Cycle smallint,
@Action nvarchar(10)
DECLARE @SPResults TABLE (
AMOUNT1 decimal (11,2) NULL,
AMOUNT2 decimal (11,2) NULL,
COST decimal (11,2) NULL,
CURRENCY nvarchar(3) NUll
)
Create table #TEMPCASEFEES (
CASEID int,
AMOUNT1 decimal (11,2) NULL,
AMOUNT2 decimal (11,2) NULL,
COST decimal (11,2) NULL,
CURRENCY nvarchar(3) NUll
)
SET @CASEFEESCURSOR = CURSOR FOR
--main query with minimal select fields to match main query in report
SELECT
c.CASEID,
c.Event,
c.ACTION,
c.CYCLE,
c.CRITERIA,
FROM CASES c
WHERE c.DATE = GETDATE()
and c.EVENT in (1, 2, 3, 4)
and c.CASEID in @pCASEID --parameter from first dataset in ssrs report
ORDER BY c.CASEID;
OPEN @CASEFEESCURSOR;
--- load first row
FETCH NEXT FROM @CASEFEESCURSOR
INTO @CASEID, @Event, @Action, @Cycle, @CRITERIA
WHILE @@FETCH_STATUS = 0
BEGIN
--- execute proc
INSERT INTO @SPResults
EXEC AmountsCalc @CASEID, 105, 96, @Action, @Event, @Cycle
INSERT INTO #TEMPCASEFEES (CASEID, Amount1, Amount2, Cost, Currency)
SELECT @CASEID, Amount1, Amount2, Cost, Currency FROM @SPResults
DELETE FROM @SPResults
--- get next row
FETCH NEXT FROM @CASEFEESCURSOR
INTO @CASEID, @Event, @Action, @Cycle, @CRITERIA
END;
CLOSE @CASEFEESCURSOR;
DEALLOCATE @CASEFEESCURSOR;
select * from #TEMPCASEFEES order by CASEID
Any suggestions would be appreciated.
I have looked at turning the called stored procedure into a function but I don't think I can as it calls another stored procedure. I also can't edit the stored procedure.
I do have a working solution but would like to know if there is a better way to do this.
Asked by UK1539
(25 rep)
Feb 9, 2024, 10:30 PM
Last activity: Feb 10, 2024, 06:55 PM
Last activity: Feb 10, 2024, 06:55 PM