一个值得研究学习的SQL
  1 if
if 
2 exists
exists 
3 (
(
4 select *
select * 
5 from dbo.sysobjects
from dbo.sysobjects 
6 where id = object_id(N'[dbo].[t]') and OBJECTPROPERTY(id, N'IsUserTable') = 1
where id = object_id(N'[dbo].[t]') and OBJECTPROPERTY(id, N'IsUserTable') = 1
7 )
)
8 drop table [dbo].[t]
drop table [dbo].[t]
9 GO
GO
10
11 CREATE TABLE [dbo].[t] (
CREATE TABLE [dbo].[t] (
12 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [id] [int] IDENTITY (1, 1) NOT NULL ,
13 [lid] [nvarchar] (50) ,
 [lid] [nvarchar] (50) ,
14 [name] [nvarchar] (50) ,
 [name] [nvarchar] (50) ,
15 [type] [int] NOT NULL
 [type] [int] NOT NULL 
16 )
) 
17 GO
GO
18
19
20
21 insert into t (lid,[name],[type])
insert into t (lid,[name],[type])
22 select '11','城站火车站',0
select '11','城站火车站',0
23 union all select '11','葵巷建国路口',0
union all select '11','葵巷建国路口',0
24 union all select '11','菜市桥',0
union all select '11','菜市桥',0
25 union all select '11','潮鸣寺巷',0
union all select '11','潮鸣寺巷',0
26 union all select '11','宝善桥建国路口',0
union all select '11','宝善桥建国路口',0
27 union all select '11','宝善桥',0
union all select '11','宝善桥',0
28 union all select '11','市体育馆',0
union all select '11','市体育馆',0
29 union all select '11','武林广场',0
union all select '11','武林广场',0
30 union all select '11','武林门',0
union all select '11','武林门',0
31 union all select '11','武林们马塍路口',0
union all select '11','武林们马塍路口',0
32 union all select '11','八字桥',0
union all select '11','八字桥',0
33 union all select '11','浙大西溪校区',0
union all select '11','浙大西溪校区',0
34 union all select '11','庆丰村',0
union all select '11','庆丰村',0
35 union all select '11','教工路口',0
union all select '11','教工路口',0
36 union all select '11','花园新村',0
union all select '11','花园新村',0
37 union all select '11','浙江工商大学',0
union all select '11','浙江工商大学',0
38 union all select '11','电子科技大学',0
union all select '11','电子科技大学',0
39 union all select '11','翠苑新村',0
union all select '11','翠苑新村',0
40 union all select '57','大关小区',0
union all select '57','大关小区',0
41 union all select '57','通信市场',0
union all select '57','通信市场',0
42 union all select '57','德胜新村',0
union all select '57','德胜新村',0
43 union all select '57','潮王路口',0
union all select '57','潮王路口',0
44 union all select '57','朝晖五区',0
union all select '57','朝晖五区',0
45 union all select '57','朝晖三区',0
union all select '57','朝晖三区',0
46 union all select '57','西湖文化广场东',0
union all select '57','西湖文化广场东',0
47 union all select '57','武林广场',0
union all select '57','武林广场',0
48 union all select '57','武林小广场',0
union all select '57','武林小广场',0
49 union all select '57','半道红',0
union all select '57','半道红',0
50 union all select '57','文三路口',0
union all select '57','文三路口',0
51 union all select '57','上宁桥',0
union all select '57','上宁桥',0
52 union all select '57','花园新村',0
union all select '57','花园新村',0
53 union all select '57','浙江工商大学',0
union all select '57','浙江工商大学',0
54 union all select '57','文一路口',0
union all select '57','文一路口',0
55 union all select '57','教工路北口',0
union all select '57','教工路北口',0
56 union all select '57','大关桥西',0
union all select '57','大关桥西',0
57 union all select '57','上塘路口',0
union all select '57','上塘路口',0
58 union all select '57','大关西六苑',0
union all select '57','大关西六苑',0
59 union all select '57','香积寺路口',0
union all select '57','香积寺路口',0
60 union all select '57','大关小区',0
union all select '57','大关小区',0
61 union all select '14','武林小广场',0
union all select '14','武林小广场',0
62 union all select '14','昌化新村',0
union all select '14','昌化新村',0
63 union all select '14','长寿桥',0
union all select '14','长寿桥',0
64 union all select '14','延安路口',0
union all select '14','延安路口',0
65 union all select '14','中大广场',0
union all select '14','中大广场',0
66 union all select '14','众安桥',0
union all select '14','众安桥',0
67 union all select '14','浙一医院',0
union all select '14','浙一医院',0
68 union all select '14','大学路北口',0
union all select '14','大学路北口',0
69 union all select '14','庆春门',0
union all select '14','庆春门',0
70 union all select '14','金衙庄',0
union all select '14','金衙庄',0
71 union all select '14','总管塘',0
union all select '14','总管塘',0
72 union all select '14','华东家具市场',0
union all select '14','华东家具市场',0
73 union all select '14','近江村',0
union all select '14','近江村',0
74 union all select '14','汽车南站',0
union all select '14','汽车南站',0
75 union all select '14','汽车南站',1
union all select '14','汽车南站',1
76 union all select '14','近江村',1
union all select '14','近江村',1
77 union all select '14','华东家具市场',1
union all select '14','华东家具市场',1
78 union all select '14','总管塘',1
union all select '14','总管塘',1
79 union all select '14','金衙庄',1
union all select '14','金衙庄',1
80 union all select '14','庆春门',1
union all select '14','庆春门',1
81 union all select '14','大学路北口',1
union all select '14','大学路北口',1
82 union all select '14','浙一医院',1
union all select '14','浙一医院',1
83 union all select '14','众安桥',1
union all select '14','众安桥',1
84 union all select '14','中大广场',1
union all select '14','中大广场',1
85 union all select '14','延安路口',1
union all select '14','延安路口',1
86 union all select '14','长寿桥',1
union all select '14','长寿桥',1
87 union all select '14','昌化新村',1
union all select '14','昌化新村',1
88 union all select '14','武林小广场',1
union all select '14','武林小广场',1
89 union all select 'K105','火车东站',0
union all select 'K105','火车东站',0
90 union all select 'K105','汽车东站',0
union all select 'K105','汽车东站',0
91 union all select 'K105','严家弄',0
union all select 'K105','严家弄',0
92 union all select 'K105','景芳五区',0
union all select 'K105','景芳五区',0
93 union all select 'K105','景御路口',0
union all select 'K105','景御路口',0
94 union all select 'K105','庆春东路',0
union all select 'K105','庆春东路',0
95 union all select 'K105','采荷新村',0
union all select 'K105','采荷新村',0
96 union all select 'K105','观音塘小区',0
union all select 'K105','观音塘小区',0
97 union all select 'K105','总管塘',0
union all select 'K105','总管塘',0
98 union all select 'K105','章家桥',0
union all select 'K105','章家桥',0
99 union all select 'K105','浙二医院',0
union all select 'K105','浙二医院',0
100 union all select 'K105','官巷口',0
union all select 'K105','官巷口',0
101 union all select 'K105','湖滨',0
union all select 'K105','湖滨',0
102 union all select 'K105','胜利剧院',0
union all select 'K105','胜利剧院',0
103 union all select 'K105','孩儿巷',0
union all select 'K105','孩儿巷',0
104 union all select 'K105','延安新村',0
union all select 'K105','延安新村',0
105 union all select 'K105','武林小广场',0
union all select 'K105','武林小广场',0
106 union all select 'K105','杭州大厦',0
union all select 'K105','杭州大厦',0
107 union all select 'K105','中北桥',0
union all select 'K105','中北桥',0
108 union all select 'K105','施家桥',0
union all select 'K105','施家桥',0
109 union all select 'K105','建国北路文晖路口',0
union all select 'K105','建国北路文晖路口',0
110 union all select 'K105','文晖大桥东',0
union all select 'K105','文晖大桥东',0
111 union all select 'K105','机神村',0
union all select 'K105','机神村',0
112 union all select 'K105','天城路口',0
union all select 'K105','天城路口',0
113 union all select 'K105','新塘路口',0
union all select 'K105','新塘路口',0
114 union all select 'K105','火车东站',0
union all select 'K105','火车东站',0
115 union all select '39','闸口',0
union all select '39','闸口',0
116 union all select '39','水澄桥',0
union all select '39','水澄桥',0
117 union all select '39','海月桥',0
union all select '39','海月桥',0
118 union all select '39','美政桥',0
union all select '39','美政桥',0
119 union all select '39','复兴路北口',0
union all select '39','复兴路北口',0
120 union all select '39','三廊庙',0
union all select '39','三廊庙',0
121 union all select '39','木材新村',0
union all select '39','木材新村',0
122 union all select '39','二凉亭',0
union all select '39','二凉亭',0
123 union all select '39','望江门外',0
union all select '39','望江门外',0
124 union all select '39','汽车南站',0
union all select '39','汽车南站',0
125 union all select '39','近江村',0
union all select '39','近江村',0
126 union all select '39','华东家具市场',0
union all select '39','华东家具市场',0
127 union all select '39','总管塘',0
union all select '39','总管塘',0
128 union all select '39','城站火车站',0
union all select '39','城站火车站',0
129 union all select 'K101','城站火车站',0
union all select 'K101','城站火车站',0
130 union all select 'K101','总管塘',0
union all select 'K101','总管塘',0
131 union all select 'K101','观音塘小区',0
union all select 'K101','观音塘小区',0
132 union all select 'K101','采荷新村',0
union all select 'K101','采荷新村',0
133 union all select 'K101','红菱新村',0
union all select 'K101','红菱新村',0
134 union all select 'K101','凤起东路口',0
union all select 'K101','凤起东路口',0
135 union all select 'K101','双菱路北口',0
union all select 'K101','双菱路北口',0
136 union all select 'K101','市红会医院',0
union all select 'K101','市红会医院',0
137 union all select 'K101','建国路口',0
union all select 'K101','建国路口',0
138 union all select 'K101','新华路口',0
union all select 'K101','新华路口',0
139 union all select 'K101','中北路口',0
union all select 'K101','中北路口',0
140 union all select 'K101','延安路口',0
union all select 'K101','延安路口',0
141 union all select 'K101','浙大湖滨校区',0
union all select 'K101','浙大湖滨校区',0
142 union all select 'K101','昌化新村',0
union all select 'K101','昌化新村',0
143 union all select 'K101','市府大楼',0
union all select 'K101','市府大楼',0
144 union all select 'K101','武林门马塍路口',0
union all select 'K101','武林门马塍路口',0
145 union all select 'K101','八字桥',0
union all select 'K101','八字桥',0
146 union all select 'K101','浙大西溪校区',0
union all select 'K101','浙大西溪校区',0
147 union all select 'K101','庆丰村',0
union all select 'K101','庆丰村',0
148 union all select 'K101','玉古路天目山路口',0
union all select 'K101','玉古路天目山路口',0
149 union all select 'K101','西湖体育馆',0
union all select 'K101','西湖体育馆',0
150 union all select '21/K21','城站火车站',0
union all select '21/K21','城站火车站',0
151 union all select '21/K21','章家桥',0
union all select '21/K21','章家桥',0
152 union all select '21/K21','新城隧道东口',0
union all select '21/K21','新城隧道东口',0
153 union all select '21/K21','解放路秋涛路口',0
union all select '21/K21','解放路秋涛路口',0
154 union all select '21/K21','采荷新村',0
union all select '21/K21','采荷新村',0
155 union all select '21/K21','红菱新村',0
union all select '21/K21','红菱新村',0
156 union all select '21/K21','双菱路北口',0
union all select '21/K21','双菱路北口',0
157 union all select '21/K21','市红会医院',0
union all select '21/K21','市红会医院',0
158 union all select '21/K21','建国路口',0
union all select '21/K21','建国路口',0
159 union all select '21/K21','新华路口',0
union all select '21/K21','新华路口',0
160 union all select '21/K21','中北路口',0
union all select '21/K21','中北路口',0
161 union all select '21/K21','延安路口',0
union all select '21/K21','延安路口',0
162 union all select '21/K21','浙大湖滨校区',0
union all select '21/K21','浙大湖滨校区',0
163 union all select '21/K21','昌化新村',0
union all select '21/K21','昌化新村',0
164 union all select '21/K21','市府大楼',0
union all select '21/K21','市府大楼',0
165 union all select '21/K21','武林门马塍路口',0
union all select '21/K21','武林门马塍路口',0
166 union all select '21/K21','八字桥',0
union all select '21/K21','八字桥',0
167 union all select '21/K21','浙大西溪校区',0
union all select '21/K21','浙大西溪校区',0
168 union all select '21/K21','庆丰村',0
union all select '21/K21','庆丰村',0
169 union all select '21/K21','跑马场',0
union all select '21/K21','跑马场',0
170 union all select '21/K21','黄龙体育中心',0
union all select '21/K21','黄龙体育中心',0
171 union all select '21/K21','浙大附中',0
union all select '21/K21','浙大附中',0
172 union all select '21/K21','求是路',0
union all select '21/K21','求是路',0
173 union all select '21/K21','西湖体育馆',0
union all select '21/K21','西湖体育馆',0
174 union all select '58/K58','大关小区',0
union all select '58/K58','大关小区',0
175 union all select '58/K58','上塘路香积寺路口',0
union all select '58/K58','上塘路香积寺路口',0
176 union all select '58/K58','大关西六苑',0
union all select '58/K58','大关西六苑',0
177 union all select '58/K58','上塘路口',0
union all select '58/K58','上塘路口',0
178 union all select '58/K58','大关桥西',0
union all select '58/K58','大关桥西',0
179 union all select '58/K58','教工路北口',0
union all select '58/K58','教工路北口',0
180 union all select '58/K58','文一路口',0
union all select '58/K58','文一路口',0
181 union all select '58/K58','浙江工商大学',0
union all select '58/K58','浙江工商大学',0
182 union all select '58/K58','花园新村',0
union all select '58/K58','花园新村',0
183 union all select '58/K58','上宁桥',0
union all select '58/K58','上宁桥',0
184 union all select '58/K58','文三新村',0
union all select '58/K58','文三新村',0
185 union all select '58/K58','八字桥',0
union all select '58/K58','八字桥',0
186 union all select '58/K58','武林门马塍路口',0
union all select '58/K58','武林门马塍路口',0
187 union all select '58/K58','武林小广场',0
union all select '58/K58','武林小广场',0
188 union all select '58/K58','武林广场',0
union all select '58/K58','武林广场',0
189 union all select '58/K58','中北桥',0
union all select '58/K58','中北桥',0
190 union all select '58/K58','朝晖一区',0
union all select '58/K58','朝晖一区',0
191 union all select '58/K58','朝晖三区',0
union all select '58/K58','朝晖三区',0
192 union all select '58/K58','朝晖五区',0
union all select '58/K58','朝晖五区',0
193 union all select '58/K58','潮王路口',0
union all select '58/K58','潮王路口',0
194 union all select '58/K58','德胜新村',0
union all select '58/K58','德胜新村',0
195 union all select '58/K58','通信市场',0
union all select '58/K58','通信市场',0
196 union all select '58/K58','大关小区',0
union all select '58/K58','大关小区',0
197 union all select 'K101','西湖体育馆',1
union all select 'K101','西湖体育馆',1
198 union all select 'K101','玉古路天目山路口',1
union all select 'K101','玉古路天目山路口',1
199 union all select 'K101','庆丰村',1
union all select 'K101','庆丰村',1
200 union all select 'K101','浙大西溪校区',1
union all select 'K101','浙大西溪校区',1
201 union all select 'K101','八字桥',1
union all select 'K101','八字桥',1
202 union all select 'K101','武林门马塍路口',1
union all select 'K101','武林门马塍路口',1
203 union all select 'K101','市府大楼',1
union all select 'K101','市府大楼',1
204 union all select 'K101','昌化新村',1
union all select 'K101','昌化新村',1
205 union all select 'K101','浙大湖滨校区',1
union all select 'K101','浙大湖滨校区',1
206 union all select 'K101','延安路口',1
union all select 'K101','延安路口',1
207 union all select 'K101','中北路口',1
union all select 'K101','中北路口',1
208 union all select 'K101','新华路口',1
union all select 'K101','新华路口',1
209 union all select 'K101','建国路口',1
union all select 'K101','建国路口',1
210 union all select 'K101','市红会医院',1
union all select 'K101','市红会医院',1
211 union all select 'K101','双菱路北口',1
union all select 'K101','双菱路北口',1
212 union all select 'K101','凤起东路口',1
union all select 'K101','凤起东路口',1
213 union all select 'K101','红菱新村',1
union all select 'K101','红菱新村',1
214 union all select 'K101','采荷新村',1
union all select 'K101','采荷新村',1
215 union all select 'K101','观音塘小区',1
union all select 'K101','观音塘小区',1
216 union all select 'K101','总管塘',1
union all select 'K101','总管塘',1
217 union all select 'K101','城站火车站',1
union all select 'K101','城站火车站',1
218
219
220
221
222 /****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/
/****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/
223 if
if 
224 exists
exists 
225 (
(
226 select *
select * 
227 from dbo.sysobjects
from dbo.sysobjects 
228 where id = object_id(N'[dbo].[Search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1
where id = object_id(N'[dbo].[Search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1
229 )
)
230 drop procedure [dbo].[Search]
drop procedure [dbo].[Search]
231
232 GO
GO
233
234 SET QUOTED_IDENTIFIER OFF
SET QUOTED_IDENTIFIER OFF 
235 GO
GO
236 SET ANSI_NULLS ON
SET ANSI_NULLS ON 
237 GO
GO
238
239
240
241 /****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/
/****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/
242 CREATE proc Search
CREATE proc Search
243 @name1 nvarchar(50),
@name1 nvarchar(50),
244 @name2 nvarchar(50)
@name2 nvarchar(50)
245 as
as
246
247 --中转站
--中转站
248 create table #tmp
create table #tmp
249 (
(
250 tmp_id int identity(1,1),
 tmp_id int identity(1,1),
251 tmp_name NVARCHAR(50)
 tmp_name NVARCHAR(50)
252 )
)
253
254 --站点队列
--站点队列
255 create table #tmp1
create table #tmp1
256 (
(
257 tmp1_id int identity(1,1),
 tmp1_id int identity(1,1),
258 tmp1_name NVARCHAR(50)
 tmp1_name NVARCHAR(50)
259 )
)
260
261
262 --查找结果
--查找结果
263 create table #result
create table #result
264 (
(
265 r_id int,
 r_id int,
266 r_lid nvarchar(50),
 r_lid nvarchar(50),
267 r_name nvarchar(50),
 r_name nvarchar(50),
268 r_type int
 r_type int
269 )
)
270
271 --直达
--直达
272 insert into #result
insert into #result
273 select c.*  from t a,t b,t c where
select c.*  from t a,t b,t c where 
274 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 
275 and a.[name] = @name1 and b.[name] = @name2
and a.[name] = @name1 and b.[name] = @name2
276 and c.id>=a.id and c.id<=b.id order by c.id
and c.id>=a.id and c.id<=b.id order by c.id
277
278 if @@rowcount>0 begin
if @@rowcount>0 begin
279 select * from #result
 select * from #result
280 end
end
281 else begin
else begin
282 --换车
 --换车
283 DECLARE @CurrenName NVARCHAR(50)
 DECLARE @CurrenName NVARCHAR(50)
284 SET @CurrenName = @name1
 SET @CurrenName = @name1
285 change:
 change:
286 /*
  /*
287 --车次入栈
  --车次入栈
288 insert into #tmp (tmp_lid)
  insert into #tmp (tmp_lid)
289 select distinct lid from t where [name] = @CurrenName
  select distinct lid from t where [name] = @CurrenName
290 DECLARE @CurrenBus NVARCHAR(50)
  DECLARE @CurrenBus NVARCHAR(50)
291 SELECT TOP 1 @CurrenBus = tmp_lid FROM #tmp
  SELECT TOP 1 @CurrenBus = tmp_lid FROM #tmp
292 */
  */
293 INSERT INTO #tmp1 (tmp1_name)
  INSERT INTO #tmp1 (tmp1_name)
294 SELECT DISTINCT b.[name] FROM t a,t b WHERE a.[name] = @CurrenName AND b.lid = a.lid AND b.[name] <> @CurrenName
  SELECT DISTINCT b.[name] FROM t a,t b WHERE a.[name] = @CurrenName AND b.lid = a.lid AND b.[name] <> @CurrenName
295
296 INSERT INTO #tmp (tmp_name)
  INSERT INTO #tmp (tmp_name)
297 select d.[tmp1_name]  from t a,t b,t c, #tmp1 d where
  select d.[tmp1_name]  from t a,t b,t c, #tmp1 d where 
298 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
  a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 
299 and a.[name] = d.[tmp1_name] and b.[name] = @name2
  and a.[name] = d.[tmp1_name] and b.[name] = @name2
300 and c.id>=a.id and c.id<=b.id
  and c.id>=a.id and c.id<=b.id
301
302 IF @@rowcount>0 BEGIN
  IF @@rowcount>0 BEGIN
303 select distinct c.*  from t a,t b,t c,#tmp d where
   select distinct c.*  from t a,t b,t c,#tmp d where 
304 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
   a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 
305 and a.[name] = @name1 and b.[name] = d.tmp_name
   and a.[name] = @name1 and b.[name] = d.tmp_name
306 and c.id>=a.id and c.id<=b.id order by c.id
   and c.id>=a.id and c.id<=b.id order by c.id
307 
   
308 select distinct c.*  from t a,t b,t c,#tmp d where
   select distinct c.*  from t a,t b,t c,#tmp d where 
309 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
   a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 
310 and a.[name] = d.tmp_name and b.[name] = @name2
   and a.[name] = d.tmp_name and b.[name] = @name2
311 and c.id>=a.id and c.id<=b.id order by c.id
   and c.id>=a.id and c.id<=b.id order by c.id
312 END
  END
313 --SELECT * FROM #tmp
  --SELECT * FROM #tmp
314 end
end
315
316 drop table #result
drop table #result
317 drop table #tmp1
drop table #tmp1
318 drop table #tmp
drop table #tmp
319 GO
GO
320
321 SET QUOTED_IDENTIFIER OFF
SET QUOTED_IDENTIFIER OFF 
322 GO
GO
323 SET ANSI_NULLS ON
SET ANSI_NULLS ON 
324 GO
GO
325
326 exec search '文一路口','总管塘'
exec search '文一路口','总管塘'
327
328
329 /*交集操作*/
/*交集操作*/
330 select distinct(lid) from t where type=0 and
select distinct(lid) from t where type=0 and 
331 lid in
lid in 
332 (
(
333 select distinct(lid) from t where type=1
select distinct(lid) from t where type=1
334 )
)
335 /*并操作*/
/*并操作*/
336 select distinct(lid) from t where type=0
select distinct(lid) from t where type=0
337 union
union
338 select distinct(lid) from t where type=1
select distinct(lid) from t where type=1
339 /*差操作*/
/*差操作*/
340 select distinct(lid) from t where type=0 and
select distinct(lid) from t where type=0 and
341 lid not in
lid not in 
342 (
(
343 select distinct(lid) from t where type=1
select distinct(lid) from t where type=1
344 )
)
345
346
347
348
349
350
351
 if
if 2
 exists
exists 3
 (
(4
 select *
select * 5
 from dbo.sysobjects
from dbo.sysobjects 6
 where id = object_id(N'[dbo].[t]') and OBJECTPROPERTY(id, N'IsUserTable') = 1
where id = object_id(N'[dbo].[t]') and OBJECTPROPERTY(id, N'IsUserTable') = 17
 )
)8
 drop table [dbo].[t]
drop table [dbo].[t]9
 GO
GO10

11
 CREATE TABLE [dbo].[t] (
CREATE TABLE [dbo].[t] (12
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [id] [int] IDENTITY (1, 1) NOT NULL ,13
 [lid] [nvarchar] (50) ,
 [lid] [nvarchar] (50) ,14
 [name] [nvarchar] (50) ,
 [name] [nvarchar] (50) ,15
 [type] [int] NOT NULL
 [type] [int] NOT NULL 16
 )
) 17
 GO
GO18

19

20

21
 insert into t (lid,[name],[type])
insert into t (lid,[name],[type])22
 select '11','城站火车站',0
select '11','城站火车站',023
 union all select '11','葵巷建国路口',0
union all select '11','葵巷建国路口',024
 union all select '11','菜市桥',0
union all select '11','菜市桥',025
 union all select '11','潮鸣寺巷',0
union all select '11','潮鸣寺巷',026
 union all select '11','宝善桥建国路口',0
union all select '11','宝善桥建国路口',027
 union all select '11','宝善桥',0
union all select '11','宝善桥',028
 union all select '11','市体育馆',0
union all select '11','市体育馆',029
 union all select '11','武林广场',0
union all select '11','武林广场',030
 union all select '11','武林门',0
union all select '11','武林门',031
 union all select '11','武林们马塍路口',0
union all select '11','武林们马塍路口',032
 union all select '11','八字桥',0
union all select '11','八字桥',033
 union all select '11','浙大西溪校区',0
union all select '11','浙大西溪校区',034
 union all select '11','庆丰村',0
union all select '11','庆丰村',035
 union all select '11','教工路口',0
union all select '11','教工路口',036
 union all select '11','花园新村',0
union all select '11','花园新村',037
 union all select '11','浙江工商大学',0
union all select '11','浙江工商大学',038
 union all select '11','电子科技大学',0
union all select '11','电子科技大学',039
 union all select '11','翠苑新村',0
union all select '11','翠苑新村',040
 union all select '57','大关小区',0
union all select '57','大关小区',041
 union all select '57','通信市场',0
union all select '57','通信市场',042
 union all select '57','德胜新村',0
union all select '57','德胜新村',043
 union all select '57','潮王路口',0
union all select '57','潮王路口',044
 union all select '57','朝晖五区',0
union all select '57','朝晖五区',045
 union all select '57','朝晖三区',0
union all select '57','朝晖三区',046
 union all select '57','西湖文化广场东',0
union all select '57','西湖文化广场东',047
 union all select '57','武林广场',0
union all select '57','武林广场',048
 union all select '57','武林小广场',0
union all select '57','武林小广场',049
 union all select '57','半道红',0
union all select '57','半道红',050
 union all select '57','文三路口',0
union all select '57','文三路口',051
 union all select '57','上宁桥',0
union all select '57','上宁桥',052
 union all select '57','花园新村',0
union all select '57','花园新村',053
 union all select '57','浙江工商大学',0
union all select '57','浙江工商大学',054
 union all select '57','文一路口',0
union all select '57','文一路口',055
 union all select '57','教工路北口',0
union all select '57','教工路北口',056
 union all select '57','大关桥西',0
union all select '57','大关桥西',057
 union all select '57','上塘路口',0
union all select '57','上塘路口',058
 union all select '57','大关西六苑',0
union all select '57','大关西六苑',059
 union all select '57','香积寺路口',0
union all select '57','香积寺路口',060
 union all select '57','大关小区',0
union all select '57','大关小区',061
 union all select '14','武林小广场',0
union all select '14','武林小广场',062
 union all select '14','昌化新村',0
union all select '14','昌化新村',063
 union all select '14','长寿桥',0
union all select '14','长寿桥',064
 union all select '14','延安路口',0
union all select '14','延安路口',065
 union all select '14','中大广场',0
union all select '14','中大广场',066
 union all select '14','众安桥',0
union all select '14','众安桥',067
 union all select '14','浙一医院',0
union all select '14','浙一医院',068
 union all select '14','大学路北口',0
union all select '14','大学路北口',069
 union all select '14','庆春门',0
union all select '14','庆春门',070
 union all select '14','金衙庄',0
union all select '14','金衙庄',071
 union all select '14','总管塘',0
union all select '14','总管塘',072
 union all select '14','华东家具市场',0
union all select '14','华东家具市场',073
 union all select '14','近江村',0
union all select '14','近江村',074
 union all select '14','汽车南站',0
union all select '14','汽车南站',075
 union all select '14','汽车南站',1
union all select '14','汽车南站',176
 union all select '14','近江村',1
union all select '14','近江村',177
 union all select '14','华东家具市场',1
union all select '14','华东家具市场',178
 union all select '14','总管塘',1
union all select '14','总管塘',179
 union all select '14','金衙庄',1
union all select '14','金衙庄',180
 union all select '14','庆春门',1
union all select '14','庆春门',181
 union all select '14','大学路北口',1
union all select '14','大学路北口',182
 union all select '14','浙一医院',1
union all select '14','浙一医院',183
 union all select '14','众安桥',1
union all select '14','众安桥',184
 union all select '14','中大广场',1
union all select '14','中大广场',185
 union all select '14','延安路口',1
union all select '14','延安路口',186
 union all select '14','长寿桥',1
union all select '14','长寿桥',187
 union all select '14','昌化新村',1
union all select '14','昌化新村',188
 union all select '14','武林小广场',1
union all select '14','武林小广场',189
 union all select 'K105','火车东站',0
union all select 'K105','火车东站',090
 union all select 'K105','汽车东站',0
union all select 'K105','汽车东站',091
 union all select 'K105','严家弄',0
union all select 'K105','严家弄',092
 union all select 'K105','景芳五区',0
union all select 'K105','景芳五区',093
 union all select 'K105','景御路口',0
union all select 'K105','景御路口',094
 union all select 'K105','庆春东路',0
union all select 'K105','庆春东路',095
 union all select 'K105','采荷新村',0
union all select 'K105','采荷新村',096
 union all select 'K105','观音塘小区',0
union all select 'K105','观音塘小区',097
 union all select 'K105','总管塘',0
union all select 'K105','总管塘',098
 union all select 'K105','章家桥',0
union all select 'K105','章家桥',099
 union all select 'K105','浙二医院',0
union all select 'K105','浙二医院',0100
 union all select 'K105','官巷口',0
union all select 'K105','官巷口',0101
 union all select 'K105','湖滨',0
union all select 'K105','湖滨',0102
 union all select 'K105','胜利剧院',0
union all select 'K105','胜利剧院',0103
 union all select 'K105','孩儿巷',0
union all select 'K105','孩儿巷',0104
 union all select 'K105','延安新村',0
union all select 'K105','延安新村',0105
 union all select 'K105','武林小广场',0
union all select 'K105','武林小广场',0106
 union all select 'K105','杭州大厦',0
union all select 'K105','杭州大厦',0107
 union all select 'K105','中北桥',0
union all select 'K105','中北桥',0108
 union all select 'K105','施家桥',0
union all select 'K105','施家桥',0109
 union all select 'K105','建国北路文晖路口',0
union all select 'K105','建国北路文晖路口',0110
 union all select 'K105','文晖大桥东',0
union all select 'K105','文晖大桥东',0111
 union all select 'K105','机神村',0
union all select 'K105','机神村',0112
 union all select 'K105','天城路口',0
union all select 'K105','天城路口',0113
 union all select 'K105','新塘路口',0
union all select 'K105','新塘路口',0114
 union all select 'K105','火车东站',0
union all select 'K105','火车东站',0115
 union all select '39','闸口',0
union all select '39','闸口',0116
 union all select '39','水澄桥',0
union all select '39','水澄桥',0117
 union all select '39','海月桥',0
union all select '39','海月桥',0118
 union all select '39','美政桥',0
union all select '39','美政桥',0119
 union all select '39','复兴路北口',0
union all select '39','复兴路北口',0120
 union all select '39','三廊庙',0
union all select '39','三廊庙',0121
 union all select '39','木材新村',0
union all select '39','木材新村',0122
 union all select '39','二凉亭',0
union all select '39','二凉亭',0123
 union all select '39','望江门外',0
union all select '39','望江门外',0124
 union all select '39','汽车南站',0
union all select '39','汽车南站',0125
 union all select '39','近江村',0
union all select '39','近江村',0126
 union all select '39','华东家具市场',0
union all select '39','华东家具市场',0127
 union all select '39','总管塘',0
union all select '39','总管塘',0128
 union all select '39','城站火车站',0
union all select '39','城站火车站',0129
 union all select 'K101','城站火车站',0
union all select 'K101','城站火车站',0130
 union all select 'K101','总管塘',0
union all select 'K101','总管塘',0131
 union all select 'K101','观音塘小区',0
union all select 'K101','观音塘小区',0132
 union all select 'K101','采荷新村',0
union all select 'K101','采荷新村',0133
 union all select 'K101','红菱新村',0
union all select 'K101','红菱新村',0134
 union all select 'K101','凤起东路口',0
union all select 'K101','凤起东路口',0135
 union all select 'K101','双菱路北口',0
union all select 'K101','双菱路北口',0136
 union all select 'K101','市红会医院',0
union all select 'K101','市红会医院',0137
 union all select 'K101','建国路口',0
union all select 'K101','建国路口',0138
 union all select 'K101','新华路口',0
union all select 'K101','新华路口',0139
 union all select 'K101','中北路口',0
union all select 'K101','中北路口',0140
 union all select 'K101','延安路口',0
union all select 'K101','延安路口',0141
 union all select 'K101','浙大湖滨校区',0
union all select 'K101','浙大湖滨校区',0142
 union all select 'K101','昌化新村',0
union all select 'K101','昌化新村',0143
 union all select 'K101','市府大楼',0
union all select 'K101','市府大楼',0144
 union all select 'K101','武林门马塍路口',0
union all select 'K101','武林门马塍路口',0145
 union all select 'K101','八字桥',0
union all select 'K101','八字桥',0146
 union all select 'K101','浙大西溪校区',0
union all select 'K101','浙大西溪校区',0147
 union all select 'K101','庆丰村',0
union all select 'K101','庆丰村',0148
 union all select 'K101','玉古路天目山路口',0
union all select 'K101','玉古路天目山路口',0149
 union all select 'K101','西湖体育馆',0
union all select 'K101','西湖体育馆',0150
 union all select '21/K21','城站火车站',0
union all select '21/K21','城站火车站',0151
 union all select '21/K21','章家桥',0
union all select '21/K21','章家桥',0152
 union all select '21/K21','新城隧道东口',0
union all select '21/K21','新城隧道东口',0153
 union all select '21/K21','解放路秋涛路口',0
union all select '21/K21','解放路秋涛路口',0154
 union all select '21/K21','采荷新村',0
union all select '21/K21','采荷新村',0155
 union all select '21/K21','红菱新村',0
union all select '21/K21','红菱新村',0156
 union all select '21/K21','双菱路北口',0
union all select '21/K21','双菱路北口',0157
 union all select '21/K21','市红会医院',0
union all select '21/K21','市红会医院',0158
 union all select '21/K21','建国路口',0
union all select '21/K21','建国路口',0159
 union all select '21/K21','新华路口',0
union all select '21/K21','新华路口',0160
 union all select '21/K21','中北路口',0
union all select '21/K21','中北路口',0161
 union all select '21/K21','延安路口',0
union all select '21/K21','延安路口',0162
 union all select '21/K21','浙大湖滨校区',0
union all select '21/K21','浙大湖滨校区',0163
 union all select '21/K21','昌化新村',0
union all select '21/K21','昌化新村',0164
 union all select '21/K21','市府大楼',0
union all select '21/K21','市府大楼',0165
 union all select '21/K21','武林门马塍路口',0
union all select '21/K21','武林门马塍路口',0166
 union all select '21/K21','八字桥',0
union all select '21/K21','八字桥',0167
 union all select '21/K21','浙大西溪校区',0
union all select '21/K21','浙大西溪校区',0168
 union all select '21/K21','庆丰村',0
union all select '21/K21','庆丰村',0169
 union all select '21/K21','跑马场',0
union all select '21/K21','跑马场',0170
 union all select '21/K21','黄龙体育中心',0
union all select '21/K21','黄龙体育中心',0171
 union all select '21/K21','浙大附中',0
union all select '21/K21','浙大附中',0172
 union all select '21/K21','求是路',0
union all select '21/K21','求是路',0173
 union all select '21/K21','西湖体育馆',0
union all select '21/K21','西湖体育馆',0174
 union all select '58/K58','大关小区',0
union all select '58/K58','大关小区',0175
 union all select '58/K58','上塘路香积寺路口',0
union all select '58/K58','上塘路香积寺路口',0176
 union all select '58/K58','大关西六苑',0
union all select '58/K58','大关西六苑',0177
 union all select '58/K58','上塘路口',0
union all select '58/K58','上塘路口',0178
 union all select '58/K58','大关桥西',0
union all select '58/K58','大关桥西',0179
 union all select '58/K58','教工路北口',0
union all select '58/K58','教工路北口',0180
 union all select '58/K58','文一路口',0
union all select '58/K58','文一路口',0181
 union all select '58/K58','浙江工商大学',0
union all select '58/K58','浙江工商大学',0182
 union all select '58/K58','花园新村',0
union all select '58/K58','花园新村',0183
 union all select '58/K58','上宁桥',0
union all select '58/K58','上宁桥',0184
 union all select '58/K58','文三新村',0
union all select '58/K58','文三新村',0185
 union all select '58/K58','八字桥',0
union all select '58/K58','八字桥',0186
 union all select '58/K58','武林门马塍路口',0
union all select '58/K58','武林门马塍路口',0187
 union all select '58/K58','武林小广场',0
union all select '58/K58','武林小广场',0188
 union all select '58/K58','武林广场',0
union all select '58/K58','武林广场',0189
 union all select '58/K58','中北桥',0
union all select '58/K58','中北桥',0190
 union all select '58/K58','朝晖一区',0
union all select '58/K58','朝晖一区',0191
 union all select '58/K58','朝晖三区',0
union all select '58/K58','朝晖三区',0192
 union all select '58/K58','朝晖五区',0
union all select '58/K58','朝晖五区',0193
 union all select '58/K58','潮王路口',0
union all select '58/K58','潮王路口',0194
 union all select '58/K58','德胜新村',0
union all select '58/K58','德胜新村',0195
 union all select '58/K58','通信市场',0
union all select '58/K58','通信市场',0196
 union all select '58/K58','大关小区',0
union all select '58/K58','大关小区',0197
 union all select 'K101','西湖体育馆',1
union all select 'K101','西湖体育馆',1198
 union all select 'K101','玉古路天目山路口',1
union all select 'K101','玉古路天目山路口',1199
 union all select 'K101','庆丰村',1
union all select 'K101','庆丰村',1200
 union all select 'K101','浙大西溪校区',1
union all select 'K101','浙大西溪校区',1201
 union all select 'K101','八字桥',1
union all select 'K101','八字桥',1202
 union all select 'K101','武林门马塍路口',1
union all select 'K101','武林门马塍路口',1203
 union all select 'K101','市府大楼',1
union all select 'K101','市府大楼',1204
 union all select 'K101','昌化新村',1
union all select 'K101','昌化新村',1205
 union all select 'K101','浙大湖滨校区',1
union all select 'K101','浙大湖滨校区',1206
 union all select 'K101','延安路口',1
union all select 'K101','延安路口',1207
 union all select 'K101','中北路口',1
union all select 'K101','中北路口',1208
 union all select 'K101','新华路口',1
union all select 'K101','新华路口',1209
 union all select 'K101','建国路口',1
union all select 'K101','建国路口',1210
 union all select 'K101','市红会医院',1
union all select 'K101','市红会医院',1211
 union all select 'K101','双菱路北口',1
union all select 'K101','双菱路北口',1212
 union all select 'K101','凤起东路口',1
union all select 'K101','凤起东路口',1213
 union all select 'K101','红菱新村',1
union all select 'K101','红菱新村',1214
 union all select 'K101','采荷新村',1
union all select 'K101','采荷新村',1215
 union all select 'K101','观音塘小区',1
union all select 'K101','观音塘小区',1216
 union all select 'K101','总管塘',1
union all select 'K101','总管塘',1217
 union all select 'K101','城站火车站',1
union all select 'K101','城站火车站',1218

219

220

221

222
 /****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/
/****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/223
 if
if 224
 exists
exists 225
 (
(226
 select *
select * 227
 from dbo.sysobjects
from dbo.sysobjects 228
 where id = object_id(N'[dbo].[Search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1
where id = object_id(N'[dbo].[Search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1229
 )
)230
 drop procedure [dbo].[Search]
drop procedure [dbo].[Search]231

232
 GO
GO233

234
 SET QUOTED_IDENTIFIER OFF
SET QUOTED_IDENTIFIER OFF 235
 GO
GO236
 SET ANSI_NULLS ON
SET ANSI_NULLS ON 237
 GO
GO238

239

240

241
 /****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/
/****** Object:  Stored Procedure dbo.Search    Script Date: 2005-9-8 10:28:35 ******/242
 CREATE proc Search
CREATE proc Search243
 @name1 nvarchar(50),
@name1 nvarchar(50),244
 @name2 nvarchar(50)
@name2 nvarchar(50)245
 as
as246

247
 --中转站
--中转站248
 create table #tmp
create table #tmp249
 (
(250
 tmp_id int identity(1,1),
 tmp_id int identity(1,1),251
 tmp_name NVARCHAR(50)
 tmp_name NVARCHAR(50)252
 )
)253

254
 --站点队列
--站点队列255
 create table #tmp1
create table #tmp1256
 (
(257
 tmp1_id int identity(1,1),
 tmp1_id int identity(1,1),258
 tmp1_name NVARCHAR(50)
 tmp1_name NVARCHAR(50)259
 )
)260

261

262
 --查找结果
--查找结果263
 create table #result
create table #result264
 (
(265
 r_id int,
 r_id int,266
 r_lid nvarchar(50),
 r_lid nvarchar(50),267
 r_name nvarchar(50),
 r_name nvarchar(50),268
 r_type int
 r_type int269
 )
)270

271
 --直达
--直达272
 insert into #result
insert into #result273
 select c.*  from t a,t b,t c where
select c.*  from t a,t b,t c where 274
 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 275
 and a.[name] = @name1 and b.[name] = @name2
and a.[name] = @name1 and b.[name] = @name2276
 and c.id>=a.id and c.id<=b.id order by c.id
and c.id>=a.id and c.id<=b.id order by c.id277

278
 if @@rowcount>0 begin
if @@rowcount>0 begin279
 select * from #result
 select * from #result280
 end
end281
 else begin
else begin282
 --换车
 --换车283
 DECLARE @CurrenName NVARCHAR(50)
 DECLARE @CurrenName NVARCHAR(50)284
 SET @CurrenName = @name1
 SET @CurrenName = @name1285
 change:
 change:286
 /*
  /*287
 --车次入栈
  --车次入栈288
 insert into #tmp (tmp_lid)
  insert into #tmp (tmp_lid)289
 select distinct lid from t where [name] = @CurrenName
  select distinct lid from t where [name] = @CurrenName290
 DECLARE @CurrenBus NVARCHAR(50)
  DECLARE @CurrenBus NVARCHAR(50)291
 SELECT TOP 1 @CurrenBus = tmp_lid FROM #tmp
  SELECT TOP 1 @CurrenBus = tmp_lid FROM #tmp292
 */
  */293
 INSERT INTO #tmp1 (tmp1_name)
  INSERT INTO #tmp1 (tmp1_name)294
 SELECT DISTINCT b.[name] FROM t a,t b WHERE a.[name] = @CurrenName AND b.lid = a.lid AND b.[name] <> @CurrenName
  SELECT DISTINCT b.[name] FROM t a,t b WHERE a.[name] = @CurrenName AND b.lid = a.lid AND b.[name] <> @CurrenName295

296
 INSERT INTO #tmp (tmp_name)
  INSERT INTO #tmp (tmp_name)297
 select d.[tmp1_name]  from t a,t b,t c, #tmp1 d where
  select d.[tmp1_name]  from t a,t b,t c, #tmp1 d where 298
 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
  a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 299
 and a.[name] = d.[tmp1_name] and b.[name] = @name2
  and a.[name] = d.[tmp1_name] and b.[name] = @name2300
 and c.id>=a.id and c.id<=b.id
  and c.id>=a.id and c.id<=b.id301

302
 IF @@rowcount>0 BEGIN
  IF @@rowcount>0 BEGIN303
 select distinct c.*  from t a,t b,t c,#tmp d where
   select distinct c.*  from t a,t b,t c,#tmp d where 304
 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
   a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 305
 and a.[name] = @name1 and b.[name] = d.tmp_name
   and a.[name] = @name1 and b.[name] = d.tmp_name306
 and c.id>=a.id and c.id<=b.id order by c.id
   and c.id>=a.id and c.id<=b.id order by c.id307
 
   308
 select distinct c.*  from t a,t b,t c,#tmp d where
   select distinct c.*  from t a,t b,t c,#tmp d where 309
 a.lid=b.lid and a.[type]=b.[type] and a.id<b.id
   a.lid=b.lid and a.[type]=b.[type] and a.id<b.id 310
 and a.[name] = d.tmp_name and b.[name] = @name2
   and a.[name] = d.tmp_name and b.[name] = @name2311
 and c.id>=a.id and c.id<=b.id order by c.id
   and c.id>=a.id and c.id<=b.id order by c.id312
 END
  END313
 --SELECT * FROM #tmp
  --SELECT * FROM #tmp314
 end
end315

316
 drop table #result
drop table #result317
 drop table #tmp1
drop table #tmp1318
 drop table #tmp
drop table #tmp319
 GO
GO320

321
 SET QUOTED_IDENTIFIER OFF
SET QUOTED_IDENTIFIER OFF 322
 GO
GO323
 SET ANSI_NULLS ON
SET ANSI_NULLS ON 324
 GO
GO325

326
 exec search '文一路口','总管塘'
exec search '文一路口','总管塘'327

328

329
 /*交集操作*/
/*交集操作*/330
 select distinct(lid) from t where type=0 and
select distinct(lid) from t where type=0 and 331
 lid in
lid in 332
 (
(333
 select distinct(lid) from t where type=1
select distinct(lid) from t where type=1334
 )
)335
 /*并操作*/
/*并操作*/336
 select distinct(lid) from t where type=0
select distinct(lid) from t where type=0337
 union
union338
 select distinct(lid) from t where type=1
select distinct(lid) from t where type=1339
 /*差操作*/
/*差操作*/340
 select distinct(lid) from t where type=0 and
select distinct(lid) from t where type=0 and341
 lid not in
lid not in 342
 (
(343
 select distinct(lid) from t where type=1
select distinct(lid) from t where type=1344
 )
)345

346

347

348

349

350

351

 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号