【问题标题】:Run a foreach loop on returned results in a stored procedure.在存储过程中对返回的结果运行 foreach 循环。
【发布时间】:2014-06-08 23:24:25
【问题描述】:

我有一个从 table1 返回结果的存储过程,然后我在另一个存储过程中使用该结果来更新 table2。我想知道是否可以将其组合到一个存储过程中,问题是它需要根据 table1 中的值在 table2 上运行 foreach 循环以查找所有要更新的值。不知道从哪里开始,甚至可能。

【问题讨论】:

    标签: c# sql sql-server stored-procedures


    【解决方案1】:

    这是为每个项目运行一些东西的示例。但是对于更新,你不应该需要这个,你应该能够通过 CROSS JOIN 来解决这个问题,无论如何下面的例子。

     declare @i int;
    set @i = 0;
    declare @t table
    (
    int id identity,
    varchar(max) col1,
    varchar(max) col2
    )
    
    insert into @t(col1,col2)
    select col1,
            col2 from  FROM        [Table]
    
    
    set i = 0;
    declare @sum int
    set @sum = 0;
    while(i < select count(*) from @t)
    begin 
     select case when (col1 == col2) then @sum = @sum+1 else @sum from @t
    where id = i;
    i++;
    end
    

    【讨论】:

      猜你喜欢
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 2014-07-10
      • 2021-03-22
      相关资源
      最近更新 更多