久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

T-SQL:更新后哪些 COLUMNS 發(fā)生了變化?

T-SQL: what COLUMNS have changed after an update?(T-SQL:更新后哪些 COLUMNS 發(fā)生了變化?)
本文介紹了T-SQL:更新后哪些 COLUMNS 發(fā)生了變化?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

好的.我正在對(duì)表中的單行進(jìn)行更新.除主鍵外,所有字段都將被新數(shù)據(jù)覆蓋.但是,并非所有值都會(huì)更改更新的 b/c.例如,如果我的表如下:

OK. I'm doing an update on a single row in a table. All fields will be overwritten with new data except for the primary key. However, not all values will change b/c of the update. For example, if my table is as follows:

TABLE (id int ident, foo varchar(50), bar varchar(50))

初始值為:

id   foo   bar
-----------------
1    hi    there

然后我執(zhí)行 UPDATE tbl SET foo = 'hi', bar = 'something else' WHERE id = 1

我想知道的是哪個(gè)列的值發(fā)生了變化,其原始值是多少,新值是什么.

What I want to know is what column has had its value changed and what was its original value and what is its new value.

在上面的示例中,我希望看到列bar"從那里"更改為其他地方".

In the above example, I would want to see that the column "bar" was changed from "there" to "something else".

可以不進(jìn)行逐列比較嗎?是否有一些優(yōu)雅的 SQL 語(yǔ)句,例如 EXCEPT 會(huì)比行更細(xì)粒度?

Possible without doing a column by column comparison? Is there some elegant SQL statement like EXCEPT that will be more fine-grained than just the row?

謝謝.

推薦答案

沒(méi)有您可以運(yùn)行的特殊語(yǔ)句來(lái)準(zhǔn)確地告訴您哪些列發(fā)生了更改,但是查詢并不難編寫(xiě):

There is no special statement you can run that will tell you exactly which columns changed, but nevertheless the query is not difficult to write:

DECLARE @Updates TABLE
(
    OldFoo varchar(50),
    NewFoo varchar(50),
    OldBar varchar(50),
    NewBar varchar(50)
)

UPDATE FooBars
SET <some_columns> = <some_values>
OUTPUT deleted.foo, inserted.foo, deleted.bar, inserted.bar INTO @Updates
WHERE <some_conditions>

SELECT *
FROM @Updates
WHERE OldFoo != NewFoo
OR OldBar != NewBar

如果你想真正做一些事情作為這些變化的結(jié)果,那么最好寫(xiě)一個(gè)觸發(fā)器:

If you're trying to actually do something as a result of these changes, then best to write a trigger:

CREATE TRIGGER tr_FooBars_Update
ON FooBars
FOR UPDATE AS
BEGIN
    IF UPDATE(foo) OR UPDATE(bar)
        INSERT FooBarChanges (OldFoo, NewFoo, OldBar, NewBar)
            SELECT d.foo, i.foo, d.bar, i.bar
            FROM inserted i
            INNER JOIN deleted d
                ON i.id = d.id
            WHERE d.foo <> i.foo
            OR d.bar <> i.bar
END

(當(dāng)然,您可能希望在觸發(fā)器中執(zhí)行更多操作,但有一個(gè)非常簡(jiǎn)單的操作示例)

(Of course you'd probably want to do more than this in a trigger, but there's an example of a very simplistic action)

您可以使用 COLUMNS_UPDATED 而不是 UPDATE 但我覺(jué)得這很痛苦,而且它仍然不會(huì)告訴您哪些列實(shí)際更改了,只是哪些列包含在 UPDATE 語(yǔ)句中.因此,例如,您可以編寫(xiě) UPDATE MyTable SET Col1 = Col1 并且它仍然會(huì)告訴您 Col1 已更新,即使沒(méi)有一個(gè)值實(shí)際更改.編寫(xiě)觸發(fā)器時(shí),您需要實(shí)際測(cè)試各個(gè)前后值,以確保獲得真正的更改(如果這是您想要的).

You can use COLUMNS_UPDATED instead of UPDATE but I find it to be pain, and it still won't tell you which columns actually changed, just which columns were included in the UPDATE statement. So for example you can write UPDATE MyTable SET Col1 = Col1 and it will still tell you that Col1 was updated even though not one single value actually changed. When writing a trigger you need to actually test the individual before-and-after values in order to ensure you're getting real changes (if that's what you want).

附言您也可以像 Rob 所說(shuō)的那樣 UNPIVOT,但您仍然需要在 UNPIVOT 子句中明確指定列,這并不神奇.

P.S. You can also UNPIVOT as Rob says, but you'll still need to explicitly specify the columns in the UNPIVOT clause, it's not magic.

這篇關(guān)于T-SQL:更新后哪些 COLUMNS 發(fā)生了變化?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開(kāi)發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問(wèn)題和答案的組合))
主站蜘蛛池模板: 亚洲高清成人在线 | 日韩精品久久 | 亚洲成人一二区 | 亭亭五月激情 | 黄色大片网 | 成人国产在线观看 | 国产精品国产成人国产三级 | 欧美精品片 | 久久久久国产一区二区三区不卡 | 精品欧美一区二区久久久伦 | 国产精品久久久久影院色老大 | 午夜精品在线观看 | 精品视频www | 免费看一级毛片 | 国产精品视频一区二区三区四区国 | 久久99精品久久久久久国产越南 | 国产精品亚洲成在人线 | 成人一级黄色毛片 | 国产精品久久久精品 | 视频一区在线播放 | 可以在线观看av的网站 | 一级片在线视频 | 91麻豆精品国产91久久久更新资源速度超快 | 国产精品久久久久久久久久久久久 | 一区二区中文 | 免费在线黄 | 成人性视频免费网站 | 九色porny自拍视频 | 成年人免费看的视频 | 午夜影院在线免费观看视频 | 91精品久久久久久久久久 | 亚洲福利一区二区 | 成人亚洲一区 | 999国产精品视频免费 | av在线一区二区 | 中文字幕日本一区二区 | 成人在线观看欧美 | 国产日韩欧美在线观看 | 久久免费观看视频 | 欧美一区二区在线视频 | 亚洲精品天堂 |