日期:2014-05-17 浏览次数:20570 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-09 17:15:01
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[顾客表]
if object_id('[顾客表]') is not null drop table [顾客表]
go
create table [顾客表]([Cusno] int,[CusName] varchar(3),[Cus_deptId] int,[CusTel] varchar(3))
insert [顾客表]
select 1,'zjf',4,'13' union all
select 2,'xdx',6,'sss'
--> 测试数据:[部门表]
if object_id('[部门表]') is not null drop table [部门表]
go
create table [部门表]([DeptId] int,[Name_cn] varchar(6),[ParentId] int,[Phone] varchar(6))
insert [部门表]
select 1,'AA公司',0,'111111' union all
select 2,'研发',1,'22222' union all
select 3,'测试部',1,'333' union all
select 4,'测试1',3,'aaa' union all
select 5,'研发1',2,'啊啊' union all
select 6,'研发2',2,'bb' union all
select 7,'研发3',2,'ss' union all
select 8,'测试2',3,'ssss'
--------------开始查询--------------------------
;WITH cte AS (
select deptid,CONVERT(VARCHAR