解决方案 »

  1.   

    用sql循环处理数据到临时表,然后显示到grid上
      

  2.   


    代码怎么写啊?是在delphi进行的
      

  3.   

    CREATE TABLE [dbo].[t11](
    [ID] [int],
    [name] nvarchar(50),
    [num] decimal(18,10),
    [price] decimal(18,10),
    [noid] nvarchar(20),
    [amount] decimal(18,10)
    ) ON [PRIMARY]GOSET NOCOUNT ON
    DECLARE @id int,  @qty1 int, @qty2 decimal(18,10), @qty3 int, @num decimal(18,10),  @qty int 
    DECLARE @name varchar(50), @price decimal(18,10) , @qty4 decimal(18,10), @qty5 decimal(18,10)
    DECLARE @str varchar(8000), @str1 varchar(1000) 
    set @id = 1
    set @qty = 500
    set @str = N'
    WITH cte(ID, name, num, price, noid, amount, qty1, qty2)
    as (
    select ID, name, num, price, noid, amount, CONVERT (int,(amount/@qty)) as qty1 , CONVERT (float, (amount%@qty)) as qty2  from t11 where id = @id)'DECLARE acursor CURSOR FOR 
    select num, CONVERT (int,(amount/@qty)) as qty1 , CONVERT (float, (amount%@qty)) as qty2  from t11 where id = @id
    OPEN acursor
    FETCH NEXT FROM acursor 
    INTO @num, @qty1, @qty2
    set @qty3 = @qty1  
    if (@qty1 >0)
    BEGIN
       set @str1 = 'select ID, name, @qty4, price, noid, amount, @qty from cte ';
       set @str = @str + '  ' + RTRIM(@str1);
       set @qty1 = @qty1-1 
    ENDWHILE (@qty1 >0)
    BEGIN
       set @str1 = 'select ID, name, @qty4, price, noid, amount, @qty from cte ';
       set @str = @str + ' union all ' + RTRIM(@str1);
       set @qty1 = @qty1-1 
    ENDCLOSE acursor
    DEALLOCATE acursorif (@qty2>0) 
    begin
      set @str1 = 'select ID, name, @qty5, price, noid, amount, @qty2 from cte';
      set @str = @str + ' union all ' + RTRIM(@str1)
    end;set @qty4 =  CONVERT(int, (@num / @qty3))
    set @qty5 =  CONVERT(decimal(18,10), (@num % @qty3))set @str1 = 'DECLARE  @id int, @qty int, @qty2 decimal(18,10),  @qty3 decimal(18,10), ';
    set @str1 = @str1 + '@qty4 nvarchar(20),  @qty5 nvarchar(20)'  ;
    set @str1 = @str1 + ' set @id = ' + CONVERT(nvarchar(20), @id) ;
    set @str1 = @str1 + ' set @qty2 = ' + CONVERT(nvarchar(20), @qty2) ;
    set @str1 = @str1 + ' set @qty3 = ' + CONVERT(nvarchar(20), @qty3) ;
    set @str1 = @str1 + ' set @qty4 = ' + CONVERT(nvarchar(20), @qty4) ;
    set @str1 = @str1 + ' set @qty5 = ' + CONVERT(nvarchar(20), @qty5) ;
    set @str1 = @str1 + ' set @qty = '+ CONVERT(nvarchar(20), @qty) ;set @str = @str1 + '; '+ @str ;exec(@str)
      

  4.   


    代码怎么写啊?是在delphi进行的取得产品1的记录,然后 总金额 div 500 得到x,总金额 mod 500 得到y
    那么从第1到第x条记录都是产品1的金额为500的记录
    第x+1条记录为产品1的金额为y的记录
      

  5.   

    如果你想用DELPHI直接算,那就取出记录后, for 循环给 dbgrid 插入你计算出来的记录了如:
    总金额;  amount := 1600 ;你直接
    for i:=0 to Trunc(amount/500) do
    begin
        插入到  dbgrid   一条记录;
    end;if (amount mod 500 > 0)
    begin
         插入该记录的最后一条到  dbgrid ;
    end;
      

  6.   

    with cte1(id, name, price, qty, did, amount, tmp)
    as
    (select *, convert(decimal(18,2), amount) as tmp from t1)
    ,
    cte2(id, name, price, qty, did, amount, tmp)
    as
    (select *  from cte1
     union all
     select a.id, a.name, a.price, a.qty, a.did, a.amount, 
    convert(decimal(18,2), (a.tmp - 500)) as tmp from cte2 a  where a.tmp - 500 > 0
     )
    select id, name, did, qty, price, amount, 
    case when tmp > 500 then 500 else tmp end as tmp from cte2 
    order by id, name ----------------------------------------------------------------------------------------------------
    1 产品1 20808001 137.28 13.00 1784.68 500.00
    1 产品1 20808001 137.28 13.00 1784.68 500.00
    1 产品1 20808001 137.28 13.00 1784.68 500.00
    1 产品1 20808001 137.28 13.00 1784.68 284.68
    2 产品2 20808001 154.12 10.00 1542.12 500.00
    2 产品2 20808001 154.12 10.00 1542.12 500.00
    2 产品2 20808001 154.12 10.00 1542.12 42.12
    2 产品2 20808001 154.12 10.00 1542.12 500.00
    3 产品3 20808001 239.94 11.00 2639.32 500.00
    3 产品3 20808001 239.94 11.00 2639.32 500.00
    3 产品3 20808001 239.94 11.00 2639.32 500.00
    3 产品3 20808001 239.94 11.00 2639.32 500.00
    3 产品3 20808001 239.94 11.00 2639.32 500.00
    3 产品3 20808001 239.94 11.00 2639.32 139.32
      

  7.   


    yangb0803
    高手,,你的那存储过程学习了,,不过现在我要用delphi去实现这个功能,有的不太明白,,请再次指教
      

  8.   

    用DELPHI实现也好办啊~你QUERY 取到数据,  
    然后在循环中直取出单条数据, 计算也可以 拖个 ClientDataSet,  将计算出来的记录,一条条插入到  ClientDataSet 然后显示到 dbgrid里