473,836 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
 
 DECLAR E @ExecStr varchar(50), @Qry nvarchar(255)
 
 SET @ExecStr = 'DBCC INPUTBUFFER(' + STR(@@SPID) + ')'
 
 INSERT INTO tbl_inputbuffer
 EXEC (@ExecStr)

  &nb sp; 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] (
  &nb sp; [EventType] [nvarchar] (30) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [Parameters] [int] NULL ,
  &nb sp; [EventInfo] [nvarchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
) ON [PRIMARY]


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

NEW Table -  tblAudit:
CREATE TABLE [dbo].[tblAudit] (
  &nb sp; [spid] [int] NULL ,
  &nb sp; [ecid] [int] NULL ,
  &nb sp; [status] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [loginname] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [hostname] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [blk] [int] NULL ,
  &nb sp; [dbname] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [cmd] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [EventType] [nvarchar] (30) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
  &nb sp; [Parameters] [int] NULL ,
  &nb sp; [EventInfo] [nvarchar] (255) COLLATE SQL_Latin1_Gene ral_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 2218
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;DECLAR E @ExecStr varchar(50), @Qry nvarchar(255)<b r>
&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;&nb sp; 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****@sommarsk og.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
1768
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 EMP_TITLE field to either Ms., Mr., or Mrs. I am using the following code: CREATE trigger triTitleCheck ON employee FOR insert, update AS declare @v1 varchar
9
3467
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, VARCHAR(10) WO.PROBLEMCODE, VARCHAR(8)
11
3956
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 lowercase of values inserted in the db. There is one
11
5571
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" + "\\root\\CIMV2:Win32_Process");
4
1974
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 other table with that ID. The trigger works fine with one affected row. But when there are more then one rows affected, i get an error. I found out that SQL-server does not support row-level triggers. I should probable make my own cursor and...
0
1841
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 different ways and none of the versions i've made works exactly as it should. I have an array called $PageArray which contains a sorted list of all pages in my application. Im trying to create a recursive function(It dosn't need to be recursive if...
15
2587
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 determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
3
1938
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 'BATCH_ID' I need to either update or insert a row onto 'DESCRIPTION column of STATUS' table as verified for that particular BATCH_ID. Any help is greatly appreciated.
11
7886
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' does not exist drop table log_errors_tab;
3
1637
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 code : Create or replace trigger bef_ins_primer before insert on dpsj_primer for each row begin insert into dpsj_primer...
0
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.