-
In your case, because the data is already in the table when the trigger is fired, if exists(select name from persons where name = @name).
It's always exists.
You can try modifying it to:
declare @datacount int;
select @datacount = count(*)from persons where name = @name;
if @datacount > 1
-
For defaults to the after trigger, so you should modify it to create a trigger statement as follows.
create trigger [dbo].[mytrigger2] on [dbo].[persons] instead of insert as
-
in the trigger. There are two ways to determine which columns have been changed:
Clause. 1. You can use the update() function, syntax: update (the column name is pure); Return value.
bool value. True if the column is updated, false otherwise. This function is more convenient and practical.
Clause. 2. Use the columns updated() function. Syntax: columns updated ( Return value: varbinary
Slag before the command 1Tables with up to eight columns. You can use the columns updated() function to determine this.
For example, if you want to test whether the first column has been modified, you can use columns updated()&1=1 to determine whether it has been modified. If true, the column is updated; Otherwise it was not repented lead update.
To test the fifth column, you can use columns updated()&16=16.
2.Tables with more than eight columns. To test the first column is updated with if the test is the 11th column, then use it, and so on.
-
Your trigger is post-triggered (equivalent to after insert), which is executed after the data is inserted.
That is, after the new data is inserted into the table, the trigger content is executed, and since the record is already in the table, the exists detection will always return true.
It is recommended to use instead of. Or, if there is another key value, add another key value condition to determine (if the other key value is different, it means that the contract number already exists; otherwise it still doesn't exist).
-
With the for keyword, the default is after, which means that the trigger is executed after the insert operation is complete.
Change for to instead of
-
create trigger trteston test -- Create a trigger in the test table.
for update -- Why the event fired.
as -- what to do after the event is triggered.
declare @a int
set @a = select a from updatedif (@a=1)
beginrollbackend
-
You are performing ordinary.
insert into reqdet values (.
Insert one row at a time?
Or execution. insert into reqdet select ..from ..
Insert multiple lines at once?
If it's inserted one row at a time.
Here, I would like to ask how to determine if sysno= already exists in the drawapplydet table'da'+right(@sreqno,10)+'0', delete these records first, and then do the insert records below? */
It's simple, and there's no need for extra judgment at all.
sysno='da'+right(@sreqno,10)+'0' ;
If the conditions are met, they will naturally be deleted, and if the conditions are not met, 0 rows will be deleted, which is equivalent to not deleting.
-
Just copy this ** there.
if exists(select 1 from rawapplydet where sysno='da'+right(@sreqno,10)+'0' )
begindelete from rawapplydet where sysno='da'+right(@sreqno,10)+'0'end
-
A special stored procedure that is automatically executed when a trigger inserts, updates, or deletes a table cannot be applied to general queries.
-
This can be done with error handling, where the error handler is called when notfound is performed to do what you need.
In the case of sqlserver, you can write a database-based trigger in which you can get the name of the table to be operated on, and sqlserver prohibits the user from deleting the specified table. >>>More
In the deleted table of the trigger, the deleted row is saved. Just take it out and insert another table. >>>More
create or replace trigger biud_scroe_a
before insert or update or deleteon scroe >>>More
select top(1)* from table_name order by update_time desc
My idea is to find the result of the first place in descending order of modification time, isn't it? >>>More
The specific steps are as follows:
1. First of all, create a trigger, which requires an update trigger to be created on the addtable table, as shown in the following figure, and then go to the next step. >>>More