[Original] SQL with usage example

Source: Internet
Author: User
Declare @ city table (ID int identity (1, 1), name nvarchar (50), parentid int null) insert into @ cityvalues ('shanghai', null), ('jiangsu province ', null), ('zhejiang province, null), ('xuhui district ', 1), ('minhang district', 1), ('huangpu district ', 1), ('nanjing City ', 2), ('suzhou City ', 2), ('changzhou City', 2), ('hangzhou City ', 3), ('wenzhou City', 3), ('yuyao City ', 3) -- select * From @ city; with tempcity (ID, name, parentid, citylevel) as (select ID, name, parentid, 0 as citylevel from @ city where parentid is null Union all select C. ID, C. name, C. parentid, citylevel + 1 from @ City C join tempcity t on C. parentid = T. ID) Select * From tempcity

 

Example 2:

;WITH entities(entityId, setEntityFieldTaskTypeId, valueToSet)  AS (    SELECT         customerID,,        CAST('8' AS VARCHAR(20))    FROM         ADMIN.dbo.customers WITH(NOLOCK)    WHERE        customerID = 13255    UNION ALL    SELECT        cia.iproBrokerID,,        CAST('0' AS VARCHAR(20))    FROM        ADMIN.dbo.customerIproAccounts cia WITH(NOLOCK), entities e    WHERE        cia.customerID = e.entityId AND        e.setEntityFieldTaskTypeId = 4    UNION ALL    SELECT        bm.eProBrokerID,,        CAST('False' AS VARCHAR(20))    FROM        dbo.brokerMappings bm WITH(NOLOCK), entities e    WHERE        BM.iProBrokerID = e.entityId AND        e.setEntityFieldTaskTypeId = 3)--SELECT * FROM entitiesSELECT    st.*FROM    dbo.scheduledTask st WITH(NOLOCK)INNER JOIN    dbo.setEntityFieldTask seft WITH(NOLOCK)ON    st.scheduledTaskId = seft.scheduledTaskIdINNER JOIN    entities eON    e.entityId = seft.entityId AND    e.setEntityFieldTaskTypeId = seft.setEntityFieldTaskTypeId AND    e.valueToSet = seft.valueToSetWHERE    st.taskStatusId = 1

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.