1

我正在为客户开发一个统计数据解决方案,现在我需要通过使用来自另一个表的动态条件(由客户填写并且它可以每天更改)来选择一些行from a table销售数据)。

另外我需要使用光标来执行更多的计算和其他的东西所以这是场景:

DECLARE cRiga CURSOR LOCAL FAST_FORWARD FOR
    -- here i will put the SELECT ...
OPEN cRiga
    FETCH NEXT FROM cRiga INTO @field1, @field2, @field3, ...
    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- do some stuff here ...
        FETCH NEXT FROM cRiga INTO @field1, @field2, @field3, ...
    END
CLOSE cRiga
DEALLOCATE cRiga

这是我可以找到动态条件的条件表(请参阅列 Completa = 0)

IDIncentive CdMarca CdSettore CdGruppo Completa
----------- ------- --------- -------- -----------
1           COES    NULL      NULL     1
1           DELONG  10        0024     0  <
1           RHOSS   NULL      NULL     1
1           SILE    10        0012     0  <
1           SILE    11        0025     0  <
1           THERMI  NULL      NULL     1
....... more rows ...

为了更清楚,我尝试将许多查询中所需的 SQL 分解为:

Select Field1, ... from SELLDATA 
where IDIncentive=1 and CdMarca='DELONG' and CdSettore=10 and CdGruppo='0024'
UNION ALL
Select Field1, ... from SELLDATA 
where IDIncentive=1 and CdMarca='SILE' and CdSettore=10 and CdGruppo='0012'
UNION ALL
Select Field1, ... from SELLDATA 
where IDIncentive=1 and CdMarca='SILE' and CdSettore=11 and CdGruppo='0025'

你可以想象我不能这样做,因为客户每天都会改变条件,所以我的问题是:

我如何创建一个具有在光标中使用的所有条件(如我上面的示例)的选择?有办法吗?

感谢谁能帮助我,如果需要更多信息以更清楚地说明这个问题,请告知

4

3 回答 3

2

假设您实际上需要一个游标(10 次中有 9 次不需要),您可以使用动态 SQL 和 sp_executesql 在运行时创建任意游标:

declare @cursor cursor
declare @sql nvarchar(max)
declare @dynamic_part nvarchar(max)
declare @name sysname

set @dynamic_part = 'tables'
set @sql = '
  set @cursor = cursor static for select top (100) name from sys.'+@dynamic_part+'
  open @cursor'

exec sp_executesql @sql, N'@cursor cursor output', @cursor output 
while 1=1 begin
  fetch next from @cursor into @name
  if @@fetch_status <> 0 break
  print @name
end
close @cursor deallocate @cursor

或者,您可以创建临时表,通过动态 SQL 填充临时表,然后针对临时表定义游标:

declare @cursor cursor
declare @sql nvarchar(max)
declare @dynamic_part nvarchar(max)
declare @name sysname

if object_id('tempdb..#these_things') is not null drop table #these_things
create table #these_things (name sysname)

set @dynamic_part = 'tables'
set @sql = 'insert #these_things (name) select top (100) name from sys.'+@dynamic_part
exec (@sql)

set @cursor = cursor fast_forward for select name from #these_things
open @cursor
while 1=1 begin
  fetch next from @cursor into @name
  if @@fetch_status <> 0 break
  print @name
end
close @cursor deallocate @cursor

这可能更干净一些。

于 2012-06-01T14:12:46.680 回答
1

您可以使用已经存在的 ConditionTable 表通过连接来过滤 SELLDATA:无游标,无联合所有。

Select Field1, ... 
  from SELLDATA 
 inner join ConditionTable
    ON SELLDATA.IDIncentive=ConditionTable.IDIncentive 
   and SELLDATA.CdMarca=ConditionTable.CdMarca
   and SELLDATA.CdSettore=ConditionTable.CdSettore
   and SELLDATA.CdGruppo=ConditionTable.CdGruppo
 where ConditionTable.Completa = 0
于 2012-06-01T14:23:58.833 回答
0

我认为子查询应该对您有所帮助。

http://dev.mysql.com/doc/refman/5.1/en/subqueries.html

于 2012-06-01T13:51:37.283 回答