I am new to sql and hope anyone can help me with this question.
we have a local database called HR-Master and a linked server called SPU . There is three tables in our local database namely EMP, REMEMP and EMPIC and one view created from the linked server called 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 -
Source
EmployeepassView
([COMID],[lastName],[Name_En],[PERTYPE],[REFID],[JobTitle],[Nationality],[PERMITAreas],[IssueDate],[ExpiryDate],[TeamInCharge],[MainRole] ,[SubRolE],[RoleName],[ROLEID],[Picture],[isDisable] ,[LastPersonPrint],[LastPrintDate],[NoOfTimesPrinted],[LastUpdated],
I want to copy the values from my views and put it in the above three tables like this
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'
Using this above query. How can I do this in a single query?Please help
You would have to do three separate inserts:
If I understand your comment about inserting ID's I am going to assume the EMPID comes from the dbo.emp table. So I would do the following:
EDITED FOR STORED 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'