整理的一些T-sql(轉)

terryisme發表於2009-06-25
把長日期轉換為短日期 Convert(char(10),getdate(),120)
整理的一些T-sql(轉) MS-SQL資料庫開發常用匯總 1.按姓氏筆畫排序:
整理的一些T-sql(轉)Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
整理的一些T-sql(轉)2.資料庫加密:
整理的一些T-sql(轉)select encrypt('原始密碼')
整理的一些T-sql(轉)select pwdencrypt('原始密碼')
整理的一些T-sql(轉)select pwdcompare('原始密碼','加密後密碼') = 1--相同;否則不相同 encrypt('原始密碼')
整理的一些T-sql(轉)select pwdencrypt('原始密碼')
整理的一些T-sql(轉)select pwdcompare('原始密碼','加密後密碼') = 1--相同;否則不相同
整理的一些T-sql(轉)3.取回表中欄位:
整理的一些T-sql(轉)declare @list varchar(1000),@sql nvarchar(1000)
整理的一些T-sql(轉)select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'
整理的一些T-sql(轉)set @sql='select '+right(@list,len(@list)-1)+' from 表A'
整理的一些T-sql(轉)exec (@sql)
整理的一些T-sql(轉)4.檢視硬碟分割槽:
整理的一些T-sql(轉)EXEC master..xp_fixeddrives
整理的一些T-sql(轉)5.比較A,B表是否相等:
整理的一些T-sql(轉)if (select checksum_agg(binary_checksum(*)) from A)
整理的一些T-sql(轉)=
整理的一些T-sql(轉)(select checksum_agg(binary_checksum(*)) from B)
整理的一些T-sql(轉)print '相等'
整理的一些T-sql(轉)else
整理的一些T-sql(轉)print '不相等'
整理的一些T-sql(轉)6.殺掉所有的事件探察器程式:
整理的一些T-sql(轉)DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocesses
整理的一些T-sql(轉)WHERE program_name IN('SQL profiler',N'SQL 事件探查器')
整理的一些T-sql(轉)EXEC sp_msforeach_worker '?'
整理的一些T-sql(轉)7.記錄搜尋:
整理的一些T-sql(轉)開頭到N條記錄
整理的一些T-sql(轉)Select Top N * From 表
整理的一些T-sql(轉)-------------------------------
整理的一些T-sql(轉)N到M條記錄(要有主索引ID)
整理的一些T-sql(轉)Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc
整理的一些T-sql(轉)----------------------------------
整理的一些T-sql(轉)N到結尾記錄
整理的一些T-sql(轉)Select Top N * From 表 Order by ID Desc
整理的一些T-sql(轉)8.如何修改資料庫的名稱:
整理的一些T-sql(轉)sp_renamedb 'old_name', 'new_name'
整理的一些T-sql(轉)9:獲取當前資料庫中的所有使用者表
整理的一些T-sql(轉)select Name from sysobjects where xtype='u' and status>=0
整理的一些T-sql(轉)10:獲取某一個表的所有欄位
整理的一些T-sql(轉)select name from syscolumns where id=object_id('表名')
整理的一些T-sql(轉)11:檢視與某一個表相關的檢視、儲存過程、函式
整理的一些T-sql(轉)select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
整理的一些T-sql(轉)12:檢視當前資料庫中所有儲存過程
整理的一些T-sql(轉)select name as 儲存過程名稱 from sysobjects where xtype='P'
整理的一些T-sql(轉)13:查詢使用者建立的所有資料庫
整理的一些T-sql(轉)select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
整理的一些T-sql(轉)或者
整理的一些T-sql(轉)select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
整理的一些T-sql(轉)14:查詢某一個表的欄位和資料型別
整理的一些T-sql(轉)select column_name,data_type from information_schema.columns
整理的一些T-sql(轉)where table_name = '表名'
整理的一些T-sql(轉)[n].[標題]:
整理的一些T-sql(轉)Select * From TableName Order By CustomerName
整理的一些T-sql(轉)[n].[標題]:
整理的一些T-sql(轉)
來自http://dev.csdn.net/develop/article/83/83138.shtm

一、 只複製一個表結構,不復制資料

整理的一些T-sql(轉)select top 0 * into [t1] from [t2]


二、 獲取資料庫中某個物件的建立指令碼

1、 先用下面的指令碼建立一個函式

整理的一些T-sql(轉)if exists(select 1 from sysobjects where id=object_id('fgetscript') and objectproperty(id,'IsInlineFunction')=0)
整理的一些T-sql(轉) drop function fgetscript
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)create function fgetscript(
整理的一些T-sql(轉) @servername varchar(50) --伺服器名
整理的一些T-sql(轉) ,@userid varchar(50)='sa' --使用者名稱,如果為nt驗證方式,則為空
整理的一些T-sql(轉) ,@password varchar(50)='' --密碼
整理的一些T-sql(轉) ,@databasename varchar(50) --資料庫名稱
整理的一些T-sql(轉) ,@objectname varchar(250) --物件名
整理的一些T-sql(轉)
整理的一些T-sql(轉)) returns varchar(8000)
整理的一些T-sql(轉)as
整理的一些T-sql(轉)begin
整理的一些T-sql(轉) declare @re varchar(8000) --返回指令碼
整理的一些T-sql(轉) declare @srvid int,@dbsid int --定義伺服器、資料庫集id
整理的一些T-sql(轉) declare @dbid int,@tbid int --資料庫、表id
整理的一些T-sql(轉) declare @err int,@src varchar(255), @desc varchar(255) --錯誤處理變數
整理的一些T-sql(轉)
整理的一些T-sql(轉)--建立sqldmo物件
整理的一些T-sql(轉) exec @err=sp_oacreate 'sqldmo.sqlserver',@srvid output
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉)--連線伺服器
整理的一些T-sql(轉) if isnull(@userid,'')='' --如果是 Nt驗證方式
整理的一些T-sql(轉) begin
整理的一些T-sql(轉) exec @err=sp_oasetproperty @srvid,'loginsecure',1
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉) exec @err=sp_oamethod @srvid,'connect',null,@servername
整理的一些T-sql(轉) end
整理的一些T-sql(轉) else
整理的一些T-sql(轉) exec @err=sp_oamethod @srvid,'connect',null,@servername,@userid,@password
整理的一些T-sql(轉)
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉)--獲取資料庫集
整理的一些T-sql(轉) exec @err=sp_oagetproperty @srvid,'databases',@dbsid output
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉)--獲取要取得指令碼的資料庫id
整理的一些T-sql(轉) exec @err=sp_oamethod @dbsid,'item',@dbid output,@databasename
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉)--獲取要取得指令碼的物件id
整理的一些T-sql(轉) exec @err=sp_oamethod @dbid,'getobjectbyname',@tbid output,@objectname
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉)--取得指令碼
整理的一些T-sql(轉) exec @err=sp_oamethod @tbid,'script',@re output
整理的一些T-sql(轉) if @err<>0 goto lberr
整理的一些T-sql(轉)
整理的一些T-sql(轉) --print @re
整理的一些T-sql(轉) return(@re)
整理的一些T-sql(轉)
整理的一些T-sql(轉)lberr:
整理的一些T-sql(轉) exec sp_oageterrorinfo NULL, @src out, @desc out
整理的一些T-sql(轉) declare @errb varbinary(4)
整理的一些T-sql(轉) set @errb=cast(@err as varbinary(4))
整理的一些T-sql(轉) exec master..xp_varbintohexstr @errb,@re out
整理的一些T-sql(轉) set @re='錯誤號: '+@re
整理的一些T-sql(轉) +char(13)+'錯誤源: '+@src
整理的一些T-sql(轉) +char(13)+'錯誤描述: '+@desc
整理的一些T-sql(轉) return(@re)
整理的一些T-sql(轉)end
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)

2、 用法如下
用法如下,

整理的一些T-sql(轉)print dbo.fgetscript('伺服器名','使用者名稱','密碼','資料庫名','表名或其它物件名')
整理的一些T-sql(轉)

3、 如果要獲取庫裡所有物件的指令碼,如如下方式

整理的一些T-sql(轉)declare @name varchar(250)
整理的一些T-sql(轉)declare #aa cursor for
整理的一些T-sql(轉) select name from sysobjects where xtype not in('S','PK','D','X','L')
整理的一些T-sql(轉)open #aa
整理的一些T-sql(轉)fetch next from #aa into @name
整理的一些T-sql(轉)while @@fetch_status=0
整理的一些T-sql(轉)begin
整理的一些T-sql(轉) print dbo.fgetscript('onlytiancai','sa','sa','database',@name)
整理的一些T-sql(轉) fetch next from #aa into @name
整理的一些T-sql(轉)end
整理的一些T-sql(轉)close #aa
整理的一些T-sql(轉)deallocate #aa
整理的一些T-sql(轉)

4、 宣告,此函式是csdn鄒建鄒老大提供的
三、 分隔字串
如果有一個用逗號分割開的字串,比如說"a,b,c,d,1,2,3,4",如何用t-sql獲取這個字串有幾個元素,獲取第幾個元素的值是多少呢?因為t-sql裡沒有split函式,也沒有陣列的概念,所以只能自己寫幾個函式了。
1、 獲取元素個數的函式

整理的一些T-sql(轉)create function getstrarrlength (@str varchar(8000))
整理的一些T-sql(轉)returns int
整理的一些T-sql(轉)as
整理的一些T-sql(轉)begin
整理的一些T-sql(轉) declare @int_return int
整理的一些T-sql(轉) declare @start int
整理的一些T-sql(轉) declare @next int
整理的一些T-sql(轉) declare @location int
整理的一些T-sql(轉) select @str =','+ @str +','
整理的一些T-sql(轉) select @str=replace(@str,',,',',')
整理的一些T-sql(轉) select @start =1
整理的一些T-sql(轉) select @next =1
整理的一些T-sql(轉) select @location = charindex(',',@str,@start)
整理的一些T-sql(轉) while (@location <>0)
整理的一些T-sql(轉) begin
整理的一些T-sql(轉) select @start = @location +1
整理的一些T-sql(轉) select @location = charindex(',',@str,@start)
整理的一些T-sql(轉) select @next =@next +1
整理的一些T-sql(轉) end
整理的一些T-sql(轉) select @int_return = @next-2
整理的一些T-sql(轉) return @int_return
整理的一些T-sql(轉)end
整理的一些T-sql(轉)

2、 獲取指定索引的值的函式

整理的一些T-sql(轉)create function getstrofindex (@str varchar(8000),@index int =0)
整理的一些T-sql(轉)returns varchar(8000)
整理的一些T-sql(轉)as
整理的一些T-sql(轉)begin
整理的一些T-sql(轉) declare @str_return varchar(8000)
整理的一些T-sql(轉) declare @start int
整理的一些T-sql(轉) declare @next int
整理的一些T-sql(轉) declare @location int
整理的一些T-sql(轉) select @start =1
整理的一些T-sql(轉) select @next =1 --如果習慣從0開始則select @next =0
整理的一些T-sql(轉) select @location = charindex(',',@str,@start)
整理的一些T-sql(轉) while (@location <>0 and @index > @next )
整理的一些T-sql(轉) begin
整理的一些T-sql(轉) select @start = @location +1
整理的一些T-sql(轉) select @location = charindex(',',@str,@start)
整理的一些T-sql(轉) select @next =@next +1
整理的一些T-sql(轉) end
整理的一些T-sql(轉) if @location =0 select @location =len(@str)+1 --如果是因為沒有逗號退出,則認為逗號在字串後
整理的一些T-sql(轉) select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗號之後的位置或者就是初始值1
整理的一些T-sql(轉) if (@index <> @next ) select @str_return = '' --如果二者不相等,則是因為逗號太少,或者@index小於@next的初始值1。
整理的一些T-sql(轉) return @str_return
整理的一些T-sql(轉)end
整理的一些T-sql(轉)

3、 測試

整理的一些T-sql(轉)SELECT [dbo].[getstrarrlength]('1,2,3,4,a,b,c,d')
整理的一些T-sql(轉)SELECT [dbo].[getstrofindex]('1,2,3,4,a,b,c,d',5)
整理的一些T-sql(轉)

四、 一條語句執行跨越若干個資料庫
我要在一條語句裡操作不同的伺服器上的不同的資料庫裡的不同的表,怎麼辦呢?
第一種方法:

整理的一些T-sql(轉)select * from OPENDATASOURCE('SQLOLEDB','Data Source=遠端ip;User ID=sa;Password=密碼').庫名.dbo.表名
整理的一些T-sql(轉)

第二種方法:
先使用聯結伺服器:

整理的一些T-sql(轉)EXEC sp_addlinkedserver '別名','','MSDASQL',NULL,NULL,'DRIVER={SQL Server};SERVER=遠端名;UID=使用者;PWD=密碼;'
整理的一些T-sql(轉)exec sp_addlinkedsrvlogin @rmtsrvname='別名',@useself='false',@locallogin='sa',@rmtuser='sa',@rmtpassword='密碼'
整理的一些T-sql(轉)GO
整理的一些T-sql(轉)

然後你就可以如下:

整理的一些T-sql(轉)select * from 別名.庫名.dbo.表名
整理的一些T-sql(轉)insert 庫名.dbo.表名 select * from 別名.庫名.dbo.表名
整理的一些T-sql(轉)select * into 庫名.dbo.新表名 from 別名.庫名.dbo.表名
整理的一些T-sql(轉)go
整理的一些T-sql(轉)

五、 怎樣獲取一個表中所有的欄位資訊
蛙蛙推薦:怎樣獲取一個表中所有欄位的資訊
先建立一個檢視

整理的一些T-sql(轉)Create view fielddesc
整理的一些T-sql(轉)as
整理的一些T-sql(轉)select o.name as table_name,c.name as field_name,t.name as type,c.length as
整理的一些T-sql(轉)
整理的一些T-sql(轉)length,c.isnullable as isnullable,convert(varchar(30),p.value) as desp
整理的一些T-sql(轉)from syscolumns c
整理的一些T-sql(轉)join systypes t on c.xtype = t.xusertype
整理的一些T-sql(轉)join sysobjects o on o.id=c.id
整理的一些T-sql(轉)left join sysproperties p on p.smallid=c.colid and p.id=o.id
整理的一些T-sql(轉)where o.xtype='U'
整理的一些T-sql(轉)
整理的一些T-sql(轉)

查詢時:

整理的一些T-sql(轉)Select * from fielddesc where table_name = '你的表名'

還有個更強的語句,是鄒建寫的,也寫出來吧

整理的一些T-sql(轉)SELECT
整理的一些T-sql(轉) (case when a.colorder=1 then d.name else '' end) N'表名',
整理的一些T-sql(轉) a.colorder N'欄位序號',
整理的一些T-sql(轉) a.name N'欄位名',
整理的一些T-sql(轉) (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'標識',
整理的一些T-sql(轉) (case when (SELECT count(*)
整理的一些T-sql(轉) FROM sysobjects
整理的一些T-sql(轉) WHERE (name in
整理的一些T-sql(轉) (SELECT name
整理的一些T-sql(轉) FROM sysindexes
整理的一些T-sql(轉) WHERE (id = a.id) AND (indid in
整理的一些T-sql(轉) (SELECT indid
整理的一些T-sql(轉) FROM sysindexkeys
整理的一些T-sql(轉) WHERE (id = a.id) AND (colid in
整理的一些T-sql(轉) (SELECT colid
整理的一些T-sql(轉) FROM syscolumns
整理的一些T-sql(轉) WHERE (id = a.id) AND (name = a.name))))))) AND
整理的一些T-sql(轉) (xtype = 'PK'))>0 then '√' else '' end) N'主鍵',
整理的一些T-sql(轉) b.name N'型別',
整理的一些T-sql(轉) a.length N'佔用位元組數',
整理的一些T-sql(轉) COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'長度',
整理的一些T-sql(轉) isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小數位數',
整理的一些T-sql(轉) (case when a.isnullable=1 then '√'else '' end) N'允許空',
整理的一些T-sql(轉) isnull(e.text,'') N'預設值',
整理的一些T-sql(轉) isnull(g.[value],'') AS N'欄位說明'
整理的一些T-sql(轉)--into ##tx
整理的一些T-sql(轉)
整理的一些T-sql(轉)FROM syscolumns a left join systypes b
整理的一些T-sql(轉)on a.xtype=b.xusertype
整理的一些T-sql(轉)inner join sysobjects d
整理的一些T-sql(轉)on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
整理的一些T-sql(轉)left join syscomments e
整理的一些T-sql(轉)on a.cdefault=e.id
整理的一些T-sql(轉)left join sysproperties g
整理的一些T-sql(轉)on a.id=g.id AND a.colid = g.smallid
整理的一些T-sql(轉)order by object_name(a.id),a.colorder
整理的一些T-sql(轉)
整理的一些T-sql(轉)

六、 時間格式轉換問題
因為新開發的軟體需要用一些舊軟體生成的一些資料,在時間格式上不統一,只能手工轉換,研究了一下午寫了三條語句,以前沒怎麼用過convert函式和case語句,還有"+"運算子在不同上下文環境也會起到不同的作用,把我搞暈了要,不過現在看來是差不多弄好了。

1、把所有"70.07.06"這樣的值變成"1970-07-06"

整理的一些T-sql(轉)UPDATE lvshi
整理的一些T-sql(轉)SET shengri = '19' + REPLACE(shengri, '.', '-')
整理的一些T-sql(轉)WHERE (zhiyezheng = '139770070153')

2、在"1970-07-06"裡提取"70","07","06"

整理的一些T-sql(轉)SELECT SUBSTRING(shengri, 3, 2) AS year, SUBSTRING(shengri, 6, 2) AS month,
整理的一些T-sql(轉) SUBSTRING(shengri, 9, 2) AS day
整理的一些T-sql(轉)FROM lvshi
整理的一些T-sql(轉)WHERE (zhiyezheng = '139770070153')
整理的一些T-sql(轉)

3、把一個時間型別欄位轉換成"1970-07-06"

整理的一些T-sql(轉)UPDATE lvshi
整理的一些T-sql(轉)SET shenling = CONVERT(varchar(4), YEAR(shenling))
整理的一些T-sql(轉) + '-' + CASE WHEN LEN(MONTH(shenling)) = 1 THEN '0' + CONVERT(varchar(2),
整理的一些T-sql(轉) month(shenling)) ELSE CONVERT(varchar(2), month(shenling))
整理的一些T-sql(轉) END + '-' + CASE WHEN LEN(day(shenling)) = 1 THEN '0' + CONVERT(char(2),
整理的一些T-sql(轉) day(shenling)) ELSE CONVERT(varchar(2), day(shenling)) END
整理的一些T-sql(轉)WHERE (zhiyezheng = '139770070153')
整理的一些T-sql(轉)

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

整理的一些T-sql(轉)--看下面的示例
整理的一些T-sql(轉)
整理的一些T-sql(轉)--示例表
整理的一些T-sql(轉)create table tempdb.dbo.t_10(
整理的一些T-sql(轉)id int primary key check(id between 1 and 10),name varchar(10))
整理的一些T-sql(轉)
整理的一些T-sql(轉)create table pubs.dbo.t_20(
整理的一些T-sql(轉)id int primary key check(id between 11 and 20),name varchar(10))
整理的一些T-sql(轉)
整理的一些T-sql(轉)create table northwind.dbo.t_30(
整理的一些T-sql(轉)id int primary key check(id between 21 and 30),name varchar(10))
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)--分割槽檢視
整理的一些T-sql(轉)create view v_t
整理的一些T-sql(轉)as
整理的一些T-sql(轉)select * from tempdb.dbo.t_10
整理的一些T-sql(轉)union all
整理的一些T-sql(轉)select * from pubs.dbo.t_20
整理的一些T-sql(轉)union all
整理的一些T-sql(轉)select * from northwind.dbo.t_30
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)--插入資料
整理的一些T-sql(轉)insert v_t select 1 ,'aa'
整理的一些T-sql(轉)union all select 2 ,'bb'
整理的一些T-sql(轉)union all select 11,'cc'
整理的一些T-sql(轉)union all select 12,'dd'
整理的一些T-sql(轉)union all select 21,'ee'
整理的一些T-sql(轉)union all select 22,'ff'
整理的一些T-sql(轉)
整理的一些T-sql(轉)--更新資料
整理的一些T-sql(轉)update v_t set name=name+'_更新' where right(id,1)=1
整理的一些T-sql(轉)
整理的一些T-sql(轉)--刪除測試
整理的一些T-sql(轉)delete from v_t where right(id,1)=2
整理的一些T-sql(轉)
整理的一些T-sql(轉)--顯示結果
整理的一些T-sql(轉)select * from v_t
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)--刪除測試
整理的一些T-sql(轉)drop table northwind.dbo.t_30,pubs.dbo.t_20,tempdb.dbo.t_10
整理的一些T-sql(轉)drop view v_t
整理的一些T-sql(轉)
整理的一些T-sql(轉)整理的一些T-sql(轉)/**//*--測試結果
整理的一些T-sql(轉)
整理的一些T-sql(轉)id name
整理的一些T-sql(轉)----------- ----------
整理的一些T-sql(轉)1 aa_更新
整理的一些T-sql(轉)11 cc_更新
整理的一些T-sql(轉)21 ee_更新
整理的一些T-sql(轉)
整理的一些T-sql(轉)(所影響的行數為 3 行)
整理的一些T-sql(轉)==*/
整理的一些T-sql(轉)整理的一些T-sql(轉)


八、 樹型的實現
整理的一些T-sql(轉)

整理的一些T-sql(轉)--參考
整理的一些T-sql(轉)
整理的一些T-sql(轉)--樹形資料查詢示例
整理的一些T-sql(轉)--作者: 鄒建
整理的一些T-sql(轉)
整理的一些T-sql(轉)--示例資料
整理的一些T-sql(轉)create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
整理的一些T-sql(轉)insert [tb] select 0,'中國'
整理的一些T-sql(轉)union all select 0,'美國'
整理的一些T-sql(轉)union all select 0,'加拿大'
整理的一些T-sql(轉)union all select 1,'北京'
整理的一些T-sql(轉)union all select 1,'上海'
整理的一些T-sql(轉)union all select 1,'江蘇'
整理的一些T-sql(轉)union all select 6,'蘇州'
整理的一些T-sql(轉)union all select 7,'常熟'
整理的一些T-sql(轉)union all select 6,'南京'
整理的一些T-sql(轉)union all select 6,'無錫'
整理的一些T-sql(轉)union all select 2,'紐約'
整理的一些T-sql(轉)union all select 2,'舊金山'
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)--查詢指定id的所有子
整理的一些T-sql(轉)create function f_cid(
整理的一些T-sql(轉)@id int
整理的一些T-sql(轉))returns @re table([id] int,[level] int)
整理的一些T-sql(轉)as
整理的一些T-sql(轉)begin
整理的一些T-sql(轉) declare @l int
整理的一些T-sql(轉) set @l=0
整理的一些T-sql(轉) insert @re select @id,@l
整理的一些T-sql(轉) while @@rowcount>0
整理的一些T-sql(轉) begin
整理的一些T-sql(轉) set @l=@l+1
整理的一些T-sql(轉) insert @re select a.[id],@l
整理的一些T-sql(轉) from [tb] a,@re b
整理的一些T-sql(轉) where a.[pid]=b.[id] and b.[level]=@l-1
整理的一些T-sql(轉) end
整理的一些T-sql(轉)整理的一些T-sql(轉)/**//**//**//*--如果只顯示最明細的子(下面沒有子),則加上這個刪除
整理的一些T-sql(轉) delete a from @re a
整理的一些T-sql(轉) where exists(
整理的一些T-sql(轉) select 1 from [tb] where [pid]=a.[id])
整理的一些T-sql(轉)--*/
整理的一些T-sql(轉) return
整理的一些T-sql(轉)end
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)--呼叫(查詢所有的子)
整理的一些T-sql(轉)select a.*,層次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)--刪除測試
整理的一些T-sql(轉)drop table [tb]
整理的一些T-sql(轉)drop function f_cid
整理的一些T-sql(轉)go
整理的一些T-sql(轉)
整理的一些T-sql(轉)

九、 排序問題

整理的一些T-sql(轉)CREATE TABLE [t] (
整理的一些T-sql(轉) [id] [int] IDENTITY (1, 1) NOT NULL ,
整理的一些T-sql(轉) [GUID] [uniqueidentifier] NULL
整理的一些T-sql(轉)) ON [PRIMARY]
整理的一些T-sql(轉)GO


下面這句執行5次

整理的一些T-sql(轉)insert t values (newid())


檢視執行結果

整理的一些T-sql(轉)select * from t


1、 第一種

整理的一些T-sql(轉)select * from t
整理的一些T-sql(轉) order by case id when 4 then 1
整理的一些T-sql(轉) when 5 then 2
整理的一些T-sql(轉) when 1 then 3
整理的一些T-sql(轉) when 2 then 4
整理的一些T-sql(轉) when 3 then 5 end


2、 第二種

整理的一些T-sql(轉)select * from t order by (id+2)%6


3、 第三種

整理的一些T-sql(轉)select * from t order by charindex(cast(id as varchar),'45123')


4、 第四種

整理的一些T-sql(轉)select * from t
整理的一些T-sql(轉)WHERE id between 0 and 5
整理的一些T-sql(轉)order by charindex(cast(id as varchar),'45123')


5、 第五種

整理的一些T-sql(轉)select * from t order by case when id >3 then id-5 else id end


6、 第六種

整理的一些T-sql(轉)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構建成的值,然後用下面的一句就全部刪
除了,比迴圈用多條語句高效吧應該。

整理的一些T-sql(轉)delete from [fujian] where charindex(','+cast([id] as varchar)+',',','+'5,6,8,9,10,11,'+',')>0


還有一種就是

整理的一些T-sql(轉)delete from table1 where id in(1,2,3,4 整理的一些T-sql(轉))


十一、獲取子表內的一列資料的組合字串
下面這個函式獲取05年已經註冊了的某個所的律師,唯一一個引數就是事務所的名稱,然後返回zhuce欄位裡包含05字樣的所有律師。

整理的一些T-sql(轉)CREATE FUNCTION fn_Get05LvshiNameBySuo (@p_suo Nvarchar(50))
整理的一些T-sql(轉)RETURNS Nvarchar(2000)
整理的一些T-sql(轉)AS
整理的一些T-sql(轉)BEGIN
整理的一些T-sql(轉) DECLARE @LvshiNames varchar(2000), @name varchar(50)
整理的一些T-sql(轉) select @LvshiNames=''
整理的一些T-sql(轉) DECLARE lvshi_cursor CURSOR FOR

資料庫裡有1,2,3,4,5 共5條記錄,要用一條sql語句讓其排序,使它排列成4,5,1,2,3,怎麼寫?
整理的一些T-sql(轉)  --資料操作
整理的一些T-sql(轉)

整理的一些T-sql(轉)  
SELECT --從資料庫表中檢索資料行和列
整理的一些T-sql(轉)
      INSERT --向資料庫表新增新資料行
整理的一些T-sql(轉)
      DELETE --從資料庫表中刪除資料行
整理的一些T-sql(轉)
      UPDATE --更新資料庫表中的資料
整理的一些T-sql(轉)

整理的一些T-sql(轉)  
--資料定義
整理的一些T-sql(轉)

整理的一些T-sql(轉)  
CREATE TABLE --建立一個資料庫表
整理的一些T-sql(轉)
      DROP TABLE --從資料庫中刪除表
整理的一些T-sql(轉)
      ALTER TABLE --修改資料庫表結構
整理的一些T-sql(轉)
      CREATE VIEW --建立一個檢視
整理的一些T-sql(轉)
      DROP VIEW --從資料庫中刪除檢視
整理的一些T-sql(轉)
      CREATE INDEX --為資料庫表建立一個索引
整理的一些T-sql(轉)
      DROP INDEX --從資料庫中刪除索引
整理的一些T-sql(轉)
      CREATE PROCEDURE --建立一個儲存過程
整理的一些T-sql(轉)
      DROP PROCEDURE --從資料庫中刪除儲存過程
整理的一些T-sql(轉)
      CREATE TRIGGER --建立一個觸發器
整理的一些T-sql(轉)
      DROP TRIGGER --從資料庫中刪除觸發器
整理的一些T-sql(轉)
      CREATE SCHEMA --向資料庫新增一個新模式
整理的一些T-sql(轉)
      DROP SCHEMA --從資料庫中刪除一個模式
整理的一些T-sql(轉)
      CREATE DOMAIN --建立一個資料值域
整理的一些T-sql(轉)
      ALTER DOMAIN --改變域定義
整理的一些T-sql(轉)
      DROP DOMAIN --從資料庫中刪除一個域
整理的一些T-sql(轉)

整理的一些T-sql(轉)  
--資料控制
整理的一些T-sql(轉)

整理的一些T-sql(轉)  
GRANT --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/93029/viewspace-1023498/,如需轉載,請註明出處,否則將追究法律責任。

相關文章