SQL Server 數(shù)據(jù)庫實(shí)用SQL語句
更新時(shí)間:2012年11月28日 09:50:54 作者:
本文將詳細(xì)介紹SQL Server 數(shù)據(jù)庫實(shí)用SQL語句,需要了解更多的朋友可以參考下
--查看指定表的外鍵約束
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ù)庫信息
select * from sys.databases where name='master'
1.按姓氏筆畫排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分頁SQL語句
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ù)庫中的所有用戶表
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ù)庫中所有存儲(chǔ)過程
select name as 存儲(chǔ)過程名稱 from sysobjects where xtype='P'
7.查詢用戶創(chuàng)建的所有數(shù)據(jù)庫
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ù)庫表的臨時(shí)的SQL語句操作時(shí),可以采用SQL SERVER事務(wù)處理,防止對(duì)數(shù)據(jù)操作后發(fā)現(xiàn)誤操作問題
開始事務(wù)
Begin tran
Insert Into TableName Values(…)
SQL語句操作不正常,則回滾事務(wù)。
回滾事務(wù)
Rollback tran
SQL語句操作正常,則提交事務(wù),數(shù)據(jù)提交至數(shù)據(jù)庫。
提交事務(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語句查詢時(shí)間
declare @d datetime
set @d=getdate()
select * from SYS_ColumnProperties select [語句執(zhí)行花費(fèi)時(shí)間(毫秒)]=datediff(ms,@d,getdate())
12、說明:幾個(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ù)行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 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ù)行。
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ù)庫信息
select * from sys.databases where name='master'
1.按姓氏筆畫排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分頁SQL語句
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ù)庫中的所有用戶表
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ù)庫中所有存儲(chǔ)過程
select name as 存儲(chǔ)過程名稱 from sysobjects where xtype='P'
7.查詢用戶創(chuàng)建的所有數(shù)據(jù)庫
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ù)庫表的臨時(shí)的SQL語句操作時(shí),可以采用SQL SERVER事務(wù)處理,防止對(duì)數(shù)據(jù)操作后發(fā)現(xiàn)誤操作問題
開始事務(wù)
Begin tran
Insert Into TableName Values(…)
SQL語句操作不正常,則回滾事務(wù)。
回滾事務(wù)
Rollback tran
SQL語句操作正常,則提交事務(wù),數(shù)據(jù)提交至數(shù)據(jù)庫。
提交事務(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語句查詢時(shí)間
declare @d datetime
set @d=getdate()
select * from SYS_ColumnProperties select [語句執(zhí)行花費(fèi)時(shí)間(毫秒)]=datediff(ms,@d,getdate())
12、說明:幾個(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ù)行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 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ù)行。
相關(guān)文章
delete from 表名與truncate table 表名區(qū)別
delete from 表名與truncate table 表名區(qū)別,選擇適合我們的刪除數(shù)據(jù)庫數(shù)據(jù)的方法2012-10-10在Sql Server中調(diào)用外部EXE執(zhí)行程序引發(fā)的問題
這篇文章主要介紹了在Sql Server中調(diào)用外部EXE執(zhí)行程序引發(fā)的問題及解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08SQL Server 觸發(fā)器 表的特定字段更新時(shí),觸發(fā)Update觸發(fā)器
另外再補(bǔ)充一句:insert和update的數(shù)據(jù)都會(huì)保存在臨時(shí)表中,所以使用inserted可以取出這些數(shù)據(jù),刪除時(shí)使用deleted可以取出被刪除的數(shù)據(jù)2009-08-0815個(gè)初學(xué)者必看的基礎(chǔ)SQL查詢語句
這篇文章主要介紹了15個(gè)初學(xué)者必看的基礎(chǔ)SQL查詢語句,對(duì)于初學(xué)者來說,這是一篇不能錯(cuò)過的文章2015-12-12sqlserverdriver配置方法 jdbc連接sqlserver
這篇文章主要介紹了sqlserverdriver配置方法,介紹了連接SqlServer2000 和連接SqlServer2005的方法,大家參考使用吧2014-01-01