3

我有一个表格顺序,其中有 3 列

  BookCode  Quantity  Billno
  --------------------------    
   Ed001        2      A1
   Ed002        3      A1
   Ed003        1      A1
   py001        5      A2
   ed001        1      A2
   ed005        2      A2
   ed003        4      A2

现在我想向用户显示我的结果

结果

   BookCode   A1     A2
   ----------------------    
   Ed001      2      1
   Ed002      3      0
   Ed003      1      4
   py001      0      5
   ed005      0      1

结果是我的临时表,我改变它并根据添加列Billno

这很好用

现在我从表中选择BookCode和插入结果QuantityOrder

我想要的是,如果已经bookcode存在,那么只需更新结果表,否则使用该书码插入一个新行。

我该怎么做?

4

2 回答 2

3

我用光标解决了它..

create table #Result(BookCode varchar(20),BookRate float)
create table #bill(bilno varchar(max) )
insert into #bill ('A1')
insert into #bill ('A2')

    SET @Ordnum = CURSOR FOR
        SELECT bilno FROM #bill 
                OPEN @Ordnum
            FETCH NEXT
            FROM @Ordnum INTO @billno
                WHILE @@FETCH_STATUS = 0
                 BEGIN                  
                        EXEC ('ALTER TABLE #temp ADD [' + @billno+ '] varchar(30)') 
                            SET @getbookcode = CURSOR FOR
                                SELECT BookCode , BookQuantity , BookRate FROM OrderDetail where BillNo = @billno
                            OPEN @getbookcode
                                FETCH NEXT
                                    FROM @getbookcode INTO @Tbookcode,@TbookQty,@TbookRate
                                        WHILE @@FETCH_STATUS = 0
                                        BEGIN
                                             if exists(select BookCode from #Result where BookCode = @Tbookcode and BookRate = @TbookRate)
                                                begin
                                                    EXEC('Update #Result set [' + @billno+']= '+@TbookQty+' where BookCode = '''+@Tbookcode+''' and  BookRate = '+@TbookRate+'')                 
                                                end                          
                                             else 
                                                begin
                                                    EXEC('Insert Into #Result ([' + @bno + '],BookCode,BookRate) values ( '+@TbookQty +' , '''+@Tbookcode+''','+@TbookRate+')')                                  
                                                end              
                                            FETCH NEXT FROM @getbookcode INTO @Tbookcode,@TbookQty , @TbookRate
                                        END
                                    CLOSE @getbookcode
                                    DEALLOCATE @getbookcode                              
                    FETCH NEXT FROM @Ordnum INTO @bno
                 END
                CLOSE @Ordnum
            DEALLOCATE @Ordnum

 select * from #Result

这里#bill 是存储Billnum 的临时表。

于 2013-02-22T08:03:56.873 回答
0

使用MERGE语句。这会根据匹配执行 INSERT 或 UPDATE

像这样的东西,其中包括所需的 PIVOT

MERGE INTO result R
USING (
        SELECT
              ISNULL(O1.BookCode, O2.BookCode) AS BookCode,
              ISNULL(O1.Quantity, 0) AS A1,
              ISNULL(O2.Quantity, 0) AS A2
        FROM
              (SELECT * FROM #ORDER WHERE BillNo = 'A1') O1
              FULL OUTER JOIN
              (SELECT * FROM #ORDER WHERE BillNo = 'A2') O2
                                 ON O1.BookCode = O2.BookCode
    ) src ON R.BookCode = src.BookCode
WHEN MATCHED THEN 
    UPDATE
    SET A1 = src.A1, A2 = src.A2
WHEN NOT MATCHED BY TARGET THEN
   INSERT (BookCode, A1, A2)
   VALUES (src.BookCode, src.A1, src.A2);
于 2013-02-22T07:39:56.550 回答