IF OBJECT_ID('[ta]') IS NOT NULL DROP TABLE [ta] Go CREATE TABLE ta([id] INT,[name] NVARCHAR(4),[parentid] NVARCHAR(2)) Go INSERT INTO ta SELECT 1,'河北省','0' UNION ALL SELECT 2,'邢台市','1' UNION ALL SELECT 3,'石家庄市','1' UNION ALL SELECT …
比如表结构数据如下: Table:Tree ID Name ParentId 1 一级 0 2 二级 1 3 三级 2 4 四级 3 SQL SERVER 2005查询方法: //上查 with tmpTree as ( select * from Tree where Id=2 union all select p.* from tmpTree inner join Tree p on p.Id=tmpTree.ParentId ) select * from tmpTree //下查…
前言: 我们在开发中,常常遇到单表的子-父id级联的表结构,在树形的深度不确定的情况下,一次查询出某个树形结构下的所有具有子-父级关系的数据变得十分困难. 这时,我们使用oracle提供的CONNECT BY PRIOR ... start WITH 实现树形结构具有子父级关系的表递归查询和删除就会很方便. 1.树形结构表举例 (1)假设有一个MENU表,该表结构如下: ID VARCHAR2(32) N sys_guid() 节点id FENXID VARCHAR2(32)…
1.数据备份 ) ) ) )),'-','') ) SET @savePath = 'f:/DatabaseBackup/' DECLARE My_Cursor CURSOR FOR ( select name from sys.databases where name not like '%tmp%' and name not like '%temp%' and name not like '%master%' and name not like '%model%' and name not…