Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 After read rows, then perform insert

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-06-01 : 14:06:28
Let's say i've table as follows,

t1
UID | Pwd
------------------------
mike | 1234
robin | 1890

t2
UID | LoginDte
------------------------



Let's say, i performed SQL as follow
select * from t1 where UID='mike' and Pwd='1234'

If rows returned, then i perform
insert into t2 values('mike',getdate())

it's possible to create trigger on select?

If yes, how the trigger look's like?




tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-01 : 14:09:43
Triggers fire on DML actions only (INSERT/UPDATE/DELETE). If you want to insert rows based upon a select, then you could instead use a SQL Agent job that runs every minute.

But can't you just modify the process that inserts into t1 so that it also inserts into t2?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-06-01 : 14:13:29
quote:
Originally posted by tkizer

Triggers fire on DML actions only (INSERT/UPDATE/DELETE). If you want to insert rows based upon a select, then you could instead use a SQL Agent job that runs every minute.

But can't you just modify the process that inserts into t1 so that it also inserts into t2?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Hi sir,

How did I know, someone perform
select * from t1 where UID='mike' and Pwd='1234'
?

I need to keep track this row has been read by someone. This track record, will be inserted into t2
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-01 : 14:22:37
Oh I see now what you want. This isn't possible except to trace for it, which is not a good idea. You'll need to add this functionality to your application.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-01 : 14:24:53
My suggestion would be to write it to some text file on a particular location within your application.Some kind of log entry.

PBUH
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-01 : 14:31:18
Why would you recommend a text file? What's the benefit of that when the application could just write it to t2 after the select is performed?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-01 : 15:01:10
I just think it is an unnecessary overhead on the DB to store this kind of data also how will you define the table t2?
I mean how many columns in it.One query may have about 15 filters while other one wont have any.What about the queries with JOINS in it?
How is the OP going to store them?
My suggestion would be write a simple method in DAL which writes the whole query statement to the text file.
If the OP is looking just for a couple of tables then the method of OP is fine.But it would be a pain if he/she wants to do it for all the tables in the DB.


PBUH
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-01 : 15:05:02
t2's DDL would need to closely match the select query's output.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-01 : 15:09:30
So if there are 10 tables and OP wants to log all the selects of those 10 tables he/she would have create 10 more tables that matches DDL of the existing tables he/she wants to log.

PBUH
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-01 : 15:15:17
I edited my post to add "closely". Typically audit tables will also include a timestamp and a userid.

And yes an audit table would be needed for any tables that need to be audited.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-01 : 15:27:13
But I think what OP wants cannot be called in real sense as "AUDIT".
But I wonder how will you audit for selects joining 4 -5 tables and having most of the columns in the select part?Wouldn't that be a little cumbersome.

PBUH
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-01 : 16:40:18
I can only go by what the OP posted has shown us so far. What I've described is satisfied by what the OP has shown us.

Auditing is cumbersome, that's just the nature of it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-06-02 : 00:40:15
tq for all the answer
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-02 : 12:27:48


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -