如果我正确地阅读了这个问题(我可能不是):::
这是游标可能的解决方案之一。 但是,游标通常是糟糕的解决方案。
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
但我建议阅读“基于集合”与“通过痛苦行来行”的哲学。如果您有兴趣,请留言。