473,382 Members | 1,736 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,382 software developers and data experts.

Trigger Question

Have a table that has following fields (pkid, field1, field2, field3,
field4). I need to create a trigger that will insert a row into another
table with the pkid column that was updated. Any help will be appreciated.
Feb 14 '08 #1
9 1965
Chico Che,

This is just an outline since I don't have your table structures, but the
pseudo-table 'inserted' contains the data of any inserted rows and the after
data for any updated rows.

INSERT INTO AnotherTable
SELECT pkid FROM inserted

RLF

"Chico Che" <js*****@yahoo.comwrote in message
news:Xn**********************@216.196.109.144...
Have a table that has following fields (pkid, field1, field2, field3,
field4). I need to create a trigger that will insert a row into another
table with the pkid column that was updated. Any help will be appreciated.

Feb 14 '08 #2
First, thanks for the response

But keep getting invalid object name on the table
Feb 14 '08 #3
Please show your code. And which table has the invalid name? Etc?

RLF

"Chico Che" <js*****@yahoo.comwrote in message
news:Xn**********************@216.196.97.131...
First, thanks for the response

But keep getting invalid object name on the table

Feb 14 '08 #4
"Russell Fields" <ru***********@nomail.comwrote in news:##pzw30bIHA.5348
@TK2MSFTNGP03.phx.gbl:
Please show your code. And which table has the invalid name? Etc?

RLF

"Chico Che" <js*****@yahoo.comwrote in message
news:Xn**********************@216.196.97.131...
>First, thanks for the response

But keep getting invalid object name on the table


CREATE TRIGGER iolta_change on tbldata
AFTER INSERT AS
BEGIN
INSERT INTO tblaudit
SELECT pkid FROM inserted
END

Feb 14 '08 #5
Chico Che (js*****@yahoo.com) writes:
>>First, thanks for the response

But keep getting invalid object name on the table

CREATE TRIGGER iolta_change on tbldata
AFTER INSERT AS
BEGIN
INSERT INTO tblaudit
SELECT pkid FROM inserted
END
I will need to ask: what do you really expect us to help with? What
the name is of your tables?

The only thing I can say is that the above looks funny. Does tblaudit
only have a single column? Best practice is to always include a column
list with the INSERT statement to state which columns you are inserting
into.

Also, in your original post, you talke about updates. Here you have a
trigger for INSERT.

I'm afraid that if you don't give us more detail on what you want to
achieve, that we will not be able to help you.

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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 14 '08 #6
Chico,

Perhaps this link will help you understand what we are looking for. Read
the article "Please provide DDL and sample data" at
http://www.aspfaq.com/etiquette.asp?id=5006

RLF

"Chico Che" <js*****@yahoo.comwrote in message
news:Xn**********************@216.196.97.131...
"Russell Fields" <ru***********@nomail.comwrote in news:##pzw30bIHA.5348
@TK2MSFTNGP03.phx.gbl:
>Please show your code. And which table has the invalid name? Etc?

RLF

"Chico Che" <js*****@yahoo.comwrote in message
news:Xn**********************@216.196.97.131...
>>First, thanks for the response

But keep getting invalid object name on the table



CREATE TRIGGER iolta_change on tbldata
AFTER INSERT AS
BEGIN
INSERT INTO tblaudit
SELECT pkid FROM inserted
END

Feb 15 '08 #7
We have two tables

Create Table tblData(
pkid int not null,
field1 varchar(55),
field2 varchar(55),
field3 varchar(55)
field4 varchar(55))

Create Table tblAudit
(recid int not null)

A program is contantly updating or insert rows in table tblData. We want to
create a trigger that will insert the pkid from tblData into tblAudit when
a new row is inserted or a row is updated.

I apologize, first time poster.

Thanks
Feb 15 '08 #8
Chico Che,

Aside from a missing comma in your tblData DDL, everything works just fine.
I created the tables, the trigger from the previous post, inserted data and
the trigger inserted data to the audit table. So, I still don't know what
error you are getting, unless it was that tblData was not created. But your
trigger was also only for INSERT not for UPDATE.

So...

Create Table tblData(
pkid int not null,
field1 varchar(55),
field2 varchar(55),
field3 varchar(55), -- Added a comma here
field4 varchar(55))

Create Table tblAudit
(recid int not null)

CREATE TRIGGER iolta_change on tbldata
AFTER INSERT, UPDATE AS -- Added for both update and insert
BEGIN
INSERT INTO tblaudit
SELECT pkid FROM inserted
END

Having said that, Erland correctly pointed out the tiny audit table is not
very useful for auditing. Perhaps at least tracking who made the change and
when:

Create Table tblAudit
(recid int not null,
loginname nvarchar(128),
changedatetime datetime)

CREATE TRIGGER iolta_change on tbldata
AFTER INSERT, UPDATE AS
BEGIN
INSERT INTO tblaudit
SELECT pkid,
SUSER_SNAME(), -- current login name
GETDATE() -- current date and time
FROM inserted
END

Then if you want a delete trigger, you can create one of those as well.

RLF
"Chico Che" <js*****@yahoo.comwrote in message
news:Xn**********************@216.196.97.131...
We have two tables

Create Table tblData(
pkid int not null,
field1 varchar(55),
field2 varchar(55),
field3 varchar(55)
field4 varchar(55))

Create Table tblAudit
(recid int not null)

A program is contantly updating or insert rows in table tblData. We want
to
create a trigger that will insert the pkid from tblData into tblAudit when
a new row is inserted or a row is updated.

I apologize, first time poster.

Thanks

Feb 15 '08 #9
On Fri, 15 Feb 2008 14:20:57 -0600, Chico Che wrote:
>We have two tables

Create Table tblData(
pkid int not null,
field1 varchar(55),
field2 varchar(55),
field3 varchar(55)
field4 varchar(55))

Create Table tblAudit
(recid int not null)

A program is contantly updating or insert rows in table tblData. We want to
create a trigger that will insert the pkid from tblData into tblAudit when
a new row is inserted or a row is updated.

I apologize, first time poster.

Thanks
Hi Chico,

The table is called tblAudit (capital A) here, but tblaudit (small a) in
the trigger code you posted. If your database uses a case sensitive
collation, these names are considered different.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Feb 15 '08 #10

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

Similar topics

4
by: Joel Thornton | last post by:
Whenever something is inserted to a given table, I want to run some shell commands using xp_cmdshell. Would it be a bad idea to put this xp_cmdshell in the INSERT trigger of this table? I...
1
by: Matik | last post by:
Hello to all, I have a small question. I call the SP outer the DB. The procedure deletes some record in table T1. The table T1 has a trigger after delete. This is very importand for me, that...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
2
by: robert | last post by:
typed this into the ibm.com search window, but didn't get anything that looked like it would answer a question: +trigger +faster +db2 +cobol the question: are DB2 (390/v6, at the moment)...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
5
by: Bob Stearns | last post by:
I have two (actually many) dates in a table I want to validate on insertion. The following works in the case of only one WHEN clause but fails with two (or more), with the (improper?...
3
by: ChrisN | last post by:
Hello all, I have a quick question. I'm using a C# object to commit new rows to a database. In the database I have an INSERT Trigger watching values come in. If the record to be committed...
3
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code...
9
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger,...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.