私がこの質問を正しく読んでいる場合(私はそうではないかもしれません):::
これは、カーソルが可能な解決策の1つです。 しかし、カーソルは通常ひどい解決策です。
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
そして今カーソル(:<)
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
しかし、私は「セットベース」対「苦悶する列による列」の哲学を読むことをお勧めします。興味のある方は、メモを残してください。