Duplicate Record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm stumbling around a SQL insert query and was hoping I could get some help...

Basically, all I'm trying to do is duplicate one record in the database and
in the duplicate, add some data...

So, it would look something like this:

Field 1 Field2
Data Number

and copy it so the table contains:

Field 1 Field2
Data Number
Data2 Number

How would I do that?

- Mkauley
 
If you're doing it in SQL Server's Enterprise Manager, or in an ADP, you can just use their designers to do it. In SQL EM, it's
called an "Insert From" query, in an ADP, it's called an Append Query. In either case, you would specify the same table for your
source and destination tables, then just select the fields you want to copy to the new record and the values you want to put in
them.

If you're trying to do this directly in SQL, your query would look something like one of the following:

INSERT INTO MyTable
SELECT [Field 1] + "Data to append to Field 1", [Field 2]
FROM MyTable
WHERE MyTable.SomeIdentifyingField = SomeValue

....where, of course, SomeIdentifyingField and SomeValue are whatever method you want to use to identify the specific record to
change. The above syntax is used when you intend to insert data in EVERY field in the table (and is often used with SELECT *). The
syntax below is for when you only want to insert SOME fields into the new record, and is often used when you have an Identity (aka
AutoNumber) field in the table, and you simply CAN'T insert values into every field:

INSERT INTO MyTable([Field 1], [Field 2])
SELECT [Field 1] + "Data to append to Field 1", [Field 2]
FROM MyTable
WHERE MyTable.SomeIdentifyingField = SomeValue

It's considered good practice to use the latter syntax in any event, but there are reasons for both. Note that you don't have to
use square brackets around the field names unless there are spaces or other "special" characters in the field names.


Rob
 
JC said:
I'm stumbling around a SQL insert query and was hoping I could get some
help...

Basically, all I'm trying to do is duplicate one record in the database
and
in the duplicate, add some data...

So, it would look something like this:

Field 1 Field2
Data Number

and copy it so the table contains:

Field 1 Field2
Data Number
Data2 Number

How would I do that?

- Mkauley


INSERT INTO tbl (col1, col2, ...)
SELECT 'Data2', col2, ...
FROM tbl
WHERE col1 = ?
AND col2 = ?
AND ... ;
 
Back
Top