473,398 Members | 2,165 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,398 software developers and data experts.

Duplicate a row in SP code

I'm sure there's a simple way to do it, I just haven't run into it yet:
I just want to duplicate a table record (row) using a stored procedure.
lq
Jul 20 '05 #1
3 5838
The solution depends on exactly what you mean by 'duplicate'. Hopefully,
the following will get you started:

CREATE TABLE MyTable1
(
Col1 INT NOT NULL
CONSTRAINT PK_MyTable1
PRIMARY KEY
)

CREATE TABLE MyTable2
(
Col1 INT NOT NULL
CONSTRAINT PK_MyTable2
PRIMARY KEY
)

INSERT INTO MyTable1 VALUES(1)
INSERT INTO MyTable1 VALUES(2)
GO

CREATE PROCEDURE CopyRow
@Col1 int
AS
INSERT INTO MyTable2
SELECT Col1 FROM MyTable1
WHERE Col1 = @Col1
GO

EXEC CopyRow 1

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Lauren Quantrell" <la*************@hotmail.com> wrote in message
news:47**************************@posting.google.c om...
I'm sure there's a simple way to do it, I just haven't run into it yet:
I just want to duplicate a table record (row) using a stored procedure.
lq

Jul 20 '05 #2
What do you mean by "duplicate a row"? Normally we aim to avoid duplicating
data in a database but here are two examples taken from Pubs.

Iinsert a duplicate of a row into a table:

INSERT INTO Authors
(au_id, au_lname, au_fname, phone, address, city, state,
zip, contract, total_sales, ytd_sales)
SELECT <new PK value>, au_lname, au_fname, phone, address, city, state,
zip, contract, total_sales, ytd_sales
FROM Authors
WHERE au_id = '172-32-1176'

Return duplicates of a single row:

SELECT au_id, au_lname, au_fname, phone, address, city, state,
zip, contract, total_sales, ytd_sales
FROM Authors
CROSS JOIN (SELECT 1 UNION ALL SELECT 1) AS T(x)
WHERE au_id = '172-32-1176'

Neither of these examples serves much of a useful purpose but HTH.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #3
You can use the INSERT COMMAND for two times to get the duplicate
records in your table, but be sure that there is no unique contrauint
on that table.

Regards

Prashant Thakwani

la*************@hotmail.com (Lauren Quantrell) wrote in message news:<47**************************@posting.google. com>...
I'm sure there's a simple way to do it, I just haven't run into it yet:
I just want to duplicate a table record (row) using a stored procedure.
lq

Jul 20 '05 #4

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

Similar topics

3
by: Mohammed Mazid | last post by:
Hi, Basically I have a problem with registering to my quiz system. I had borrowed some code from an existing program but I just do not know why it doesn't work. If (txtUsername = "" Or...
7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
7
by: ucfcpegirl06 | last post by:
Hello, I have a dilemma. I am trying to flag duplicate messages received off of a com port. I have a software tool that is supposed to detect dup messages and flag and write the text "DUP" on...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
5
by: baur79 | last post by:
Hi guys i try to run this code in loop and to pass even the entry is duplicated def email_insert_in_db(email): sql="INSERT INTO emails (email) values ('%s') "%(email)...
2
by: Harry Haller | last post by:
I want to duplicate the form data, edit it to remove some items (such as __EVENTTARGET, __EVENTVALIDATION, etc) and save it to a log. How can I make a duplicate (editable) copy of the Form...
1
by: oaklander | last post by:
I would like to make sure there are no duplicate data entries in my Oracle 9i table (called MainTable) which has an Id field that is the primary key, ValData with a varchar data type, Fid and Fid2...
2
by: raphael001 | last post by:
In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values...
1
by: xraive | last post by:
I have a problem with this. Currently I am trying Allen's code and i am not successful. Current Design Table1 (Main Form) TravelID (PK) ApprovedBY EntreredBy BudgetCode ExpenseCode
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.