如何从链接的服务器视图插入多个表?

时间:2017-08-08 10:52:49

标签: sql sql-server database insert linked-server

我是sql的新手,希望有人可以帮我解决这个问题。

我们有一个名为 HR-Master 的本地数据库和一个名为 SPU 的链接服务器。 我们的本地数据库中有三个表,即 EMP REMEMP EMPIC ,以及一个名为 EmployeepassView的链接服务器创建的视图

EMP

ID,LASTNAME,FIRST NAME, MIDNAME,COMID,LASTCHANGED, PERMISSIONS, ALLOWEDPERMISSIONS,ASSET_GROUPID,TR_DBID,QUED,SEGMENTID -

REMEMP

ID ,*JOBROLE,*NATIONALITY ,*PERMITAREAS,DEPT , *ISSUEDATE,*REFID,*TEAMINCHARGE 
 *MAINROLE,*SUBROLE,*ROLENAME,*ROLEID, EXPIRYDATE

EMPPIC

EMPID,OBJECT,TYPE ,*EMP_BLOB,*LASTCHANGED,ACCEPTANCETHRESHOLD, BIO_BODYPAR -

来源

EmployeepassView

([COMID],[lastName],[Name_En],[PERTYPE],[REFID],[JobTitle],[Nationality],[PERMITAreas],[IssueDate],[ExpiryDate],[TeamInCharge],[MainRole] ,[SubRolE],[RoleName],[ROLEID],[Picture],[isDisable] ,[LastPersonPrint],[LastPrintDate],[NoOfTimesPrinted],[LastUpdated],

我想复制我的观点中的值并将其放在上面这三个表中

     INSERT dbo.emp ([LASTNAME],[COMID)],[LASTCHANGED])
        INSERT dbo.rememp ([jobrole],[nationality)],[PERMITAREAS], [ISSUEDATE],[REFID],[TEAMINCHARGE], [MAINROLE],[SUBROLE],[ROLENAME],[ROLEID])
        INSERT dbo.emppic ([LASTNAME],[SSNO)],[LASTCHANGED])
    select ([LASTNAME],[COMID)],[LASTCHANGED],[jobrole],[nationality],[PERMITAREAS], [ISSUEDATE],[REFID],[TEAMINCHARGE], [MAINROLE],[SUBROLE],[ROLENAME],[ROLEID],[LASTNAME],[SSNO)],[LASTCHANGED])
from EmployeepassView as ET
where ET.COMID='1234'

使用以上查询。如何在单个查询中执行此操作?请帮助

1 个答案:

答案 0 :(得分:0)

你必须做三个单独的插入:

如果我理解你关于插入ID的评论,我将假设EMPID来自dbo.emp表。所以我会做以下事情:

为存储的PROC编辑:

CREATE PROCEDURE InsertStuff @ID int
AS

INSERT dbo.emp ([ID],[LASTNAME],[COMID)],[LASTCHANGED])
 select @ID,[LASTNAME],[COMID)],[LASTCHANGED] from EmployeepassView as ET
 where ET.COMID='1234'

        INSERT dbo.rememp 
SELECT (@ID,[jobrole],[nationality)],[PERMITAREAS], [ISSUEDATE],[REFID],[TEAMINCHARGE], [MAINROLE],[SUBROLE],[ROLENAME],[ROLEID])
select [jobrole],[nationality],[PERMITAREAS], [ISSUEDATE],[REFID],[TEAMINCHARGE], [MAINROLE],[SUBROLE],[ROLENAME],[ROLEID],[LASTNAME],[SSNO)],[LASTCHANGED]) from EmployeepassView as ET
where ET.COMID='1234'

INSERT dbo.emppic ([EMPID],[LASTNAME],[SSNO)],[LASTCHANGED])
select @ID, [LASTNAME],[COMID)],[LASTCHANGED] from EmployeepassView as ET
where ET.COMID='1234'