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

T-SQL 如何在沒(méi)有觸發(fā)器的情況下設(shè)置修改日期,

T-SQL How to set modification date without Trigger only if other values actually changed(T-SQL 如何在沒(méi)有觸發(fā)器的情況下設(shè)置修改日期,僅當(dāng)其他值實(shí)際更改時(shí))
本文介紹了T-SQL 如何在沒(méi)有觸發(fā)器的情況下設(shè)置修改日期,僅當(dāng)其他值實(shí)際更改時(shí)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我的總體目標(biāo)是僅在需要更新的部分"上更新 [StandardUnitCost] 字段,同時(shí)僅在相同的行上更新 [DateUpdated] 字段.我使用的工具僅通過(guò)匹配 [DateUpdated] 列來(lái)同步數(shù)據(jù),因此重要的是它只在 [StandardUnitCost] 也已更新的行上更改.重要的是兩者都發(fā)生,并且只發(fā)生在同一行.如果 [StandardUnitCost] 字段將更新為與當(dāng)前相同的值,則該值未更改且 [DateUpdated] 字段應(yīng)該更新.

My overall goal is to update the [StandardUnitCost] field only on "parts" that need to be updated while also updating the [DateUpdated] field only on those same lines. I am using a tool that syncs the data by matching up to the [DateUpdated] column only, so it is important that it only changes on lines that the [StandardUnitCost] has also updated on. It is important that both happen, and only on the same rows. If the [StandardUnitCost] field will be updated with the same value that it currently has, then the value has not changed and the [DateUpdated] field should not be updated.

我目前將此作為兩個(gè)單獨(dú)的更新語(yǔ)句,需要幫助將它們組合起來(lái).

I currently have this as two separate update statements and need help combining them.

Update [mas_wgd].[dbo].[CI_Item]
  Set dateupdated = CASE
   WHEN StandardUnitCost < AverageUnitCost then convert (date, GETDATE())
   WHEN (AverageUnitCost + 2) >= 22.0
    AND standardunitcost > (AverageUnitCost + 2.000000) then convert (date, GETDATE())
   When StandardUnitCost < 22.000000
    And StandardUnitCost > 0 then convert (date, GETDATE())
  Else dateupdated
end
Where ProductLine IN ('A010', 'A020', 'A030', 'A040', 'A050', 'A060', 'A070', 'A080',
      'A090', 'A100', 'A110', 'A120', 'A130', 'A130', 'A140', 'A150', 'A200', 'A250',
      'A300', 'A350', 'A400', 'A450', 'A500', 'A550', 'A600', 'AGNC', 'C010', 'C020',
      'C030', 'C040', 'C050', 'C060', 'C070', 'C080', 'C090', 'C100', 'C110', 'C120',
      'C130', 'C130', 'C140', 'C150', 'C200', 'C250', 'C300', 'C350', 'C400', 'C450',
      'C500', 'C550', 'C600', 'CGNC')


Update [mas_wgd].[dbo].[CI_Item]
 Set Standardunitcost = CASE
  WHEN (AverageUnitCost between 0.010000 and 22.000000) Then  22.00000 
  WHEN AverageUnitCost > 22.000000 then AverageUnitCost + 2.000000
 Else StandardUnitCost 
end
Where ProductLine IN ('A010', 'A020', 'A030', 'A040', 'A050', 'A060', 'A070', 'A080',
      'A090', 'A100', 'A110', 'A120', 'A130', 'A130', 'A140', 'A150', 'A200', 'A250',
      'A300', 'A350', 'A400', 'A450', 'A500', 'A550', 'A600', 'AGNC', 'C010', 'C020',
      'C030', 'C040', 'C050', 'C060', 'C070', 'C080', 'C090', 'C100', 'C110', 'C120',
      'C130', 'C130', 'C140', 'C150', 'C200', 'C250', 'C300', 'C350', 'C400', 'C450',
      'C500', 'C550', 'C600', 'CGNC')

推薦答案

通過(guò) CTE 執(zhí)行此操作將允許您指定一次公式并使用它來(lái)觸發(fā)兩個(gè)字段的更新:

Doing this via a CTE will allow you to specify the formula once and use it to trigger the update on both fields:

SET NOCOUNT ON;
SET ANSI_NULLS ON;

DECLARE @Test TABLE (ID INT NOT NULL IDENTITY(1, 1),
                     AverageUnitCost INT NULL,
                     StandardUnitCost INT NULL,
                     ModifiedDate DATETIME,
                     WasUpdated BIT NULL);
INSERT INTO @Test VALUES (5, 8, GETDATE(), 0);
INSERT INTO @Test VALUES (15, 11, GETDATE(), 0);
INSERT INTO @Test VALUES (12, 35, GETDATE(), 0);
INSERT INTO @Test VALUES (22, 47, GETDATE(), 0);

SELECT * FROM @Test;

;WITH cte AS
(
  SELECT tmp.ID,
         tmp.StandardUnitCost,
         tmp.ModifiedDate,
         tmp.WasUpdated,
         CASE WHEN tmp.AverageUnitCost BETWEEN 10 AND 20 THEN 22
              WHEN tmp.AverageUnitCost > 20 THEN (tmp.AverageUnitCost + 2)
              ELSE tmp.StandardUnitCost
         END AS [NewValue]
  FROM @Test tmp
  --WHERE ProductLine IN ('A010'...) -- this is part of the real table so uncomment
)
UPDATE tab
SET    tab.StandardUnitCost = tab.NewValue,
       tab.ModifiedDate = GETDATE(),
       tab.WasUpdated = 1 -- only here to clearly indicate that the row was updated
FROM   cte tab
WHERE  tab.StandardUnitCost <> tab.NewValue;

SELECT * FROM @Test;

[WasUpdated] 字段之所以存在,只是因?yàn)樵撜Z(yǔ)句運(yùn)行得足夠快,其中更新的行上的 [ModifiedDate] 字段的值可能沒(méi)有差異.

The [WasUpdated] field is only there because the statement runs fast enough where there might not be a difference in value for the [ModifiedDate] field on rows that were updated.

這篇關(guān)于T-SQL 如何在沒(méi)有觸發(fā)器的情況下設(shè)置修改日期,僅當(dāng)其他值實(shí)際更改時(shí)的文章就介紹到這了,希望我們推薦的答案對(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)題和答案的組合))
主站蜘蛛池模板: 亚洲福利一区 | 九九热视频这里只有精品 | 久久精品国产精品青草 | 色狠狠一区 | 午夜看片网站 | 中文字幕在线观看第一页 | 日韩中文一区二区三区 | 国产在线视频在线观看 | 精品久久久久久亚洲综合网 | 亚洲毛片| 欧美日韩精品中文字幕 | 蜜桃视频一区二区三区 | 狠狠爱一区二区三区 | 亚洲成人av| 四虎永久在线精品免费一区二 | 欧美日一区 | 日韩欧美一区二区三区免费观看 | 亚洲三区在线播放 | 国产精品久久久久久久午夜片 | 91av在线电影| 91精品国产综合久久久久久丝袜 | 在线午夜 | www.97国产 | 国产精品v | 欧美一级免费看 | 成人精品国产免费网站 | 日韩久久综合网 | 久久久久国产一区二区三区 | 91视频电影 | 日韩在线小视频 | 国产激情偷乱视频一区二区三区 | 国产精品国产三级国产aⅴ原创 | 成人av一区二区三区 | 午夜影院 | 青青草一区二区三区 | 亚洲一区二区三区在线播放 | 日韩一区二区在线观看 | 狠狠干天天干 | 亚洲精品成人网 | 亚洲精品视频在线 | 亚洲视频在线看 |