我创建了一个存储过程,它从某个表中计算并检索新的数据集:
" DECLARE @maxVal int " +
" Set @maxVal = (SELECT ID FROM TableCustomers " +
" WHERE Service_ID = @Service_ID) " +
" execute SP_CaculateData @maxVal ";
现在 TableCustomers 也有一个名为 CustomerName 的列,每个 CustmerName 可以有多个 Service_ID。 我怎样才能多次运行我的存储过程,这完全取决于每个 CustomerName 有多少服务。像这样的东西:
execute SP_CaculateData @maxVal
execute SP_CaculateData @maxVal
execute SP_CaculateData @maxVal
execute SP_CaculateData @maxVal
我一直在阅读有关游标的内容,但如果有人能帮助我,我将不胜感激。
最佳答案
一种选择是使用 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
我不能说这是一个比游标性能更好的解决方案,但它应该可以完成工作。
关于asp.net - 多次动态运行存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333405/