分享 整理了一些t sql技巧(二)

2021-04-18 05:51:35 字數 4244 閱讀 5356

七、 分割槽檢視

分割槽檢視是提高查詢效能的乙個很好的辦法

--看下面的示例

--示例表

create table tempdb.dbo.t_10(

id int primary key check(id between 1 and 10),name varchar(10))

create table pubs.dbo.t_20(

id int primary key check(id between 11 and 20),name varchar(10))

create table northwind.dbo.t_30(

id int primary key check(id between 21 and 30),name varchar(10))

go --分割槽檢視

create view v_t

as select * from tempdb.dbo.t_10

union all

select * from pubs.dbo.t_20

union all

select * from northwind.dbo.t_30

go --插入資料

insert v_t select 1 ,'aa'

union  all select 2 ,'bb'

union  all select 11,'cc'

union  all select 12,'dd'

union  all select 21,'ee'

union  all select 22,'ff'

--更新資料

update v_t set name=name+'_更新' where right(id,1)=1

--刪除測試

delete from v_t where right(id,1)=2

--顯示結果

select * from v_t

go --刪除測試

drop table northwind.dbo.t_30,pubs.dbo.t_20,tempdb.dbo.t_10

drop view v_t

/**//*--測試結果

id          name     

----------- ----------

1          aa_更新

11          cc_更新

21          ee_更新

(所影響的行數為 3 行)

==*/

八、 樹型的實現

--參考

--樹形資料查詢示例

--示例資料

create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))

insert [tb] select 0,'中國'

union  all  select 0,'美國'

union  all  select 0,'加拿大'

union  all  select 1,'北京'

union  all  select 1,'上海'

union  all  select 1,'江蘇'

union  all  select 6,'蘇州'

union  all  select 7,'常熟'

union  all  select 6,'南京'

union  all  select 6,'無錫'

union  all  select 2,'紐約'

union  all  select 2,'舊金山'

go --查詢指定id的所有子

create function f_cid(

@id int

)returns @re table([id] int,[level] int)

as begin

declare @l int

set @l=0

insert @re select @id,@l

while @@rowcount>0

begin

set @l=@l+1

insert @re select a.[id],@l

from [tb] a,@re b

where a.[pid]=b.[id] and b.[level]=@l-1

end

/**//**//**//*--如果只顯示最明細的子(下面沒有子),則加上這個刪除

delete a from @re a

where exists(

select 1 from [tb] where [pid]=a.[id])

--*/

return

end

go --呼叫(查詢所有的子)

select a.*,層次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]

go --刪除測試

drop table [tb]

drop function f_cid

go 九、 排序問題

create table [t] (

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

[guid] [uniqueidentifier] null

) on [primary]

go 下面這句執行5次

insert t values (newid())

檢視執行結果

select * from t

1、 第一種

select * from t

order by case id when 4 then 1

when 5 then 2

when 1 then 3

when 2 then 4

when 3 then 5 end

2、 第二種

select * from t order by (id+2)%6

3、 第三種

select * from t order by charindex(cast(id as varchar),'45123')

4、 第四種

select * from t

where id between 0 and 5

order by charindex(cast(id as varchar),'45123')

5、 第五種

select * from t order by case when id >3 then id-5 else id end

6、 第六種

select * from t order by id / 4 desc,id asc

十、 一條語句刪除一批記錄

首先id列是int標識類型別,然後刪除id值為5,6,8,9,10,11的列,這裡的cast函式不能用convert函式代替,而且轉換的型別必須是varchar,而不能是char,否則就會執行出你不希望的結果,這裡的"5,6,8,9,10,11"可以是你在頁面上獲取的乙個chkboxlist構建成的值,然後用下面的一句就全部刪

除了,比迴圈用多條語句高效吧應該。

delete from [fujian] where charindex(','+cast([id] as varchar)+',',','+'5,6,8,9,10,11,'+',')>0

還有一種就是

delete from table1 where id in(1,2,3,4 )

十一、獲取子表內的一列資料的組合字串

下面這個函式獲取05年已經註冊了的某個所的律師,唯一乙個引數就是事務所的名稱,然後返回zhuce欄位裡包含05字樣的所有律師。

create  function fn_get05lvshinamebysuo  (@p_suo nvarchar(50))

returns nvarchar(2000)

as begin 

declare @lvshinames varchar(2000), @name varchar(50)

select @lvshinames=''

declare lvshi_cursor cursor for

資料庫裡有1,2,3,4,5 共5條記錄,要用一條sql語句讓其排序,使它排列成4,5,1,2,3,怎麼寫?

整理了一些高質量的教程與大家分享

1.beginning.iphone.3.development,exploring.the.iphone.sdk apress,2009 7 2.iphone in action 3.iphone os 程式設計指南 4.iphone 開發基礎教程 5.learn objective c 6.mo...

整理了關於git的一些基本用法

建立版本庫 git init 新增檔案 git add 刪除檔案 git rm 撤銷工作區修改 git checkout 撤銷暫存區修改,重新放回工作區 git reset head 提交git commit m 檢視狀態 git status 檢視日誌 git log pretty oneline...

Pandas的一些技巧分享

pandas 是乙個廣泛應用於資料分析等領域的 python 庫。關於它的教程有很多,但這裡會一些比較冷門但是非常有用的技巧。read csv 這是乙個大家都應該知道的函式,因為它就是讀取 csv 檔案的方法。但如果需要讀取資料量很大的時候,可以新增乙個引數 nrows 5,來先載入少量資料,這可以...