If I'm reading this question correctly (I may not be) :::
This is one of those things where a cursor is a possible solution. BUT, cursors are usually terrible solutions.
USE [Northwind]
GO
/* uspCustomerGetSingleWithOrders 'SIMOB' */
CREATE PROCEDURE [dbo].[uspCustomerGetSingleWithOrders] (
@customerId varchar(12)
)
AS
SET NOCOUNT ON
Select
--CustomerID ,
--ContactName ,
--City
CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax
from
dbo.Customers
WHERE
LTRIM(RTRIM(UPPER(CustomerID))) = LTRIM(RTRIM(UPPER(@customerId)))
Select
OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry
from
dbo.Orders o
WHERE
LTRIM(RTRIM(UPPER(o.CustomerID))) = LTRIM(RTRIM(UPPER(@customerId)))
SET NOCOUNT OFF
GO
and now a cursor ( :< )
Use Northwind
GO
declare @PROC_fetch_status int
declare @CustomerID varchar(12)
declare @ContactName varchar(24)
-- Select * from Customers
DECLARE curCustomers CURSOR FAST_FORWARD FOR select CustomerID , ContactName
from Customers Order by ContactName
OPEN curCustomers
-- Perform the first fetch.
fetch curCustomers into @CustomerID , @ContactName
select @PROC_fetch_status = @@fetch_status
IF @PROC_fetch_status <> 0
begin
print 'No Records'
end
WHILE @PROC_fetch_status = 0
BEGIN
print 'Current Customer = ' + @CustomerID + ' : ' + @ContactName
print '------------------------------------'
print ''
EXEC dbo.uspCustomerGetSingleWithOrders @CustomerID
print ''
print ''
FETCH NEXT FROM curCustomers INTO @CustomerID , @ContactName
select @PROC_fetch_status = @@fetch_status
END
CLOSE curCustomers
DEALLOCATE curCustomers
But I would suggest reading up on "set based" versus "row by agonizing row" philosophies.
If you're interested, please leave a note.