How can I use if statement after a CTE (SQL Server 2005)

前端 未结 4 679
无人共我
无人共我 2020-12-29 04:12

Last night I was writing a simple T-SQL program something like this

DECLARE @ROLEID AS INT

SELECT @ROLEID = [ROLE ID] FROM TBLROLE

;WITH CTE
AS
( 
    SELE         


        
4条回答
  •  梦毁少年i
    2020-12-29 04:29

    Common table expressions are defined within the context of a single statement:

    WITH cte_name AS (
      )
    ;
    

    So you can do something like:

    WITH CTE
    AS
    ( 
        SELECT * FROM SOMETABLE
    )
    SELECT * FROM CTE;
    

    or

    WITH CTE
    AS
    ( 
        SELECT * FROM SOMETABLE
    )
    UPDATE CTE 
    SET somefield = somevalue
    WHERE id = somekey;
    

    A CTE must be followed by a single SELECT, INSERT, UPDATE, MERGE, or DELETE statement that references some or all the CTE columns. A CTE can also be specified in a CREATE VIEW statement as part of the defining SELECT statement of the view

提交回复
热议问题