MSSQL經典語句

2021-04-13 23:53:46 字數 2540 閱讀 4748

1.按姓氏筆畫排序:select *

from tablename order by customername collate

chinese_prc_stroke_ci_as

2.資料庫加密:select encrypt('原始密碼')

select

pwdencrypt('原始密碼')

select

pwdcompare('原始密碼','加密後密碼') = 1--相同;否則不相同

encrypt('原始密碼')

select

pwdencrypt('原始密碼')

select

pwdcompare('原始密碼','加密後密碼') = 1--相同;否則不相同

3.取回表中字段:declare @list varchar(1000),@sql nvarchar(1000)

select @list=@list+','+b.name from

sysobjects a,syscolumns b where a.id=b.id and a.name='表a'

set @sql='select '+right(@list,len(@list)-1)+' from 表a'

exec (@sql)

4.檢視硬碟分割槽:exec

master..xp_fixeddrives

5.比較a,b表是否相等:if (

select checksum_agg(binary_checksum(*)) from a)

=(select checksum_agg(binary_checksum(*)) from b)

print '相等'

else

print '不相等'

6.殺掉所有的事件探察器程序:declare hcforeach

cursor global for select 'kill '+rtrim(spid) from master.dbo.sysprocesses

where program_name in('sql profiler',n'sql 事件探查器')

exec sp_msforeach_worker '?'

7.記錄搜尋:開頭到n條記錄

select top n * from 表

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

n到m條記錄(要有主索引id)select top m-n * from 表 where id in (select top m id from 表) order

by id desc

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

n到結尾記錄

select top n * from 表 order by id desc

8.如何修改資料庫的名稱:sp_renamedb 'old_name', 'new_name'

9:獲取當前資料庫中的所有使用者表

select name

from sysobjects where xtype='u' and status>=0

10:獲取某乙個表的所有字段

select name

from syscolumns where id=object_id('表名')

select a.*

from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

12:檢視當前資料庫中所有儲存過程

select name as 儲存過程名稱

from sysobjects where xtype='p'

13:查詢使用者建立的所有資料庫

select *

from master..sysdatabases d where sid not in(select sid from master..syslogins where name='sa')

或者select dbid, name as db_name from master..sysdatabases where sid <> 0x01

14:查詢某乙個表的字段和資料型別

select column_name,data_type

from information_schema.columns

where table_name = '表名'

[n].[標題]:select *

from tablename order by customername

[n].[標題]:select *

from tablename order by customername

MSSQL經典語句

1.按姓氏筆畫排序 select from tablename order by customername collate chinese prc stroke ci as 2.資料庫加密 select encrypt 原始密碼 select pwdencrypt 原始密碼 select pwdco...

MSSQL備份語句

完整備份 backup database northwindcs to disk g backup northwindcs full 20070908.bak 差異備份 backup database northwindcs to disk g backup northwindcs diff 200...

mssql動態語句

有時候需要執行動態拼接的sql,這就用到了動態語句 exec 執行傳入變數可以,但是如果想傳出引數,需要用到sp executesql 並且注意引數需要是國家字符集 set quoted identifier off set v sql select n num count 1 v val sum ...