SQL Server 數(shù)據(jù)庫(kù)實(shí)用SQL語(yǔ)句
發(fā)布日期:2022-01-09 17:44 | 文章來(lái)源:源碼中國(guó)
select * from sysobjects where parent_obj in(
select id from sysobjects where name='表名')
and xtype='PK'
--查看所有表
select * from sysobjects where xtype='PK'
--刪除列中含數(shù)字的
delete news where patindex('%[0-9]%',title)>0
--刪除刪去 字段 title值重復(fù)的行,且只保留 id 較小的這個(gè)
delete news where exists(select 1 from news t where t.title=news.title and t.id<news.id)
--查看數(shù)據(jù)庫(kù)信息
select * from sys.databases where name='master'
1.按姓氏筆畫排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分頁(yè)SQL語(yǔ)句
select * from(select (row_number() OVER (ORDER BY tab.ID Desc)) as rownum,tab.* from 表名 As tab) As t where rownum between 起始位置 And 結(jié)束位置
3.獲取當(dāng)前數(shù)據(jù)庫(kù)中的所有用戶表
select * from sysobjects where xtype='U' and category=0
4.獲取某一個(gè)表的所有字段
select name from syscolumns where id=object_id('表名')
5.查看與某一個(gè)表相關(guān)的視圖、存儲(chǔ)過程、函數(shù)
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
6.查看當(dāng)前數(shù)據(jù)庫(kù)中所有存儲(chǔ)過程
select name as 存儲(chǔ)過程名稱 from sysobjects where xtype='P'
7.查詢用戶創(chuàng)建的所有數(shù)據(jù)庫(kù)
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
8.查詢某一個(gè)表的字段和數(shù)據(jù)類型
select column_name,data_type from information_schema.columns
where table_name = '表名'
9.使用事務(wù)
在使用一些對(duì)數(shù)據(jù)庫(kù)表的臨時(shí)的SQL語(yǔ)句操作時(shí),可以采用SQL SERVER事務(wù)處理,防止對(duì)數(shù)據(jù)操作后發(fā)現(xiàn)誤操作問題
開始事務(wù)
Begin tran
Insert Into TableName Values(…)
SQL語(yǔ)句操作不正常,則回滾事務(wù)。
回滾事務(wù)
Rollback tran
SQL語(yǔ)句操作正常,則提交事務(wù),數(shù)據(jù)提交至數(shù)據(jù)庫(kù)。
提交事務(wù)
Commit tran
10. 按全文匹配方式查詢
字段名 LIKE N'%[^a-zA-Z0-9]China[^a-zA-Z0-9]%'
OR 字段名 LIKE N'%[^a-zA-Z0-9]China'
OR 字段名 LIKE N'China[^a-zA-Z0-9]%'
OR 字段名 LIKE N'China
11.計(jì)算執(zhí)行SQL語(yǔ)句查詢時(shí)間
declare @d datetime
set @d=getdate()
select * from SYS_ColumnProperties select [語(yǔ)句執(zhí)行花費(fèi)時(shí)間(毫秒)]=datediff(ms,@d,getdate())
12、說(shuō)明:幾個(gè)高級(jí)查詢運(yùn)算詞
A: UNION 運(yùn)算符
UNION 運(yùn)算符通過組合其他兩個(gè)結(jié)果表(例如 TABLE1 和 TABLE2)并消去表中任何重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨 UNION 一起使用時(shí)(即 UNION ALL),不消除重復(fù)行。兩種情況下,派生表的每一行不是來(lái)自 TABLE1 就是來(lái)自 TABLE2。
B: EXCEPT 運(yùn)算符
EXCEPT 運(yùn)算符通過包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨 EXCEPT 一起使用時(shí) (EXCEPT ALL),不消除重復(fù)行。
C: INTERSECT 運(yùn)算符
INTERSECT 運(yùn)算符通過只包括 TABLE1 和 TABLE2 中都有的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨 INTERSECT 一起使用時(shí) (INTERSECT ALL),不消除重復(fù)行。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信