Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- define the last customer ID handled
- DECLARE @LastCustomerID INT
- SET @LastCustomerID = 0
- -- define the customer ID to be handled now
- DECLARE @CustomerIDToHandle INT
- -- select the next customer to handle
- SELECT TOP 1 @CustomerIDToHandle = CustomerID
- FROM Sales.Customer
- WHERE CustomerID > @LastCustomerID
- ORDER BY CustomerID
- -- as long as we have customers......
- WHILE @CustomerIDToHandle IS NOT NULL
- BEGIN
- -- call your sproc
- -- set the last customer handled to the one we just handled
- SET @LastCustomerID = @CustomerIDToHandle
- SET @CustomerIDToHandle = NULL
- -- select the next customer to handle
- SELECT TOP 1 @CustomerIDToHandle = CustomerID
- FROM Sales.Customer
- WHERE CustomerID > @LastCustomerID
- ORDER BY CustomerID
- END
- -- define the last customer ID handled
- DECLARE @LastCustomerID INT
- SET @LastCustomerID = 0
- -- define the customer ID to be handled now
- DECLARE @CustomerIDToHandle INT
- SET @CustomerIDToHandle = 1
- -- as long as we have customers......
- WHILE @LastCustomerID <> @CustomerIDTOHandle
- BEGIN
- SET @LastCustomerId = @CustomerIDToHandle
- -- select the next customer to handle
- SELECT TOP 1 @CustomerIDTOHandle = CustomerID
- FROM Sales.Customer
- WHERE CustomerID > @LastCustomerId
- ORDER BY CustomerID
- IF @CustomerIDTOHandle <> @LastCustoemrID
- BEGIN
- -- call your sproc
- END
- END
- -- Declare & init (2008 syntax)
- DECLARE @CustomerID INT = 0
- -- Iterate over all customers
- WHILE (1 = 1)
- BEGIN
- -- Get next customerId
- SELECT TOP 1 @CustomerID = CustomerID
- FROM Sales.Customer
- WHERE CustomerID > @CustomerId
- ORDER BY CustomerID
- -- Exit loop if no more customers
- IF @@ROWCOUNT = 0 BREAK;
- -- call your sproc
- EXEC dbo.YOURSPROC @CustomerId
- END
- SELECT CustomerID, CustomerSum.Total
- FROM Customers
- CROSS APPLY ufn_ComputeCustomerTotal(Customers.CustomerID) AS CustomerSum
- CREATE FUNCTION ComputeCustomerTotal
- (
- @CustomerID INT
- )
- RETURNS TABLE
- AS
- RETURN
- (
- SELECT SUM(CustomerOrder.Amount) AS Total FROM CustomerOrder WHERE CustomerID = @CustomerID
- )
Add Comment
Please, Sign In to add comment