How to use exists on this sql statment ?

ahmed salah 3,216 Reputation points
2021-02-18T10:36:39.73+00:00

How to use Exists on this sql statement ?

update m set m.rohsstatus=RHst.Name from #ManuFacture m

                        inner JOIN Parts.ROHS Rhh WITH(NOLOCK) ON Rhh.ZPartID=m.PartID
            inner JOIN Nop_AcceptedValuesOption RHst WITH(NOLOCK) on RHst.AcceptedValuesOptionID=Rhh.RoHSStatus AND RHst.AcceptedValuesID=911

i need to update table ManuFacture where part id exist on Parts.ROHS

so i need it as below

update m set m.rohsstatus=RHst.Name from #ManuFacture m
where exists(select 1 from Parts.ROHS)

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-02-19T06:10:47.063+00:00

    Hi @ahmed salah ,

    Agreed with Tom,you could not do an UPDATE statement together with WHERE EXISTS table if you would like to update table ManuFacture where part id exist on Parts.ROHSsince since there is no data returns.

    The best way you could refer is using INNER JOIN.

    But you could use update ..WHERE EXISTS in below situations:

    One: update the column with another column (for example, 'name') in the same table.

    update m set m.rohsstatus=m.Name     
    from #ManuFacture m    
    where exists(select 1 from   Parts.ROHS Rhh WITH(NOLOCK)     
    inner JOIN Nop_AcceptedValuesOption RHst WITH(NOLOCK)     
    on RHst.AcceptedValuesOptionID=Rhh.RoHSStatus AND RHst.AcceptedValuesID=911    
    where m.partid=Rhh.zpartid )  
    

    Two: update the column with a fixed value(for example, 'Pass').

    update m set m.rohsstatus='Pass'    
    from #ManuFacture m    
    where exists(select 1 from   Parts.ROHS Rhh WITH(NOLOCK)     
    inner JOIN Nop_AcceptedValuesOption RHst WITH(NOLOCK)     
    on RHst.AcceptedValuesOptionID=Rhh.RoHSStatus AND RHst.AcceptedValuesID=911    
    where m.partid=Rhh.zpartid )   
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2021-02-18T11:23:14.533+00:00

    Next time please post table design as DDL, some sample data as DML and the expected result

    This here?

    update m 
    set m.rohsstatus=RHst.Name 
    from #ManuFacture m
    where exists( **select 1 from Parts.ROHS AS Rhh WHERE Rhh.ZPartID=m.PartI** )
    

  2. Olaf Helper 47,436 Reputation points
    2021-02-18T11:39:48.587+00:00

    Sorry, from this point on your post is only confusing.

    Again, please post table design as DDL, some sample data as DML and the expected result, without it's not really possible to assist you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.