SQL查询使用If Exists语句无法更新或插入行

时间:2018-08-21 09:29:51

标签: sql sql-server database sql-server-2008 tsql

以前发布时,我错过了保存urn和org_ref的表b。我有三张桌子。表a,b和c,其中我必须根据表a中的URN键更新表c的“总Emp”列。表B没有URN列,但是它具有Org_ref,因此要连接表a和表c,我们需要在a.urn = b.urn上连接表b。我编写了一个查询来遍历记录并进行插入或更新,但是它不起作用并且不会更新。只需在表b中插入空白“员工总数”即可。

   |urn  |total employee
   |333  |25
   |123  |26
   |21   |2
   |11   |23

表B

   |urn  |org_ref
   |333  |1234
   |123  |343
   |21   |2
   |11   |23

表C

   org_ref    total employe 
   1234            0       --should update from table a 
    343            0       --should update from table a 
                           --add record from table a org_ref 21
                           --add record from table a org_ref 11

这是我的剧本

Create table #orgs(
iorgrowid int identity (1,1),
ORG_REF  varchar(255),
Total_Leader int,
urn int
)


insert into #orgs

select  ORG.organisation_ref,s.[TOTAL_LEADERSHIP_ROLE],s.urn

    from DiTestDatabase.dbo.organisation org

    JOIN DataConversion.dbo.School_plus_download_NI s on org.total_headcount=s.urn 

DECLARE 
@iReturnCode int,
@iNextcodeRowId int,
@icodeRowId int,
@iCurrentcodeRowId int,
@iNextattRowId int,
@iCurrentattRowId int,
@icodeLoopControl int,
@ORGREF INT,
@counter int,
@Ly_employees int,
@orgnamecount int,
@add1count int,
@urn int


SELECT @iCodeLoopControl = 1
SELECT @iNextCodeRowId = MIN(iorgrowId)
FROM #orgs


IF ISNULL(@iNextCodeRowId,0) = 0
BEGIN
SELECT 'No data in found in table!'
RETURN
END


SELECT
@iCurrentcodeRowId = iorgrowId,
@ORGREF=ORG_REF,
@Ly_employees=Total_Leader,
@urn=urn
FROM #orgs
WHERE iorgRowId = @iNextcodeRowId


set @counter = 0    
set @orgnamecount=0
set @add1count=0

--Start the main processing loop.
WHILE @iCodeLoopControl = 1



BEGIN


        begin
          set @counter = @counter + 1
         print 'The counter is ' + cast(@counter as char)  + cast(@ORGREF as char)
        end

IF EXISTS (SELECT OG.ORGANISATION_REF
FROM ORG_ACCOUNT OG WHERE OG.ORGANISATION_REF= @ORGREF
)

BEGIN
  UPDATE ORG_ACCOUNT
  SET LY_EMPLOYEES= (SELECT SP.TOTAL_LEADERSHIP_ROLE
            FROM DataConversion.dbo.School_plus_download_NI SP
            JOIN ORGANISATION ORG ON ORG.TOTAL_HEADCOUNT=SP.URN
            JOIN ORG_ACCOUNT OA ON OA.ORGANISATION_REF=ORG.ORGANISATION_REF
            WHERE OA.ORGANISATION_REF=@ORGREF)
            FROM DataConversion.dbo.School_plus_download_NI SP WHERE SP.URN=@urn

    END

ELSE

BEGIN

   alter table ditestdatabase.dbo.org_account disable trigger all   

    INSERT INTO ORG_ACCOUNT
    (ORGANISATION_REF,LY_TURNOVER,LY_PROFIT,LY_REMUN,LY_EMPLOYEES,PY_TURNOVER,PY_PROFIT,PY_REMUN,PY_EMPLOYEES,UK_TURNOVER,UK_PROFIT,UK_REMUN,UK_EMPLOYEES,CREATED_BY,
    CREATE_TIMESTAMP,UPDATED_BY,UPDATE_TIMESTAMP,PY_POT_SUB,LY_POT_SUB,LY_DATE,PY_DATE,UK_DATE)
    VALUES(@ORGREF,NULL,NULL,NULL,@Ly_employees,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,null,GETDATE(),null,null,null,null,null,null,null)

    set @orgref=(select organisation_ref from DiTestDatabase.dbo.ORG_ACCOUNT where ORGANISATION_REF=@ORGREF)

        end

        alter table ditestdatabase.dbo.org_account enable trigger all       







SELECT @iNextCodeRowId = NULL

--Get the next iRowId.
SELECT @iNextcodeRowId = MIN(iorgrowId)
FROM #orgs
WHERE iorgrowId > @iCurrentcodeRowId

--Did we get a valid next row id?
IF ISNULL(@iNextCodeRowId,0) = 0


BEGIN
BREAK
END

SELECT @iCurrentcodeRowId = iorgrowId,
@ORGREF=ORG_REF,
@Ly_employees=Total_Leader
FROM #orgs
WHERE iorgRowId = @iNextcodeRowId

END

DROP TABLE #orgs

RETURN

解决方案:-由于我在表a和c之间没有任何共同之处,因此我不得不寻求表b的帮助,其中a和b之间唯一的共同之处是URN列,而b和c之间唯一的共同之处是Org_ref。我对更新进行了更改

SET LY_EMPLOYEES= (SELECT SP.TOTAL_LEADERSHIP_ROLE
            FROM DataConversion.dbo.School_plus_download_NI SP
            JOIN ORGANISATION ORG ON ORG.TOTAL_HEADCOUNT=SP.URN
            JOIN ORG_ACCOUNT OA ON OA.ORGANISATION_REF=ORG.ORGANISATION_REF
            WHERE sp.URN = @urn) --@ORGREF)

            FROM ORG_ACCOUNT WHERE ORGANISATION_REF=@ORG_REF

解决了这个问题。

谢谢

4 个答案:

答案 0 :(得分:1)

这有帮助吗??

    CREATE TABLE #a(urn bigint,  total_employe bigint)

    CREATE TABLE #b(urn bigint,org_ref bigint)

    CREATE TABLE #c(org_ref bigint,total_employe bigint)

    INSERT INTO #a
    SELECT 333,25 Union ALL
    SELECT 123,26 Union ALL
    SELECT 21,2 Union ALL
    SELECT 11,23




    INSERT INTO #b
    SELECT 333,1234 Union ALL
    SELECT 123,343 Union ALL
    SELECT 21,2 Union ALL
    SELECT 11,23 


    INSERT INTO #c
    SELECT 1234,0 Union ALL
    SELECT 343,0



    ;with cte
    AS
    (
    SELECT b.org_ref,a.total_employe from #a a
    INNER JOIN #b b on a.urn=b.urn
    )


    MERGE INTO #c c
    USING cte  on c.org_ref=cte.org_ref 
    WHEN MATCHED THEN
    UPDATE
    SET c.total_employe=cte.total_employe

    WHEN NOT MATCHED THEN
    INSERT(org_ref,total_employe)
    VALUES(org_ref,total_employe);

    SELECT * FROM #c


    DROP TABLE #a
    DROP TABLE #b
    DROP TABLE #c

答案 1 :(得分:0)

请勿使用while循环或游标进行简单的upsert操作!您会在网络上到处发现它适得其反!

我将以表a / b的简单形式回答:

-- First, update the ones that exist
update alias_b -- Note that you NEED an alias to update using join
set [total employee]=a.[total employee]
from a 
inner join b alias_b on a.org_ref=alias_b.org_ref

-- Then, insert the ones that don't
insert b
select (the columns you want)
from a
where not exists (select 1 from b where a.org_ref=b.org_ref)

答案 2 :(得分:0)

--UPDATE
UPDATE B SET B.TOTAL_LEADER = A.TOTAL_LEADER FROM TABLEA A INNER JOIN TABLEB B ON 
A.ORG_REF = B.ORG_REF
--INSERT
INSERT INTO TABLEB(ORG_REF,URN,TOTAL_LEADER)
SELECT A.ORG_REF,A.URN,A.TOTAL_LEADER FROM TABLEA A LEFT OUTER JOIN  TABLEB B ON 
A.ORG_REF = B.ORG_REF WHERE B.ORG_REF IS NULL

我认为您正在寻找这个。

答案 3 :(得分:0)

SELECT INTO怎么样?像这样:

SELECT b.org_ref, a.totalemployee INTO c FROM a LEFT JOIN b ON (a.urn = b.urn)