简体   繁体   中英

How to copy records from table and insert into same table using stored procedure SQL Server

I have following table:

在此处输入图片说明

I want to copy only those records which are from version 0 and their student_id is never repeated in version 1, that means unchanged records. and I want to insert all copied records to same table with version 1. What will be stored procedure for this.

using group by and having max(version) = 0 :

insert into student_name (student_id, student_name, version)
select student_id, max(student_name), 1
from student_name
group by student_id
having max(version) = 0

As a stored procedure, taking a parameter for version , that inserts records for students who do not have a record for that version: and outputs the rows that were inserted:

create procedure dbo.insert_new_version (@version int) as
begin;
  set nocount, xact_abort on;

  insert into student_name (student_id, student_name, version)
  output inserted.*
  select 
      student_id
    , student_name = max(student_name)
    , version = @version
  from student_name
  group by student_id
  having max(version) < @version
end;

rextester demo: http://rextester.com/JSTNI40605

returns:

+-----------+------------+--------------+---------+
| record_id | student_id | student_name | version |
+-----------+------------+--------------+---------+
|        11 |          3 | ccc          |       1 |
+-----------+------------+--------------+---------+

You can select the records by doing:

select t.*
from t
where t.version = 0 and
      not exists (select 1
                  from t t2
                  where t2.student_id = t.student_id and t2.version = 1
                 );

The rest is just an insert .

You can select and insert like this

Insert INTO tableName select t1.student_Id, t1.student_name,1 from tablename t1 
where t1.version = 0 and not exists 
(select 1 from tablename  t2 where t2.student_id = t.student_id and t2.version = 1);

You can try this by LEFT Join:

INSERT INTO tbl
SELECT  T1.record_id,T1.student_Id,T1.student_name, 1
FROM    tbl T1 LEFT JOIN tbl    T2
ON  T1.student_Id   =   T2.student_Id AND   T2.version      =   1
WHERE   T1.version  =   0 AND       T2.record_id    IS NULL

I'd suggest using a while loop to go through the table and identify the items that you need to copy, then these values will be evaluated and if they meet the criterion for re-inserting and then insert them.

Your code should look like the following. Add CREATE PROC part and Edit table names where applicable (Caveat: I have written this on notepad so if you get a few errors, just try to fix them)

DECLARE @counter int = 0, @row_Count int = 0, @currentId int,
@currentName nvarchar(100), @version int, @current_Student_id int

SET @row_Count = (SELECT COUNT(record_Id) from yourTable)

WHILE @counter <= @row_Count
BEGIN
SET @currentId = (SELECT record_Id FROM (SELECT row_number() over (order by id) 
            AS RowNum, record_Id  FROM yourTable) sub WHERE RowNum=@counter)

SET @currentName = (SELECT student_name FROM yourTable WHERE record_Id = @currentId)
SET @current_Student_id = (SELECT student_id FROM yourTable WHERE record_Id = @currentId)
SET @version = (SELECT version FROM yourTable WHERE record_Id = @currentId)

--USE IF to check if the current version is 0 and the student ID has not been inserted already

IF (SELECT COUNT(record_Id) FROM yourTable WHERE student_id = @current_Student_id AND version = 1) < 1
            AND @version = 0
BEGIN
INSERT INTO yourTable (student_id, student_name, version)
    VALUES
(
@current_Student_id,
@currentName,
1
)
END

SET @counter = @counter + 1;
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM