如何使用存储过程SQL Server从表中复制记录并插入到同一个表中

时间:2017-06-19 12:17:35

标签: sql sql-server

我有以下表格:

enter image description here

我只想复制那些来自版本0的记录,并且他们的student_id在版本1中从不重复,这意味着记录不变。并且我想将所有复制的记录插入到版本1的同一个表中。这将是什么存储过程。

5 个答案:

答案 0 :(得分:0)

您可以通过执行以下操作来选择记录:

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
                 );

剩下的就是insert

答案 1 :(得分:0)

使用group byhaving 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

作为存储过程,使用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演示:http://rextester.com/JSTNI40605

返回:

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

答案 2 :(得分:0)

您可以像这样选择和插入

Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\ITDHOST\core\classes\database.php:31 Stack trace: #0 C:\xampp\htdocs\ITDHOST\core\classes\users.php(10): Database::RunQuery('SELECT * FROM g...', Array) #1 C:\xampp\htdocs\ITDHOST\core\pieces\connection\login.php(8): Users::GetUser(1) #2 C:\xampp\htdocs\ITDHOST\core\functions.php(25): require_once('C:\\xampp\\htdocs...') #3 C:\xampp\htdocs\ITDHOST\core\pages\connection.php(4): include_piece('connection', 'login') #4 C:\xampp\htdocs\ITDHOST\core\functions.php(12): require_once('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\ITDHOST\core\init.php(18): include_page('connection') #6 C:\xampp\htdocs\ITDHOST\index.php(3): include('C:\\xampp\\htdocs...') #7 {main} thrown in C:\xampp\htdocs\ITDHOST\core\classes\database.php on line 31

答案 3 :(得分:0)

你可以通过LEFT加入来试试这个:

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

答案 4 :(得分:0)

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