MS SqlServer中少用但是好用的SQL语句
生活随笔
收集整理的這篇文章主要介紹了
MS SqlServer中少用但是好用的SQL语句
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼 /*
--?2010-02-26?
--?布朗
--?QQ:156298979
*/?
--?with?ties可以附加與排序字段相同值的多個(gè)行
select??top?3??with?ties?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?3?--設(shè)置全局變量,使每次返回的行數(shù)都為3行
select?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?0?--設(shè)置全局變量,使每次返回的行數(shù)為所有行
--?select?...where?符合代替if語句
declare?@m?int?,@n?int?,@i?int
set?@m=4?
set?@n=1
select?@i=?ceiling(@m/@n)?where?@m>@n
select?@i
--服務(wù)器環(huán)境信息
select??serverproperty('Edition')
--字符串函數(shù)?
--將一個(gè)字符串指定位置開始指定長(zhǎng)度的內(nèi)容替換成新串的值
select?stuff('abcedefg',3,2,'1234')
--搜索子串在父串的位置
select?CharIndex('c','abcdefg',1)
select?PatIndex('%[cd]%','abcdefg')
--發(fā)音相似的詞或名稱
select?soundex('lfc')
--返回兩個(gè)字符串的差異程度
select?Difference('abcd','abce')
inner?join?--內(nèi)連接
left?outer?join?--左外連接
right?outer?join?--右外連接
full?outer?join?--全外連接
cross?outer?join?--交叉連接?A表4條記錄,B表5條記錄,交叉后生成20條記錄(笛卡爾乘積)
--兩個(gè)表合并
select?*?from?hrEmployeeA?
union?all?
select?*?from?hrEmployeeB
--兩個(gè)表相交
select?distinct(UserName)?
from?
(
select?distinct(UserName)?from?hrEmployeeA?
union?all?
select?distinct(UserName)?from?hrEmployeeB
)?Emp
group?by?UserName
--兩個(gè)表關(guān)系除
--兩個(gè)表關(guān)系差(集合差)
?
--全文索引?
exec?sp_fulltext_database?'enable'?--啟用全文檢索?'disable'禁用
create?fulltext?catalog?nofc2?as?default--為數(shù)據(jù)庫(kù)創(chuàng)建全文目錄
create?fulltext?index?on?hrEmployee([Name],[ShortName],[Description])?--創(chuàng)建針對(duì)數(shù)據(jù)庫(kù)中某個(gè)表的一列或多列的全文索引。每個(gè)表只允許有一個(gè)全文索引
?Key?Index?PK_hrEmployee
--全文檢索
select?*?from?hrEmployee?where?Contains(hrEmployee.*,'龍')?and?Contains(hrEmployee.*,'付')?--(where?Contains類似?where?in())
--全文檢索
select?*?from?ContainsTable?(hrEmployee,*,'龍')
--臨近的詞,屈折變體,
--全文檢索-模糊查詢
select?*?from?hrEmployee?where?FreeText(*,'成')
select?*?from?FreeTextTable(hrEmployee,*,'成')
drop?fulltext?index?on?hrEmployee
drop?fulltext?catalog?nofc2
exec?sp_fulltext_database?'disable'
--視圖加密
create?view?Vabc?as
(
select?*?from?hrEmployee
)
with?Encryption
--WITH?CHECK?OPTION?/?SCHEMABINDING?/?VIEW_METADATA
--Insert語句
--Insert/values
--Insert/select
--Insert/exec???--插入存儲(chǔ)過程的結(jié)果集
insert?into?hrEmployee?(Name,shortName,Description)?exec?sp_hrGetEmployee?
--insert?Default?--插入表的默認(rèn)值
insert?into?hrEmployee?default?values
--Update語句連接多個(gè)表
update?hrEmployee?
set?Salary?=?Salary?*?(1?+?2*?case?when?t1.EnterTime?>?'2000-01-01'?then?1?else?0?end?)??from?hrEmployee?t1?join?zj_Dept?t2?on?t1.DeptID?=?t2.DeptID
--Delete語句?連接多個(gè)表
delete?from?zj_Operation_Log
from?zj_Operation_Log?t1?
join?zj_Dept?t2?on?t1.DeptNo?=?t2.DeptNo
where?t2.DeptName='行政部'
?
--標(biāo)識(shí)值
select?@@identity???--全局變量所有表最所生成的最近一個(gè)標(biāo)識(shí)值
select?Scope_identity()??--批處理或者最近的作用域中所生成的最近一個(gè)標(biāo)識(shí)值
select?ident_current('hrEmployee')?--指定表名的表所生成的最近一個(gè)標(biāo)識(shí)值
--全局唯一標(biāo)識(shí)符
select?newid()
--?2010-02-26?
--?布朗
--?QQ:156298979
*/?
--?with?ties可以附加與排序字段相同值的多個(gè)行
select??top?3??with?ties?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?3?--設(shè)置全局變量,使每次返回的行數(shù)都為3行
select?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?0?--設(shè)置全局變量,使每次返回的行數(shù)為所有行
--?select?...where?符合代替if語句
declare?@m?int?,@n?int?,@i?int
set?@m=4?
set?@n=1
select?@i=?ceiling(@m/@n)?where?@m>@n
select?@i
--服務(wù)器環(huán)境信息
select??serverproperty('Edition')
--字符串函數(shù)?
--將一個(gè)字符串指定位置開始指定長(zhǎng)度的內(nèi)容替換成新串的值
select?stuff('abcedefg',3,2,'1234')
--搜索子串在父串的位置
select?CharIndex('c','abcdefg',1)
select?PatIndex('%[cd]%','abcdefg')
--發(fā)音相似的詞或名稱
select?soundex('lfc')
--返回兩個(gè)字符串的差異程度
select?Difference('abcd','abce')
inner?join?--內(nèi)連接
left?outer?join?--左外連接
right?outer?join?--右外連接
full?outer?join?--全外連接
cross?outer?join?--交叉連接?A表4條記錄,B表5條記錄,交叉后生成20條記錄(笛卡爾乘積)
--兩個(gè)表合并
select?*?from?hrEmployeeA?
union?all?
select?*?from?hrEmployeeB
--兩個(gè)表相交
select?distinct(UserName)?
from?
(
select?distinct(UserName)?from?hrEmployeeA?
union?all?
select?distinct(UserName)?from?hrEmployeeB
)?Emp
group?by?UserName
--兩個(gè)表關(guān)系除
--兩個(gè)表關(guān)系差(集合差)
?
--全文索引?
exec?sp_fulltext_database?'enable'?--啟用全文檢索?'disable'禁用
create?fulltext?catalog?nofc2?as?default--為數(shù)據(jù)庫(kù)創(chuàng)建全文目錄
create?fulltext?index?on?hrEmployee([Name],[ShortName],[Description])?--創(chuàng)建針對(duì)數(shù)據(jù)庫(kù)中某個(gè)表的一列或多列的全文索引。每個(gè)表只允許有一個(gè)全文索引
?Key?Index?PK_hrEmployee
--全文檢索
select?*?from?hrEmployee?where?Contains(hrEmployee.*,'龍')?and?Contains(hrEmployee.*,'付')?--(where?Contains類似?where?in())
--全文檢索
select?*?from?ContainsTable?(hrEmployee,*,'龍')
--臨近的詞,屈折變體,
--全文檢索-模糊查詢
select?*?from?hrEmployee?where?FreeText(*,'成')
select?*?from?FreeTextTable(hrEmployee,*,'成')
drop?fulltext?index?on?hrEmployee
drop?fulltext?catalog?nofc2
exec?sp_fulltext_database?'disable'
--視圖加密
create?view?Vabc?as
(
select?*?from?hrEmployee
)
with?Encryption
--WITH?CHECK?OPTION?/?SCHEMABINDING?/?VIEW_METADATA
--Insert語句
--Insert/values
--Insert/select
--Insert/exec???--插入存儲(chǔ)過程的結(jié)果集
insert?into?hrEmployee?(Name,shortName,Description)?exec?sp_hrGetEmployee?
--insert?Default?--插入表的默認(rèn)值
insert?into?hrEmployee?default?values
--Update語句連接多個(gè)表
update?hrEmployee?
set?Salary?=?Salary?*?(1?+?2*?case?when?t1.EnterTime?>?'2000-01-01'?then?1?else?0?end?)??from?hrEmployee?t1?join?zj_Dept?t2?on?t1.DeptID?=?t2.DeptID
--Delete語句?連接多個(gè)表
delete?from?zj_Operation_Log
from?zj_Operation_Log?t1?
join?zj_Dept?t2?on?t1.DeptNo?=?t2.DeptNo
where?t2.DeptName='行政部'
?
--標(biāo)識(shí)值
select?@@identity???--全局變量所有表最所生成的最近一個(gè)標(biāo)識(shí)值
select?Scope_identity()??--批處理或者最近的作用域中所生成的最近一個(gè)標(biāo)識(shí)值
select?ident_current('hrEmployee')?--指定表名的表所生成的最近一個(gè)標(biāo)識(shí)值
--全局唯一標(biāo)識(shí)符
select?newid()
?
總結(jié)
以上是生活随笔為你收集整理的MS SqlServer中少用但是好用的SQL语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .Net程序员学用Oracle系列(18
- 下一篇: 【整理】强化学习与MDP