MS SqlServer中少用但是好用的SQL语句
生活随笔
收集整理的這篇文章主要介紹了
MS SqlServer中少用但是好用的SQL语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 /*
--?2010-02-26?
--?布朗
--?QQ:156298979
*/?
--?with?ties可以附加與排序字段相同值的多個行
select??top?3??with?ties?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?3?--設置全局變量,使每次返回的行數都為3行
select?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?0?--設置全局變量,使每次返回的行數為所有行
--?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
--服務器環境信息
select??serverproperty('Edition')
--字符串函數?
--將一個字符串指定位置開始指定長度的內容替換成新串的值
select?stuff('abcedefg',3,2,'1234')
--搜索子串在父串的位置
select?CharIndex('c','abcdefg',1)
select?PatIndex('%[cd]%','abcdefg')
--發音相似的詞或名稱
select?soundex('lfc')
--返回兩個字符串的差異程度
select?Difference('abcd','abce')
inner?join?--內連接
left?outer?join?--左外連接
right?outer?join?--右外連接
full?outer?join?--全外連接
cross?outer?join?--交叉連接?A表4條記錄,B表5條記錄,交叉后生成20條記錄(笛卡爾乘積)
--兩個表合并
select?*?from?hrEmployeeA?
union?all?
select?*?from?hrEmployeeB
--兩個表相交
select?distinct(UserName)?
from?
(
select?distinct(UserName)?from?hrEmployeeA?
union?all?
select?distinct(UserName)?from?hrEmployeeB
)?Emp
group?by?UserName
--兩個表關系除
--兩個表關系差(集合差)
?
--全文索引?
exec?sp_fulltext_database?'enable'?--啟用全文檢索?'disable'禁用
create?fulltext?catalog?nofc2?as?default--為數據庫創建全文目錄
create?fulltext?index?on?hrEmployee([Name],[ShortName],[Description])?--創建針對數據庫中某個表的一列或多列的全文索引。每個表只允許有一個全文索引
?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???--插入存儲過程的結果集
insert?into?hrEmployee?(Name,shortName,Description)?exec?sp_hrGetEmployee?
--insert?Default?--插入表的默認值
insert?into?hrEmployee?default?values
--Update語句連接多個表
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語句?連接多個表
delete?from?zj_Operation_Log
from?zj_Operation_Log?t1?
join?zj_Dept?t2?on?t1.DeptNo?=?t2.DeptNo
where?t2.DeptName='行政部'
?
--標識值
select?@@identity???--全局變量所有表最所生成的最近一個標識值
select?Scope_identity()??--批處理或者最近的作用域中所生成的最近一個標識值
select?ident_current('hrEmployee')?--指定表名的表所生成的最近一個標識值
--全局唯一標識符
select?newid()
--?2010-02-26?
--?布朗
--?QQ:156298979
*/?
--?with?ties可以附加與排序字段相同值的多個行
select??top?3??with?ties?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?3?--設置全局變量,使每次返回的行數都為3行
select?*?from?hrEmployee?order?by?shortName?asc
set?rowcount?0?--設置全局變量,使每次返回的行數為所有行
--?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
--服務器環境信息
select??serverproperty('Edition')
--字符串函數?
--將一個字符串指定位置開始指定長度的內容替換成新串的值
select?stuff('abcedefg',3,2,'1234')
--搜索子串在父串的位置
select?CharIndex('c','abcdefg',1)
select?PatIndex('%[cd]%','abcdefg')
--發音相似的詞或名稱
select?soundex('lfc')
--返回兩個字符串的差異程度
select?Difference('abcd','abce')
inner?join?--內連接
left?outer?join?--左外連接
right?outer?join?--右外連接
full?outer?join?--全外連接
cross?outer?join?--交叉連接?A表4條記錄,B表5條記錄,交叉后生成20條記錄(笛卡爾乘積)
--兩個表合并
select?*?from?hrEmployeeA?
union?all?
select?*?from?hrEmployeeB
--兩個表相交
select?distinct(UserName)?
from?
(
select?distinct(UserName)?from?hrEmployeeA?
union?all?
select?distinct(UserName)?from?hrEmployeeB
)?Emp
group?by?UserName
--兩個表關系除
--兩個表關系差(集合差)
?
--全文索引?
exec?sp_fulltext_database?'enable'?--啟用全文檢索?'disable'禁用
create?fulltext?catalog?nofc2?as?default--為數據庫創建全文目錄
create?fulltext?index?on?hrEmployee([Name],[ShortName],[Description])?--創建針對數據庫中某個表的一列或多列的全文索引。每個表只允許有一個全文索引
?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???--插入存儲過程的結果集
insert?into?hrEmployee?(Name,shortName,Description)?exec?sp_hrGetEmployee?
--insert?Default?--插入表的默認值
insert?into?hrEmployee?default?values
--Update語句連接多個表
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語句?連接多個表
delete?from?zj_Operation_Log
from?zj_Operation_Log?t1?
join?zj_Dept?t2?on?t1.DeptNo?=?t2.DeptNo
where?t2.DeptName='行政部'
?
--標識值
select?@@identity???--全局變量所有表最所生成的最近一個標識值
select?Scope_identity()??--批處理或者最近的作用域中所生成的最近一個標識值
select?ident_current('hrEmployee')?--指定表名的表所生成的最近一個標識值
--全局唯一標識符
select?newid()
?
總結
以上是生活随笔為你收集整理的MS SqlServer中少用但是好用的SQL语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .Net程序员学用Oracle系列(18
- 下一篇: 【整理】强化学习与MDP