asp.net - 多次动态运行存储过程

标签 asp.net sql database stored-procedures

我创建了一个存储过程,它从某个表中计算并检索新的数据集:

  " 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/

相关文章:

javascript - 检查某些属性是否有值

asp.net - 如何访问 rowdatabound 上的 gridview 列?

java - ReadyStatement 多个替换,不带逗号分隔符

sql - 检查 Postgres 数组中是否存在值

database - Kohana 3.2 将 session 保存在数据库中

asp.net - 使用框架 4.8 的 web.config Asp.net Web 项目中不允许使用 'sameSite' 属性

sql - 如何格式化百分比?

apache - 将android连接到数据库

mysql - 检查字符串编号

asp.net - 在 IIS 7.5 上的同一端口上运行两个网站?