感觉这个CTE递归查询蛮好用的,先举个例子: use City; go create table Tree ( ID int identity(1,1) primary key not null, Name varchar(20) not null, Parent varchar(20) null ) go insert Tree values('大学',null) insert Tree values('学院','大学') insert Tree values('计算机学院','学院') in…
1.删除所有表 select 'drop table '+name+';' from sys.tables where name like 'DataSyncV1DelaySample%' or name like 'DataSyncV2DelaySample%' 2.递归查询 使用关键字with as with temp ( [Id], [parentid]) as ( select Id, ParentId from SysLocation where ParentId = @ParentI…
USE dbNameGO/****** Object: StoredProcedure [dbo].[usp_Createjob] Script Date: 03/26/2014 14:36:30 ******/if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_Createjob]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure…
1.递归查询父分类下的各个子分类.表设计: SQL: --CTE 语句(适用于MSSQL2005以后版本) with cte_testNavi(Id,Name,Pid ) as ( --这是查询语句 SELECT Id,Name,Pid FROM Navi WHERE Name='汽车' union all --这是需要递归的部分,CTE 自身调用完成循环递归查找 SELECT a.Id,a.Name,a.Pid FROM Navi a INNER JOIN cte_testNavi b ON…
ROW_NUMBER() OVER()函数用法 with CTE as ( -->Begin 一个定位点成员 select ID, Name,Parent,cast(Name as nvarchar(max)) as TE, ROW_NUMBER()over(order by getdate()) as OrderID --最关键是上面这个字段,要获取排序字段,按字符串来排序. --其中窗口函数必须要使用o…
很多人可能想要查询整个树形表关联的内容都会通过循环递归来查...事实上在微软在SQL2005或以上版本就能用别的语法进行查询,下面是示例. --通过子节点查询父节点WITH TREE AS( SELECT * FROM Areas WHERE id = 6 -- 要查询的子 id UNION ALL SELECT Areas.* FROM Areas, TREE WHERE TREE.PId = Areas.Id ) SELECT Are…
USE [test] GO /****** Object: StoredProcedure [dbo].[wangchuang] Script Date: 2016/8/25 14:09:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <…