一个值得研究学习的SQL

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

293  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
295
296  INSERT INTO #tmp (tmp_name)
297  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 
299  and a.[name] = d.[tmp1_name] and b.[name] = @name2
300  and c.id>=a.id and c.id<=b.id
301
302  IF @@rowcount>0 BEGIN
303   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 
305   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
307   
308   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 
310   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
312  END
313  --SELECT * FROM #tmp
314end
315
316drop table #result
317drop table #tmp1
318drop table #tmp
319GO
320
321SET QUOTED_IDENTIFIER OFF 
322GO
323SET ANSI_NULLS ON 
324GO
325
326exec search '文一路口','总管塘'
327
328
329/*交集操作*/
330select distinct(lid) from t where type=0 and 
331lid in 
332(
333select distinct(lid) from t where type=1
334)
335/*并操作*/
336select distinct(lid) from t where type=0
337union
338select distinct(lid) from t where type=1
339/*差操作*/
340select distinct(lid) from t where type=0 and
341lid not in 
342(
343select distinct(lid) from t where type=1
344)
345
346
347
348
349
350
351
posted @ 2005-10-26 15:58  jhtchina  阅读(404)  评论(0)    收藏  举报