473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2199
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 want to create a trigger that will limit the...
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 fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
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) return ' language 'pltcl'; I use it to force...
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 ManagementClass ("\\\\" +"RemoteServerName" +...
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 id of the affected row and does a update on a...
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 and i have tried creating the function in ten...
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 interval. Basically, I look in a SQL table (queue) to...
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 into my 'ACCOUNT' table with a particular...
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 at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
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 sequence for its PK by using trigger here's the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.