I have created a stored procedure that calculates from some table and retrieve a new dataset back:
我创建了一个存储过程,从某些表中计算并检索新的数据集:
" DECLARE @maxVal int " +
" Set @maxVal = (SELECT ID FROM TableCustomers " +
" WHERE Service_ID = @Service_ID) " +
" execute SP_CaculateData @maxVal ";
Now the TableCustomers also have a column called CustomerName and each CustmerName can have multiple Service_ID's. How can I run my stored procedure multiple times, all depends on how many services each CustomerName has. Something like:
现在TableCustomers也有一个名为CustomerName的列,每个CustmerName都可以有多个Service_ID。如何多次运行存储过程,都取决于每个CustomerName拥有多少服务。喜欢的东西:
execute SP_CaculateData @maxVal
execute SP_CaculateData @maxVal
execute SP_CaculateData @maxVal
execute SP_CaculateData @maxVal
I have been reading something about cursors, but if anyone can give me a hand hear I would appreciate that.
我读过一些关于游标的东西,但是如果有人能帮我一个忙,请听我说,我会很感激的。
1 个解决方案
#1
2
One option is to use a while loop to iterate through the customers and service ids:
一种选择是使用while循环遍历客户和服务id:
declare
@maxVal int
,@customerName varchar(200)
,@serviceID int
select @customerName = MIN(CustomerName)
from TableCustomers t
while(select COUNT(1)
from TableCustomers t
where t.CustomerName >= @customerName) > 0
begin
--here we are dealing w/ a specific customer
--loop through the serviceIDs
select @serviceID = MIN(Service_ID)
from TableCustomers t
where t.CustomerName = @customerName
while(select COUNT(1)
from TableCustomers t
where t.CustomerName = @customerName
and t.Service_ID >= @serviceID) > 0
begin
select @maxVal = MAX(Id)
from TableCustomers t
where t.Service_ID = @serviceID
execute SP_CalculateData @maxVal
select @serviceID = MIN(Service_ID)
from TableCustomers t
where t.CustomerName = @customerName
and t.Service_ID > @serviceID
end
select @customerName = MIN(CustomerName)
from TableCustomers t
where t.CustomerName > @customerName
end
I can't say whether or not this is a solution that will perform better than a cursor, but it should get the job done.
我不能说这是否是一个比游标性能更好的解决方案,但它应该完成这项工作。
#1
2
One option is to use a while loop to iterate through the customers and service ids:
一种选择是使用while循环遍历客户和服务id:
declare
@maxVal int
,@customerName varchar(200)
,@serviceID int
select @customerName = MIN(CustomerName)
from TableCustomers t
while(select COUNT(1)
from TableCustomers t
where t.CustomerName >= @customerName) > 0
begin
--here we are dealing w/ a specific customer
--loop through the serviceIDs
select @serviceID = MIN(Service_ID)
from TableCustomers t
where t.CustomerName = @customerName
while(select COUNT(1)
from TableCustomers t
where t.CustomerName = @customerName
and t.Service_ID >= @serviceID) > 0
begin
select @maxVal = MAX(Id)
from TableCustomers t
where t.Service_ID = @serviceID
execute SP_CalculateData @maxVal
select @serviceID = MIN(Service_ID)
from TableCustomers t
where t.CustomerName = @customerName
and t.Service_ID > @serviceID
end
select @customerName = MIN(CustomerName)
from TableCustomers t
where t.CustomerName > @customerName
end
I can't say whether or not this is a solution that will perform better than a cursor, but it should get the job done.
我不能说这是否是一个比游标性能更好的解决方案,但它应该完成这项工作。