日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sql server教程sql存储过程和算法

發布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sql server教程sql存储过程和算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

sql 2005分頁存儲過程實例

/*
-------------寒冰制作---------------
------------------------------------
------------ QQ:81868164 ----------
--- email:hanbing81868164@163.com --
--- 由sql 2000的分頁存儲過來改造 ---
---- 加入sql 2005里新分頁技術 ----
-- 如有更好的實現方法可以自行修改 --
----------- 2006.7.11 --------------
- 版權共享 給我留言號記號就行 ^o^ -
------------------------------------
*/
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@OrderType bit = 0, -- 設置排序類型, 非 0 值則降序
@IsCount bit=0, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(3000) ='', -- 查詢條件 (注意: 不要加 where)
@StartIndex varchar(255),
@maxRows varchar(255)
AS

declare @strSQL varchar(3000) -- 主語句
declare @strCount varchar(3000) -- 返回數據的總條數語句
declare @strOrder varchar(3000) -- 排序類型語句

if @OrderType != 0
begin
set @strOrder = ' order by ' + @fldName +' desc'
end
else
begin
set @strOrder = ' order by ' + @fldName +' asc'
end

set @strSQL ='select * from (select *, ROW_NUMBER() OVER ('+ @strOrder +') as Pos from ' + @tblName + ') as T where T.Pos > ('+ @StartIndex +') and T.Pos <'+ @maxRows
set @strCount ='select count(*) as Total from ' + @tblName + ' as count'

if @strWhere !=''
begin
set @strSQL ='select * from (select *, ROW_NUMBER() OVER ('+ @strOrder +') as Pos from ' + @tblName + ' where ' + @strWhere + ') as T where T.Pos > ('+ @StartIndex +') and T.Pos <'+ @maxRows
set @strCount ='select count(*) as Total from ' + @tblName + ' as count where ' + @strWhere
end

if @IsCount!=0 ----按需要返回數據的總條數
begin
exec (@strCount)
end

exec (@strSQL)

?

?

代碼還在完善中

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[t]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[t]

go

create table [dbo].[t] (

[id] [int] identity (1, 1) not null ,

[lid] [nvarchar] (50) ,

[name] [nvarchar] (50) ,

[type] [int] not null

)

go

insert into t (lid,[name],[type])

select "11","城站火車站",0

union all select "11","葵巷建國路口",0

union all select "11","菜市橋",0

union all select "11","潮鳴寺巷",0

union all select "11","寶善橋建國路口",0

union all select "11","寶善橋",0

union all select "11","市體育館",0

union all select "11","武林廣場",0

union all select "11","武林門",0

union all select "11","武林們馬塍路口",0

union all select "11","八字橋",0

union all select "11","浙大西溪校區",0

union all select "11","慶豐村",0

union all select "11","教工路口",0

union all select "11","花園新村",0

union all select "11","浙江工商大學",0

union all select "11","電子科技大學",0

union all select "11","翠苑新村",0

union all select "57","大關小區",0

union all select "57","通信市場",0

union all select "57","德勝新村",0

union all select "57","潮王路口",0

union all select "57","朝暉五區",0

union all select "57","朝暉三區",0

union all select "57","西湖文化廣場東",0

union all select "57","武林廣場",0

union all select "57","武林小廣場",0

union all select "57","半道紅",0

union all select "57","文三路口",0

union all select "57","上寧橋",0

union all select "57","花園新村",0

union all select "57","浙江工商大學",0

union all select "57","文一路口",0

union all select "57","教工路北口",0

union all select "57","大關橋西",0

union all select "57","上塘路口",0

union all select "57","大關西六苑",0

union all select "57","香積寺路口",0

union all select "57","大關小區",0

union all select "14","武林小廣場",0

union all select "14","昌化新村",0

union all select "14","長壽橋",0

union all select "14","延安路口",0

union all select "14","中大廣場",0

union all select "14","眾安橋",0

union all select "14","浙一醫院",0

union all select "14","大學路北口",0

union all select "14","慶春門",0

union all select "14","金衙莊",0

union all select "14","總管塘",0

union all select "14","華東家具市場",0

union all select "14","近江村",0

union all select "14","汽車南站",0

union all select "14","汽車南站",1

union all select "14","近江村",1

union all select "14","華東家具市場",1

union all select "14","總管塘",1

union all select "14","金衙莊",1

union all select "14","慶春門",1

union all select "14","大學路北口",1

union all select "14","浙一醫院",1

union all select "14","眾安橋",1

union all select "14","中大廣場",1

union all select "14","延安路口",1

union all select "14","長壽橋",1

union all select "14","昌化新村",1

union all select "14","武林小廣場",1

union all select "k105","火車東站",0

union all select "k105","汽車東站",0

union all select "k105","嚴家弄",0

union all select "k105","景芳五區",0

union all select "k105","景御路口",0

union all select "k105","慶春東路",0

union all select "k105","采荷新村",0

union all select "k105","觀音塘小區",0

union all select "k105","總管塘",0

union all select "k105","章家橋",0

union all select "k105","浙二醫院",0

union all select "k105","官巷口",0

union all select "k105","湖濱",0

union all select "k105","勝利劇院",0

union all select "k105","孩兒巷",0

union all select "k105","延安新村",0

union all select "k105","武林小廣場",0

union all select "k105","杭州大廈",0

union all select "k105","中北橋",0

union all select "k105","施家橋",0

union all select "k105","建國北路文暉路口",0

union all select "k105","文暉大橋東",0

union all select "k105","機神村",0

union all select "k105","天城路口",0

union all select "k105","新塘路口",0

union all select "k105","火車東站",0

union all select "39","閘口",0

union all select "39","水澄橋",0

union all select "39","海月橋",0

union all select "39","美政橋",0

union all select "39","復興路北口",0

union all select "39","三廊廟",0

union all select "39","木材新村",0

union all select "39","二涼亭",0

union all select "39","望江門外",0

union all select "39","汽車南站",0

union all select "39","近江村",0

union all select "39","華東家具市場",0

union all select "39","總管塘",0

union all select "39","城站火車站",0

union all select "k101","城站火車站",0

union all select "k101","總管塘",0

union all select "k101","觀音塘小區",0

union all select "k101","采荷新村",0

union all select "k101","紅菱新村",0

union all select "k101","鳳起東路口",0

union all select "k101","雙菱路北口",0

union all select "k101","市紅會醫院",0

union all select "k101","建國路口",0

union all select "k101","新華路口",0

union all select "k101","中北路口",0

union all select "k101","延安路口",0

union all select "k101","浙大湖濱校區",0

union all select "k101","昌化新村",0

union all select "k101","市府大樓",0

union all select "k101","武林門馬塍路口",0

union all select "k101","八字橋",0

union all select "k101","浙大西溪校區",0

union all select "k101","慶豐村",0

union all select "k101","玉古路天目山路口",0

union all select "k101","西湖體育館",0

union all select "21/k21","城站火車站",0

union all select "21/k21","章家橋",0

union all select "21/k21","新城隧道東口",0

union all select "21/k21","解放路秋濤路口",0

union all select "21/k21","采荷新村",0

union all select "21/k21","紅菱新村",0

union all select "21/k21","雙菱路北口",0

union all select "21/k21","市紅會醫院",0

union all select "21/k21","建國路口",0

union all select "21/k21","新華路口",0

union all select "21/k21","中北路口",0

union all select "21/k21","延安路口",0

union all select "21/k21","浙大湖濱校區",0

union all select "21/k21","昌化新村",0

union all select "21/k21","市府大樓",0

union all select "21/k21","武林門馬塍路口",0

union all select "21/k21","八字橋",0

union all select "21/k21","浙大西溪校區",0

union all select "21/k21","慶豐村",0

union all select "21/k21","跑馬場",0

union all select "21/k21","黃龍體育中心",0

union all select "21/k21","浙大附中",0

union all select "21/k21","求是路",0

union all select "21/k21","西湖體育館",0

union all select "58/k58","大關小區",0

union all select "58/k58","上塘路香積寺路口",0

union all select "58/k58","大關西六苑",0

union all select "58/k58","上塘路口",0

union all select "58/k58","大關橋西",0

union all select "58/k58","教工路北口",0

union all select "58/k58","文一路口",0

union all select "58/k58","浙江工商大學",0

union all select "58/k58","花園新村",0

union all select "58/k58","上寧橋",0

union all select "58/k58","文三新村",0

union all select "58/k58","八字橋",0

union all select "58/k58","武林門馬塍路口",0

union all select "58/k58","武林小廣場",0

union all select "58/k58","武林廣場",0

union all select "58/k58","中北橋",0

union all select "58/k58","朝暉一區",0

union all select "58/k58","朝暉三區",0

union all select "58/k58","朝暉五區",0

union all select "58/k58","潮王路口",0

union all select "58/k58","德勝新村",0

union all select "58/k58","通信市場",0

union all select "58/k58","大關小區",0

union all select "k101","西湖體育館",1

union all select "k101","玉古路天目山路口",1

union all select "k101","慶豐村",1

union all select "k101","浙大西溪校區",1

union all select "k101","八字橋",1

union all select "k101","武林門馬塍路口",1

union all select "k101","市府大樓",1

union all select "k101","昌化新村",1

union all select "k101","浙大湖濱校區",1

union all select "k101","延安路口",1

union all select "k101","中北路口",1

union all select "k101","新華路口",1

union all select "k101","建國路口",1

union all select "k101","市紅會醫院",1

union all select "k101","雙菱路北口",1

union all select "k101","鳳起東路口",1

union all select "k101","紅菱新村",1

union all select "k101","采荷新村",1

union all select "k101","觀音塘小區",1

union all select "k101","總管塘",1

union all select "k101","城站火車站",1

/****** object: stored procedure dbo.search script date: 2005-9-8 10:28:35 ******/

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[search]') and objectproperty(id, n'isprocedure') = 1)

drop procedure [dbo].[search]

go

set quoted_identifier off

go

set ansi_nulls on

go

/****** object: stored procedure dbo.search script date: 2005-9-8 10:28:35 ******/

create proc search

@name1 nvarchar(50),

@name2 nvarchar(50)

as

--中轉站

create table #tmp

(

tmp_id int identity(1,1),

tmp_name nvarchar(50)

)

--站點隊列

create table #tmp1

(

tmp1_id int identity(1,1),

tmp1_name nvarchar(50)

)

--查找結果

create table #result

(

r_id int,

r_lid nvarchar(50),

r_name nvarchar(50),

r_type int

)

--直達

insert into #result

select c.* from t a,t b,t c where

a.lid=b.lid and a.[type]=b.[type] and a.idand a.[name] = @name1 and b.[name] = @name2

and c.id>=a.id and c.id<=b.id order by c.id

if @@rowcount>0 begin

select * from #result

end

else begin

--換車

declare @currenname nvarchar(50)

set @currenname = @name1

change:

/*

--車次入棧

insert into #tmp (tmp_lid)

select distinct lid from t where [name] = @currenname

declare @currenbus nvarchar(50)

select top 1 @currenbus = tmp_lid from #tmp

*/

insert into #tmp1 (tmp1_name)

select distinct b.[name] from t a,t b where a.[name] = @currenname and b.lid = a.lid and b.[name] <> @currenname

insert into #tmp (tmp_name)

select d.[tmp1_name] from t a,t b,t c, #tmp1 d where

a.lid=b.lid and a.[type]=b.[type] and a.id and a.[name] = d.[tmp1_name] and b.[name] = @name2

and c.id>=a.id and c.id<=b.id

if @@rowcount>0 begin

select distinct c.* from t a,t b,t c,#tmp d where

a.lid=b.lid and a.[type]=b.[type] and a.id and a.[name] = @name1 and b.[name] = d.tmp_name

and c.id>=a.id and c.id<=b.id order by c.id

select distinct c.* from t a,t b,t c,#tmp d where

a.lid=b.lid and a.[type]=b.[type] and a.id and a.[name] = d.tmp_name and b.[name] = @name2

and c.id>=a.id and c.id<=b.id order by c.id

end

--select * from #tmp

end

drop table #result

drop table #tmp1

drop table #tmp

go

set quoted_identifier off

go

set ansi_nulls on

go

exec search '文一路口','總管塘'

轉載于:https://www.cnblogs.com/Nina-piaoye/archive/2006/09/18/507231.html

總結

以上是生活随笔為你收集整理的sql server教程sql存储过程和算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。