T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select top 0 * into [t1] from [t2]


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

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

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

2、 用法如下
用法如下,

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)print dbo.fgetscript('伺服器名','使用者名稱','密碼','資料庫名','表名或其它物件名')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)declare @name varchar(250)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)declare #aa cursor for
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select name from sysobjects where xtype not in('S','PK','D','X','L')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)open #aa
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)fetch next from #aa into @name
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)while @@fetch_status=0
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)begin
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) print dbo.fgetscript('onlytiancai','sa','sa','database',@name)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) fetch next from #aa into @name
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)end
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)close #aa
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)deallocate #aa
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)create function getstrarrlength (@str varchar(8000))
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)returns int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)as
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)begin
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @int_return int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @start int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @next int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @location int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @str =','+ @str +','
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @str=replace(@str,',,',',')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @start =1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @next =1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @location = charindex(',',@str,@start)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) while (@location <>0)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) begin
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @start = @location +1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @location = charindex(',',@str,@start)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @next =@next +1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) end
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @int_return = @next-2
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) return @int_return
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)end
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)create function getstrofindex (@str varchar(8000),@index int =0)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)returns varchar(8000)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)as
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)begin
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @str_return varchar(8000)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @start int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @next int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) declare @location int
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @start =1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @next =1 --如果習慣從0開始則select @next =0
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @location = charindex(',',@str,@start)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) while (@location <>0 and @index > @next )
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) begin
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @start = @location +1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @location = charindex(',',@str,@start)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @next =@next +1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) end
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) if @location =0 select @location =len(@str)+1 --如果是因為沒有逗號退出,則認為逗號在字串後
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗號之後的位置或者就是初始值1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) if (@index <> @next ) select @str_return = '' --如果二者不相等,則是因為逗號太少,或者@index小於@next的初始值1。
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) return @str_return
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)end
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

3、 測試

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)SELECT [dbo].[getstrarrlength]('1,2,3,4,a,b,c,d')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)SELECT [dbo].[getstrofindex]('1,2,3,4,a,b,c,d',5)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from OPENDATASOURCE('SQLOLEDB','Data Source=遠端ip;User ID=sa;Password=密碼').庫名.dbo.表名
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)EXEC sp_addlinkedserver '別名','','MSDASQL',NULL,NULL,'DRIVER={SQL Server};SERVER=遠端名;UID=使用者;PWD=密碼;'
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)exec sp_addlinkedsrvlogin @rmtsrvname='別名',@useself='false',@locallogin='sa',@rmtuser='sa',@rmtpassword='密碼'
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)GO
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

然後你就可以如下:

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from 別名.庫名.dbo.表名
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)insert 庫名.dbo.表名 select * from 別名.庫名.dbo.表名
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * into 庫名.dbo.新表名 from 別名.庫名.dbo.表名
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)go
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)Create view fielddesc
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)as
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select o.name as table_name,c.name as field_name,t.name as type,c.length as
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)length,c.isnullable as isnullable,convert(varchar(30),p.value) as desp
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)from syscolumns c
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)join systypes t on c.xtype = t.xusertype
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)join sysobjects o on o.id=c.id
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)left join sysproperties p on p.smallid=c.colid and p.id=o.id
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)where o.xtype='U'
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

查詢時:

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)Select * from fielddesc where table_name = '你的表名'

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

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

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

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)UPDATE lvshi
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)SET shengri = '19' + REPLACE(shengri, '.', '-')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)WHERE (zhiyezheng = '139770070153')

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)SELECT SUBSTRING(shengri, 3, 2) AS year, SUBSTRING(shengri, 6, 2) AS month,
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) SUBSTRING(shengri, 9, 2) AS day
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)FROM lvshi
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)WHERE (zhiyezheng = '139770070153')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)UPDATE lvshi
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)SET shenling = CONVERT(varchar(4), YEAR(shenling))
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) + '-' + CASE WHEN LEN(MONTH(shenling)) = 1 THEN '0' + CONVERT(varchar(2),
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) month(shenling)) ELSE CONVERT(varchar(2), month(shenling))
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) END + '-' + CASE WHEN LEN(day(shenling)) = 1 THEN '0' + CONVERT(char(2),
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) day(shenling)) ELSE CONVERT(varchar(2), day(shenling)) END
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)WHERE (zhiyezheng = '139770070153')
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

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


八、 樹型的實現
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

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

九、 排序問題

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)CREATE TABLE [t] (
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) [id] [int] IDENTITY (1, 1) NOT NULL ,
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) [GUID] [uniqueidentifier] NULL
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)) ON [PRIMARY]
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)GO


下面這句執行5次

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)insert t values (newid())


檢視執行結果

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from t


1、 第一種

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from t
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) order by case id when 4 then 1
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) when 5 then 2
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) when 1 then 3
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) when 2 then 4
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) when 3 then 5 end


2、 第二種

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from t order by (id+2)%6


3、 第三種

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from t order by charindex(cast(id as varchar),'45123')


4、 第四種

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from t
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)WHERE id between 0 and 5
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)order by charindex(cast(id as varchar),'45123')


5、 第五種

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)select * from t order by case when id >3 then id-5 else id end


6、 第六種

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)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(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)delete from [fujian] where charindex(','+cast([id] as varchar)+',',','+'5,6,8,9,10,11,'+',')>0


還有一種就是

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)delete from table1 where id in(1,2,3,4 T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html))


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

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)CREATE FUNCTION fn_Get05LvshiNameBySuo (@p_suo Nvarchar(50))
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)RETURNS Nvarchar(2000)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)AS
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)BEGIN
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) DECLARE @LvshiNames varchar(2000), @name varchar(50)
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) select @LvshiNames=''
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html) DECLARE lvshi_cursor CURSOR FOR

資料庫裡有1,2,3,4,5 共5條記錄,要用一條sql語句讓其排序,使它排列成4,5,1,2,3,怎麼寫?
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)  --資料操作
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)  
SELECT --從資料庫表中檢索資料行和列
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      INSERT --向資料庫表新增新資料行
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DELETE --從資料庫表中刪除資料行
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      UPDATE --更新資料庫表中的資料
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)  
--資料定義
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)  
CREATE TABLE --建立一個資料庫表
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP TABLE --從資料庫中刪除表
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      ALTER TABLE --修改資料庫表結構
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      CREATE VIEW --建立一個檢視
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP VIEW --從資料庫中刪除檢視
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      CREATE INDEX --為資料庫表建立一個索引
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP INDEX --從資料庫中刪除索引
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      CREATE PROCEDURE --建立一個儲存過程
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP PROCEDURE --從資料庫中刪除儲存過程
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      CREATE TRIGGER --建立一個觸發器
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP TRIGGER --從資料庫中刪除觸發器
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      CREATE SCHEMA --向資料庫新增一個新模式
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP SCHEMA --從資料庫中刪除一個模式
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      CREATE DOMAIN --建立一個資料值域
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      ALTER DOMAIN --改變域定義
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)
      DROP DOMAIN --從資料庫中刪除一個域
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)  
--資料控制
T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)

T-SQL(http://www.cnblogs.com/ghd258/archive/2006/03/20/354147.html)  
GRANT --

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

相關文章