将列转换为行并进行更新

时间:2013-06-20 10:44:18

标签: c# sql-server

所以,我有一个excel文件,其中包含列格式的仓库和行格式的项目代码。在我的表中的数据库中,我有行代码的仓库代码和项目代码。我需要分别用所有仓库更新每个项目的库存(以列格式表示的excel)。在C#(前端)中,我可以通过应用循环来实现它。但循环执行的次数为2,25,000次。用仓库更新物品代码的库存值大约需要10个小时。因此,我建议从excel表中获取仓库代码,并将列(仓库代码)设置为行。我几乎就在那里,但正如我在上一个问题中所说,我得到的是列的名称,但不是它的价值。请提出任何解决方案。

1 个答案:

答案 0 :(得分:0)

我得到了答案,写了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