Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2006, 03:09 PM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Overwriting fields in a table

Hello All,

            I need to insert values in a table so that if a duplicate field comes for a record, it gets overwritten on the previous field and so on.
I dont have to use unique key constraints to avoid duplicate entry of data.

Any idea how to proceed....

Regards
Monica

 
Old July 22nd, 2006, 12:39 PM
Authorized User
 
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Monica!

You can use an instead of trigger for this case. But, one thing must be kept in mind that triggerd is fired once for a single INSERT/UPDATE/DELETE statement.

In this case, I am taking example of INSTEAD OF INSERT trigger.
------------------------------------------------------
-- Example of a Table (tbl_01)
CREATE TABLE tbl_01 ( id int , name varchar(20))
go

CREATE trigger trg_01 on tbl_01 instead of insert
as
declare @x int
declare @nam_old varchar(20)
declare @nam_new varchar(20)

select @x=id , @nam_new=name from inserted
if exists ( select id from tbl_001 where id=@x )
    Update tbl_001 set name=@nam_new where id=@x
else
    insert into tbl_001 select * from inserted

------------------------------------------------------
This will add new row and if id column is same, this would overwrite to the name column.

But, This will work for the last row only if you use a insert statement something like this -

INSERT INTO TBL_01
SELECT 2 , 'JACK'
UNION ALL
SELECT 3 , 'MAC'

Then, this example will work for MAC only.

Although, this demo can be modified to perform with same logic, but you may need a loop to perform it or a cursor.

But, if such insert statements are not used, this demo will do.

Reply soon ..

- Som Dutt
http://somdutt.blogspot.com



 
Old July 23rd, 2006, 01:09 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

Here is an example that will work for multi row as well as single row inserts
CREATE TABLE tbl_01 ( id int , name varchar(20))
go

CREATE trigger trg_01 on tbl_01 instead of insert
as
UPDATE tbl_01 set name = i.name
FROM tbl_01 t
JOIN INSERTED i
ON t.id = i.id

INSERT into tbl_01
select i.*
FROM tbl_01 t
RIGHT JOIN INSERTED i
ON t.id = i.id
WHERE t.id IS NULL


David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
 
Old July 24th, 2006, 09:00 AM
Authorized User
 
Join Date: Jul 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks all.....i will come back with the results!






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to obtain fields of a certain table? MTLedari ASP.NET 2.0 Professional 2 September 27th, 2007 03:56 PM
Fields in a dynamic table? Roshu Javascript How-To 12 January 10th, 2005 09:04 AM
Update Table fields karajian Access VBA 2 December 7th, 2004 03:48 PM
Updating table fields ppenn Access VBA 2 September 20th, 2003 06:09 PM
Listing the Fields in a table John Anthony Access VBA 1 July 4th, 2003 08:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.