Sql server 2005 knowing new record is inserted

前端 未结 4 1918
遥遥无期
遥遥无期 2020-12-22 08:34

Is there any way to find out whether a new record is inserted other than writing trigger in sql server 2005. (it can be tsql or by java...) Since the vendor is not allowing

相关标签:
4条回答
  • 2020-12-22 08:46

    I would suggest a best practice that might also help you out here. If you restrict updates and inserts to only go through a stored procedure, you could use that proc to also write out either an audit record or simply update a time/date field.

    0 讨论(0)
  • 2020-12-22 08:51

    I think you need to define "Is there any way to find out whether a new record is inserted other than writing trigger". Triggers are basically event handlers in SQL. So are you wanting to have something like a trigger that fires whenever a new row is inserted into a table? If so, the answer is yes, it's called SQL Server Notification Services.

    Here's a beginners article, but I'm sure you can just google "sql server notification" and get tons of results.

    Here's another good article: Using SqlDependency To Monitor SQL Database Changes

    0 讨论(0)
  • 2020-12-22 08:53

    You can define an audit table and then add an OUTPUT command to your INPUT or UPDATE statements (DELETE's too, btw).

    create table fab4 (id int identity(1,1),FName varchar(80));
    create table fab4_audit (act varchar(10),id int, FName_old varchar(80), FName_new varchar(80), ts datetime not null default(getdate()));
    
    -- insert
    insert into fab4(FName)
    output
    'INSERTED'
    , inserted.*
    into fab4_audit([act],id,FName_new)
    select 'John' union all
    select 'Paul' union all
    select 'George' union all
    select 'Ringo';
    go
    
    -- update
    update f
    set FName='Walrus'
    output
    'UPDATED'
    , inserted.id
    , deleted.FName
    , inserted.FName
    into fab4_audit([act],id,FName_old,FName_new)
    from fab4 f
    where FName='Paul';
    
    -- delete
    delete f
    output
    'DELETED'
    , deleted.id
    , deleted.FName
    into fab4_audit([act],id,FName_old)
    from fab4 f
    where FName in ('John','George');
    go
    
    select * from fab4;
    select * from fab4_audit;
    go
    
    0 讨论(0)
  • 2020-12-22 08:56

    Does the table have an auto increment primary key or a createDate column? You could write your a cron job that wakes up periodically and queries to see if anything new has hit the database since you last time you ran the cron job.

    0 讨论(0)
提交回复
热议问题