将列转换为行并进行更新

本文关键字:更新 转换 | 更新日期: 2023-09-27 18:19:23

所以,我有一个excel文件,其中包含我的仓库列格式和项目代码行格式。在DB在我的表我有仓库代码和项目代码行格式。我需要分别在所有仓库更新每个项目的库存(excel列显示)。在c#(前端)中,我可以通过应用循环来实现它。但是这个循环被执行了奇数22.5万次。与仓库更新商品代码的库存值大约需要10个小时。所以,我建议从excel表格中获取仓库代码,并使列(仓库代码)行。我几乎在那里,但正如我在上一个问题中所说的,我得到了列的名称,但不是它的值。

将列转换为行并进行更新

我得到了答案,编写了2个while循环,并能够动态地确定列名及其值

DECLARE @intFlag INT
declare @count int
declare @count1 int
set @count1 = (select min(srno) from TEMP_STOCK_uPDATE)
declare @f varchar(20)
declare @wh varchar(20)
SET @intFlag = 1
set @count = (select max(srno) from dbo.TEMP_STOCK_uPDATE)
declare @col_Count int
set @col_Count = (select count(*) NoOfColumns from SYSCOLUMNS
WHERE id= (Select id from SYSOBJECTS where name = 'TEMP_STOCK_uPDATE'))
--select @col_Count

declare @cnt int
set @cnt=4
--select @intFlag
--select @count1
--select @count
--select @cnt
--select @col_Count



while(@count1<=@count)
begin
    while(@cnt<=@col_Count-1)
        begin
            declare @whcode varchar(20)
            set @whcode=(SELECT COLUMN_NAME
            FROM Ecata_New.INFORMATION_SCHEMA.COLUMNS
            where Table_Name = 'TEMP_STOCK_uPDATE'  
            and COLUMN_NAME =(select whcode from dbo.temp_stock_map where func=(select func from dbo.temp_stock_map where sr_no=(@cnt-3))))
            --select @whcode
            DECLARE @SQL VARCHAR(4000)
            --SET @SQL = 'select ' + @whcode + ' from TEMP_STOCK_uPDATE where F1=(select F1 from dbo.TEMP_STOCK_uPDATE where srno='+convert(varchar,16091)+')'
            SET @SQL = 'select ' + @whcode + ' from TEMP_STOCK_uPDATE where srno='+convert(varchar,@count1)
            --print @SQL
            --EXEC (@SQL)
            declare @tab table
            (value1 varchar(20)) 
            insert into @tab EXEC (@SQL) 
            declare @value2 varchar(20)
            set @value2=(SELECT * from @tab)
            declare @final decimal(10,2)
            if(@value2 is null)
            set @final=0.00
            else
            set @final = @value2
        update dbo.tbl_wh_Itemwise
        set
        whItm_OP_STK_Qty=@final,
        whItm_Creation_DT=getdate()
        where 
        whItm_item_code=(select F1 from dbo.TEMP_STOCK_uPDATE where srno=@count1) 
        and 
        whItm_wh_code=(select func from dbo.temp_stock_map where sr_no=(@cnt-3))
        delete from @tab
set @cnt=@cnt+1
end
set @cnt =4
set @count1=@count1+1
end