如何通过验证来自一个表的数据并将数据插入另一个表来创建存储过程

时间:2016-01-04 06:50:28

标签: sql-server sql-server-2008

我想通过使用sessionid和profileid验证表profilelogs和profilelogs_arch之间的数据来创建存储过程。如果会话ID和profileid不同,那么应该继续将数据插入profilelog_arch,否则插入不是

3 个答案:

答案 0 :(得分:0)

Create prcedure sp_archiveprofile_logs
as
IF EXISTS ( SELECT PROFILEID,SESSIONID FROM  tbl_profilelogs as main exists 
(SELECT PROFILEID,SESSIONID FROM  tbl_profilelogs_arch as arch where arch.profileid<>main.profileid and arch.sessionid<>main.sessionid))
begin
insert into tbl_profilelogs_arch select * from tbl_profilelogs
end 

以上代码只是一个逻辑,请根据要求进行必要的更改。

谢谢

答案 1 :(得分:0)

创建prhedure sp_archiveprofile_logs

作为

插入tbl_profilelogs_arch select * from tbl_profilelogs

答案 2 :(得分:0)

Create prcedure sp_archiveprofile_logs
as
IF EXISTS ( SELECT 1 
            FROM  tbl_profilelogs as main exists 
               (SELECT 1 
                FROM  tbl_profilelogs_arch as arch 
                WHERE arch.profileid<>main.profileid 
                AND arch.sessionid<>main.sessionid
               )
          )
        BEGIN
            INSERT INTO tbl_profilelogs_arch select * from tbl_profilelogs
        END 

试试吧......