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

Duplicate records and set field to constant

I have a table on SQL 2000.

I need to duplicate a group of records into the same table but duplicates need to have one field set to constant.

Is it possible to modify Column value in the subselect?

Thanks
Feb 14 '08 #1
5 3818
ck9663
2,878 Expert 2GB
I have a table on SQL 2000.

I need to duplicate a group of records into the same table but duplicates need to have one field set to constant.

Is it possible to modify Column value in the subselect?

Thanks

Yes. Try this:

Expand|Select|Wrap|Line Numbers
  1. insert into yourtable (col1, col2, col3, col4)
  2. (select 'ThisIsTheCopy' as col1, col2, col3, col4 from yourtable where thisfield = 'thisvalue')
  3.  
-- CK
Feb 14 '08 #2
Yes. Try this:

Expand|Select|Wrap|Line Numbers
  1. insert into yourtable (col1, col2, col3, col4)
  2. (select 'ThisIsTheCopy' as col1, col2, col3, col4 from yourtable where thisfield = 'thisvalue')
  3.  
-- CK
I am sorry - was in a rush earlier. Here is better explanation of the problem:
Table has: col1, col2, col3 - for example could be State, County, City plus other columns.
I need to insert duplicates of some group of records but to change column to a different value.

I know this will insert duplicates for col1 = somevalue

INSERT INTO Table VALUES ( SELECT * FROM Table WHERE col1 = somevalue)

How can I change the col1 into "othervalue" just for duplicate records not the originals?
Is it possible to do it on the fly?

Also, prefer not to use column names in SELECT because column names will vary in different tables but I do know column name to use in WHERE clause and that the column that needs to be changed.

Thanks.
Feb 15 '08 #3
Delerna
1,134 Expert 1GB
unless I am missunderstanding you then you just need to modify
CK's example to something like this
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Table VALUES ( SELECT othervalue as col1,col2,col3 FROM Table WHERE col1 = somevalue)
  2.  
and you will have to specify the field names in the select because you want to change the value of one of them
Feb 15 '08 #4
unless I am missunderstanding you then you just need to modify
CK's example to something like this
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Table VALUES ( SELECT othervalue as col1,col2,col3 FROM Table WHERE col1 = somevalue)
  2.  
and you will have to specify the field names in the select because you want to change the value of one of them
Thanks for clarifying. I was hoping to avoid naming fields but looks like there is no other way.
Feb 15 '08 #5
ck9663
2,878 Expert 2GB
If you really want to grab the field names dynamically, you can still do it. It will take a little bit of coding, but you can.

Try running this on your sql analyzer:

Expand|Select|Wrap|Line Numbers
  1. select * from syscolumns where id (select id from sysobjects where name = 'YourTableNameHere')
It will return the column names of your table. You now need to create a cursor to fetch all those records to build a dynamic INSERT query and execute it.

Happy coding.

-- CK
Feb 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
1
by: Robert | last post by:
How can I query an existing table and update a field in each record in the table with the occurrence count of each record e.g. update field to 1 (= first record occurrence), update field to 2 for...
2
by: Ross | last post by:
i have less hair now than i used to...i have a database with many duplicate records in it with only one exception, there is one field with the date it was put in the database and that is different....
4
by: KT | last post by:
Is there any one click solution that would do the trick? I would like to create a button, so the person who maintains the database can perform clean up work to delete duplicate records which...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
4
by: mcca0081 | last post by:
hi - i'm trying to delete one of the duplicate records based on the most current date. here's the code for my access 2000 db. any help would be appreciated!!! - thank you kindly Sub...
7
by: jmstur2 | last post by:
I have a table with what I consider duplicate records. Data in all columns are duplicate except for the date column, meaning that duplicate data was entered on different dates and those dates were...
1
by: VinArt | last post by:
MS Acc 2003, XP Thank you in advance for any help. I have tables called "Makeup" and "Lines". Each makeup can have multiple lines. Goal is to create a new "makeup" with identical "lines"...
4
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but,...
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?
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:
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
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...
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,...
0
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...

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.