を更新:SQL Serverのトリガ私は今私が説明し、基本的なSQLトリガーの例を必要とする列値
私は5列(カラム1、電源、COLUMN3、column4、回)
値を持つテーブルがあります彼のデータ型は「実」 「times」のデータ型は「int」
「電源」が0(ゼロ)回になるトリガーとなるでしょう。 '増加する1
私の英語は完璧ではないt!あなたが何を意味するのか理解してもらいたいと思っています!何かが明確でない場合は教えてください。次のように:)
を更新:SQL Serverのトリガ私は今私が説明し、基本的なSQLトリガーの例を必要とする列値
私は5列(カラム1、電源、COLUMN3、column4、回)
値を持つテーブルがあります彼のデータ型は「実」 「times」のデータ型は「int」
「電源」が0(ゼロ)回になるトリガーとなるでしょう。 '増加する1
私の英語は完璧ではないt!あなたが何を意味するのか理解してもらいたいと思っています!何かが明確でない場合は教えてください。次のように:)
仮定COLUMN1が主キーであり、トリガーの一般的な形式は:
create trigger MyPower
on MyTable
after insert, update
as
if exists (select column1
from inserted i join MyTable m
on i.column1 = m.column1
and i.power = 0)
update MyTable set times = times + 1
where exists (select column1 from inserted i
join MyTable m
on i.column1 = m.column1)
ありがとうありがとうございます "MyTable set power = power + 1"を 'times = times +1'に設定しないでください。 – LdB
申し訳ありませんが、私はそれを誤解し、答えを変更します。 –
悪いニュースをもたらすのは嫌いだが、カウント(*)は常に存在する。 –
可能基本的なトリガ:NOCOUNTおよび更新(パワー)のため
create trigger MyBasicTrigger on MyBasicTable
after insert, update
as
-- Trigger rowcount should not mess with update that triggered it, search for it in documentation provided here
set NoCount ON
-- If power is mentioned in update/insert statement at all
if update(Power)
begin
-- Update times for each changed row that has value of power 0
-- Inserted table holds new values in updated rows
-- You didn't mention your PK column(s), so I assume the name would be "ID"
update MyBasicTable set Times = Times + 1
from MyBasicTable inner join inserted on MyBasicTable.ID = inserted.ID
where Inserted.Power = 0
end
ドキュメントhereです。
Nikolaさん、ありがとう、私の主なキーはcolumn2 :) – LdB
説明のポイント:あなたのタイトルには "MSSQL"がありますが、あなたは "mysql"タグを持っています...あなたはどちらを使っていますか? –
ええ、あなたは間違っている私は誤ってmysqlタグを追加する間違っている入力エラーXD、btw私はmssqlを使用しています。 – LdB