[英]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.我只想复制那些来自版本 0 的记录,并且它们的 student_id 在版本 1 中永远不会重复,这意味着未更改的记录。 and I want to insert all copied records to same table with version 1. What will be stored procedure for this.我想将所有复制的记录插入到版本 1 的同一个表中。为此将使用什么存储过程。
using group by
and having max(version) = 0
:使用group by
并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:作为一个存储过程,采用version
的参数,为没有该版本记录的学生插入记录:并输出插入的行:
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 reextester 演示: 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
.其余的只是一个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:您可以通过 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.我建议使用 while 循环遍历表格并确定您需要复制的项目,然后将评估这些值,如果它们符合重新插入的标准,然后插入它们。
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)在适用的情况下添加 CREATE PROC 部分和编辑表名(注意:我已将其写在记事本上,因此如果您遇到一些错误,请尝试修复它们)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.