如何使用SQL中where子句中的列名更新表数据?

时间:2015-04-24 12:27:37

标签: sql sql-server-2012 sql-update multiple-columns

我在表格中有以下数据:

tbl_DevicePrices

id  condition   deviceid    Price
51  Broken       23          50.00
52  New          23          50.00
53  Mint         23          50.00
54  GOOD         23          50.00
55  Fair         23          50.00
56  Poor         23          50.00
81  New          15          350.00
82  Mint         15          350.00
83  GOOD         15          350.00
84  Fair         15          350.00
85  Poor         15          350.00
86  Broken       15          350.00

价格在Excel中更新,并且已使用匹配的COLUMN名称和Device ID上传以更新值。

DeviceID    Device     Category/Manufacturer    Fair  GOOD  Mint New   Poor Broken
23         Apple 5     iPad/Apple               60    60    60   70    60   60
15         Apple 6     iPad/Apple               400   400   400  450   400  300

过程: 用户将Edit Excel 表格中的PricesUpload此excel表格中的update prices这些设备。

例如:  DeviceID = 23,其各自conditions的价格为60,60,60,70,60,60需要通过将COLUMN名称与Excel中的数据相匹配 tbl_DevicePrices更新。即哪里 DeviceID = 23和公平,良好,薄荷,新的,差的值,对于DeviceID = 23且条件=破碎,新的,薄荷,良好,公平的表tbl_DevicePrices已破损,普尔。

需要输出:

id  condition  deviceid Price
51  Broken      23       60
52  New         23       60
53  Mint        23       70
54  GOOD        23       60
55  Fair        23       60
56  Poor        23       60
81  New         15       450
82  Mint        15       400
83  GOOD        15       400
84  Fair        15       400
85  Poor        15       400
86  Broken      15       300

帮助感谢!

2 个答案:

答案 0 :(得分:0)

假设excel文档(在下面称为ExcelUpload)存储在临时表中,并假设如果找不到条件我们就不必插入新记录......

    static void Test3()
    {
        var sessionName = "ASPNETMonitorSession";
        using (var session = new TraceEventSession(sessionName, null))  
        {
            Console.WriteLine("Starting Test1");
            session.StopOnDispose = true;
            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
            {
                session.Dispose();
            };
            using (var source = new ETWTraceEventSource(sessionName, TraceEventSourceType.Session))
            {
                Action<TraceEvent> action = delegate(TraceEvent data)
                {
                    Console.WriteLine("GOT EVENT: " + data.ToString());
                };

                var registeredParser = new RegisteredTraceEventParser(source);
                registeredParser.All += action;
                source.UnhandledEvents += delegate(TraceEvent data)
                {
                    if ((int)data.ID != 0xFFFE)         
                        Console.WriteLine("GOT UNHANDLED EVENT: " + data.Dump());
                };

                session.EnableProvider(new Guid("AFF081FE-0247-4275-9C4E-021F3DC1DA35"));
                Console.WriteLine("Starting Listening for events");
                source.Process();                                                              
            }
        }
        Console.WriteLine("Done");
        return;
    }

答案 1 :(得分:0)

我假设您正在将Excel表加载到名为UpdatePrices的数据库表中。以下几乎适用于任何SQL方言:

update tbl_DevicePrices dp
    set price = coalesce((select (case when dp.condition = 'Fair' then up.Fair
                                       when dp.condition = 'Good' then up.Good
                                       . . .
                                  end)
                          from UpdatePrices up
                          where up.deviceid = dp.deviceid
                         ), price);