Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join on a stored procedure?

I have a stored procedure that takes no parameters, and it returns two fields. The stored procedure sums up all transactions that are applied to a tenant, and it returns the balance and the id of the tenant.

I want to use the record set it returns with a query, and I need to join it's results on the id of the tenant.

This is my current query:

SELECT t.TenantName, t.CarPlateNumber, t.CarColor, t.Sex, t.SSNO, t.Phone, t.Memo,         u.UnitNumber,         p.PropertyName FROM tblTenant t     LEFT JOIN tblRentalUnit u     ON t.UnitID = u.ID      LEFT JOIN tblProperty p     ON u.PropertyID = p.ID  ORDER BY p.PropertyName, t.CarPlateNumber 

The stored procedure is this:

SELECT tenant.ID AS TenantID, SUM(ISNULL(trans.Amount,0)) AS TenantBalance FROM tblTenant tenant     LEFT JOIN tblTransaction trans     ON tenant.ID = trans.TenantID     GROUP BY tenant.ID 

I would like to add the balance from the stored procedure to it also.

How can I do this?

like image 404
Malfist Avatar asked May 28 '09 14:05

Malfist


People also ask

Can we use join in stored procedure?

Joining tables to obtain the needed data for a query, script or stored procedure is a key concept as you learn about SQL Server development. In a nutshell, joins are typically performed in the FROM clause of a table or view for the SELECT, INSERT... SELECT, SELECT... INTO, UPDATE and DELETE statements.

How can I join stored procedure and SQL table?

insert the result of the SP into a temp table, then join: CREATE TABLE #Temp ( TenantID int, TenantBalance int ) INSERT INTO #Temp EXEC TheStoredProc SELECT t.

Can you join with or condition?

If the OR is in the WHERE clause - it can only be modified to a UNION ALL if the OR is performed on the same column. As soon as you have an OR statement across multiple columns you have to insure that only one of the OR conditions can be true before a UNION ALL will work.

How do I combine two procedures in SQL?

CAST(SUBSTRING(TABLE_NAME, 7, 5) AS int) AS SystemID -- Returning the substring as a integer of the table name.


2 Answers

insert the result of the SP into a temp table, then join:

CREATE TABLE #Temp (     TenantID int,      TenantBalance int )  INSERT INTO #Temp EXEC TheStoredProc  SELECT t.TenantName, t.CarPlateNumber, t.CarColor, t.Sex, t.SSNO, t.Phone, t.Memo,     u.UnitNumber, p.PropertyName FROM tblTenant t INNER JOIN #Temp ON t.TenantID = #Temp.TenantID ... 
like image 99
devio Avatar answered Sep 19 '22 23:09

devio


I actually like the previous answer (don't use the SP), but if you're tied to the SP itself for some reason, you could use it to populate a temp table, and then join on the temp table. Note that you're going to cost yourself some additional overhead there, but it's the only way I can think of to use the actual stored proc.

Again, you may be better off in-lining the query from the SP into the original query.

like image 22
AllenG Avatar answered Sep 19 '22 23:09

AllenG