[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how do i duplicate a record in a recordset where the primary key with identity set true

Posted on 2014-03-28
6
Medium Priority
?
222 Views
Last Modified: 2014-04-11
I was trying to create a stored procedure listed below
where the primary key is  RecordNumber  and  identity is true

CREATE PROCEDURE DuplicateRegisterHeader(@RecordNumber varchar(10))
AS
   Begin
     INSERT INTO tRegisterHeader
     SELECT * FROM tRegisterHeader
     WHERE RecordNumber = 278844
   End

this is the returned error msg
Msg 8101, Level 16, State 1, Procedure DuplicateRegisterHeader, Line 4
An explicit value for the identity column in table 'tRegisterHeader' can only be specified when a column list is used and IDENTITY_INSERT is ON.
0
Comment
Question by:rbrown0311
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 39962609
You'll need to explicitly list the columns you want to insert, and set IDENTITY_INSERT on, since you're specifying its value:


CREATE PROCEDURE DuplicateRegisterHeader (
    @RecordNumber varchar(10)
)
AS
SET NOCOUNT ON;
SET IDENTITY_INSERT tRegisterHeader ON
INSERT INTO tRegisterHeader ( RecordNumber, ...other_columns... )
SELECT @RecordNumber AS RecordNumber, ...other_columns...
FROM tRegisterHeader
WHERE
    RecordNumber = 278844
GO
0
 

Author Comment

by:rbrown0311
ID: 39970864
I've requested that this question be deleted for the following reason:

found solution
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39970866
I am re-opening this question to allow the author to re-evaluate as a valid solution was offered and they were unable to produce any.  Failing that an EE Moderator will review.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39980710
I recommend:
2) http:#a39962609
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question