Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Overwriting fields in a table

Author  Topic 

caterpillar
Starting Member

9 Posts

Posted - 2006-07-21 : 16:17:32
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

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-07-21 : 16:28:56
Could you explain more, perhaps with a data example?

Tara Kizer
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-22 : 02:21:42
And give us information as described here
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

caterpillar
Starting Member

9 Posts

Posted - 2006-07-24 : 10:16:13
Thanks Tara & Madhivanan.
This is what i have done:
----------------------------------------------------------------------
if (select count(*) from table1 where field1=@param1) > 0 update table1 set field1=@param1

else

INSERT INTO
table1(field1,field2)
VALUES
(@param1,@param2)

--field1 and field2 are of type varchar
--there is no primary key in the table
----------------------------------------------------------------------
This has replaced all the existing values of field1 with param1.

I hope it has given a better picture now.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-07-24 : 10:27:28
This code can be used to delete duplicates.
DELETE
FROM Table1
WHERE Field1 = @Param1
AND Field2 = @Param2

INSERT Table1
(
Field1,
Field2
)
VALUES (
@Param1,
@Param2
)



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Q
Yak Posting Veteran

76 Posts

Posted - 2006-07-24 : 10:28:49
In your update statement should also be 'where field1= @param1'

But this won't do a thing. If field1 is equal to @param1, then set field1 to @param1...
Example: if field1 is 'a', then set field1 to 'a'...
Go to Top of Page
   

- Advertisement -