How do I put an 'if clause' in an SQL string?

前端 未结 9 1392
误落风尘
误落风尘 2021-01-29 18:16

So here\'s what I want to do on my MySQL database.

I would like to do:

SELECT *
    FROM itemsOrdered
    WHERE purchaseOrder_ID = \'@purchaseOr         


        
9条回答
  •  梦毁少年i
    2021-01-29 18:59

    after sql server 2008 provide Merge to insert,update and delete operation based on single match statement, also that allows you to join. below sample example might be helps you.

    MERGE Target AS T
    USING Source AS S
    ON (T.EmployeeID = S.EmployeeID) 
    WHEN NOT MATCHED BY TARGET AND S.EmployeeName LIKE 'S%' 
        THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
    WHEN MATCHED 
        THEN UPDATE SET T.EmployeeName = S.EmployeeName
    WHEN NOT MATCHED BY SOURCE AND T.EmployeeName LIKE 'S%'
        THEN DELETE 
    OUTPUT $action, inserted.*, deleted.*;
    

    like this you can insert, update and delete in one statements.

    and for more information you can refer official documents on https://technet.microsoft.com/en-us/library/bb522522(v=sql.105).aspx

提交回复
热议问题