SMSS v17存储过程以将行插入表中

时间:2019-05-20 21:38:11

标签: sql sql-server row sql-insert

我有两个表:PS_Emp和PS_SAR_General

存储过程应使用PS_Emp中该表中不存在的任何员工ID更新PS_SAR_General。你能告诉我我哪里出问题了吗?

这是我到目前为止所拥有的:

CREATE PROCEDURE [dbo].[SAR_NewEmp_app]
@USID
AS
INSERT INTO dbo.PS_SAR_General ([USID])
VALUES (@USID)


FROM dbo.PS_Emp
WHERE NOT EXISTS (SELECT PS_Emp.[USID] FROM PS_Emp WHERE PS_SAR_General 
[USID] = PS_Emp.[USID]) AND PS_Emp.[Status] = 'Active' AND 
PS_Emp.[ECS ICS] = 'ECS' AND PS_Emp.[Employee Class] NOT IN 
('BRWDRESORCE', 'SUBCONTRACT')

1 个答案:

答案 0 :(得分:0)

对于其他在这里寻找答案的人来说,最终的结果是:

CREATE PROCEDURE [dbo].[SAR_AddNewEmps_app]
AS
INSERT INTO dbo.PS_SAR_General ([USID], [Status], [ECS ICS], [Employee Class])
  SELECT PS_SAR_General.USID, PS_SAR_General.[Status]
    , PS_SAR_General.[ECS ICS], PS_SAR_General.[Employee Class]
  FROM dbo.PS_SAR_General
  WHERE NOT EXISTS (
    SELECT PS_Emp.[USID]
    FROM PS_Emp
    WHERE PS_SAR_General.[USID] = PS_Emp.[USID]
  ) AND [Status] = 'Active'
  AND [ECS ICS] = 'ECS'
  AND [Employee Class] NOT IN ('BRWDRESORCE', 'SUBCONTRACT') 

此存储过程现在添加了新表中不存在但在employee表中存在的行。