472,338 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

Help with trigger

MM
I currently have a trigger on a table which works fine.

It performs some audit trail functions.

************************************************** ****
CREATE TRIGGER trg_1 ON [dbo].[LINE14_PROD] 
FOR INSERT, UPDATE, DELETE
AS
BEGIN
 SET NOCOUNT ON
 
 DECLARE @ExecStr varchar(50), @Qry nvarchar(255)
 
 SET @ExecStr = 'DBCC INPUTBUFFER(' + STR(@@SPID) + ')'
 
 INSERT INTO tbl_inputbuffer
 EXEC (@ExecStr)

    insert tblWho execute sp_who @@spid

END
************************************************** ****

It inserts the results of the command 'DBCC INPUTBUFFER (@@SPID) into table tbl_InputBuffer
It inserts the results of the command 'spWho @@SPID) into table tblwho

What I would like it to do is make one record in one table (with the fields of both tables).

How can I rewrite the trigger to retrieve the results of each command and then insert the data as one record into a table?

EXISTING Table - tbl_InputBuffer:
CREATE TABLE [dbo].[tbl_inputbuffer] (
    [EventType] [nvarchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Parameters] [int] NULL ,
    [EventInfo] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


EXISTING Table - tblWho:
CREATE TABLE [dbo].[tblWho] (
    [spid] [int] NULL ,
    [ecid] [int] NULL ,
    [status] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [loginname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [hostname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [blk] [int] NULL ,
    [dbname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [cmd] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]

NEW Table -  tblAudit:
CREATE TABLE [dbo].[tblAudit] (
    [spid] [int] NULL ,
    [ecid] [int] NULL ,
    [status] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [loginname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [hostname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [blk] [int] NULL ,
    [dbname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [cmd] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [EventType] [nvarchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Parameters] [int] NULL ,
    [EventInfo] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


Thanks in advance,

MM
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 10 '05 #1
1 2144
MM (me@home.com) writes:
I currently have a trigger on a table which works fine.<br>
<br>
It performs some audit trail functions.<br>
<br>
************************************************** ****<br>
CREATE TRIGGER trg_1 ON [dbo].[LINE14_PROD]&nbsp; <br>
FOR INSERT, UPDATE, DELETE <br>
AS<br>
BEGIN<br>
&nbsp;SET NOCOUNT ON<br>
&nbsp;<br>
&nbsp;DECLARE @ExecStr varchar(50), @Qry nvarchar(255)<br>
&nbsp;<br>
&nbsp;SET @ExecStr = 'DBCC INPUTBUFFER(' + STR(@@SPID) + ')'<br>
&nbsp;<br>
&nbsp;INSERT INTO tbl_inputbuffer <br>
&nbsp;EXEC (@ExecStr)<br>
<br>
&nbsp;&nbsp;&nbsp; insert tblWho execute sp_who @@spid<br>
<br>
END<br>
<br>
It inserts the results of the command 'DBCC INPUTBUFFER (@@SPID) into
table tbl_InputBuffer<br>
It inserts the results of the command 'spWho @@SPID) into table tblwho<br>
<br>
What I would like it to do is make one record in one table (with the
fields of both tables).<br>


You could get the data into temp tables and from these insert into
the target table. An alternative would be to get the information
from sp_who directly from sysprocesses. I think the latter is perferable,
as then you don't need the temp table, and I have had bad experiences
of temp tables in triggers.

However, I would be hesitant to do this. Triggers should be fast,
as they execute in the context of a transaction. I don't really like
running INPUTBUFFER or querying sysprocesses from a trigger. sysprocesses
is a virtual table that is constructed from internal server structures.
How much resources it takes to get the information I don't know.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 10 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Curtis Gilchrist | last post by:
I'm trying my hand at triggers and it doesn't seem to be working for me. I have a very simple database that consists of one table: Employees. I...
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following...
11
by: Jules Alberts | last post by:
Hello everybody, Someone helped me earlier with this TCL trigger function: create or replace function tlow() returns trigger as ' set NEW($1) ...
11
by: ricolee99 | last post by:
Hi everyone, I'm trying to invoke my .exe application from a remote server. Here is the code: ManagementClass processClass = new...
4
by: SUKRU | last post by:
Hello everybody. Unfortunately I am pretty new to sql-server 2000 I need some help with a Trigger I created. I created a trigger witch takes the...
0
by: Michael L | last post by:
Hi Guys(I apologize for the lengty post - Im trying to explain it as best i can) I've been cracking my head on this one for the past 24+ hours...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second...
3
by: Sam Durai | last post by:
Need help to write a trigger according to the following business requirement. This on DB2 UDB V8.2 / AIX 5.3 Whenever a 100th record is inserted...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR...
3
by: faathir88 | last post by:
i'd like to insert lots of data n its hard to determine which field would be the primary key, coz all of them almost similar. So, i decided to use...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.