SQLServer常用語句

1.sp_helptext是顯示規(guī)則、默認(rèn)值、未加密的存儲(chǔ)過程、用戶定義函數(shù)、觸發(fā)器或視圖的文本。

我們提供的服務(wù)有:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、商洛ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的商洛網(wǎng)站制作公司

2.SQL 查詢某字段數(shù)據(jù)所在的表

select a.name as 表名 from sysobjects as a left join syscolumns as b on a.id=b.id where b.name='字段名'

1>根據(jù)已知字段查詢表:

select a.name from sysobjects a join syscolumns b on a.id=b.id where b.name='字段名'

2>查詢符合此字段值的記錄:

select * from 表名 where 字段名=字段值 (表名是步驟一查詢出來的名稱)

3.獲取當(dāng)前數(shù)據(jù)庫(kù)中的所有用戶表
select Name from sysobjects where xtype='u' and status>=0

4.列出數(shù)據(jù)庫(kù)里所有的表名
select name from sysobjects where type='U'

5.獲取某一個(gè)表的所有字段
select name from syscolumns where id=object_id('表名')

select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')

兩種方式的效果相同

6.查詢某一個(gè)表的字段和數(shù)據(jù)類型
select column_name,data_type from information_schema.columns
where table_name = '表名'

7.取回表中字段:
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)

8.查看與某一個(gè)表相關(guān)的視圖、存儲(chǔ)過程、函數(shù)
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

9.查看當(dāng)前數(shù)據(jù)庫(kù)中所有視圖

select name as 視圖 from sysobjects where xtype='V'

10.查看當(dāng)前數(shù)據(jù)庫(kù)中所有存儲(chǔ)過程
select name as 存儲(chǔ)過程名稱 from sysobjects where xtype='P'

11.查詢用戶創(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

12.1=1,1=2的使用,在SQL語句組合時(shí)用的較多

“where 1=1” 是表示選擇全部    “where 1=2”全部不選,
如:
if @strWhere !=''
begin
set @strSQL = 'select count() as Total from [' + @tblName + '] where ' + @strWhere
end
else
begin
set @strSQL = 'select count(
) as Total from [' + @tblName + ']'
end

我們可以直接寫成

錯(cuò)誤!未找到目錄項(xiàng)。
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere

13.包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)

14.刪除重復(fù)記錄
1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
2),select distinct into temp from tablename
delete from tablename
insert into tablename select
from temp
評(píng)價(jià): 這種操作牽連大量的數(shù)據(jù)的移動(dòng),這種做法不適合大容量但數(shù)據(jù)操作
3),例如:在一個(gè)外部表中導(dǎo)入數(shù)據(jù),由于某些原因第一次只導(dǎo)入了一部分,但很難判斷具體位置,這樣只有在下一次全部導(dǎo)入,這樣也就產(chǎn)生好多重復(fù)的字段,怎樣刪除重復(fù)字段

alter table tablename
--添加一個(gè)自增列
add  column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b)  from tablename group by column1,column2,...)
alter table tablename drop column column_b

15.SQL兩表之間:根據(jù)一個(gè)表的字段更新另一個(gè)表的字段

  1. 寫法輕松,更新效率高:

update table1

set field1=table2.field1,

field2=table2.field2

from table2

where table1.id=table2.id

  1. 常規(guī)方式,種寫法相當(dāng)于一個(gè) Left join, 以外面的where為更新條數(shù),如果不加where就是所有記錄

update table1

set field1=(select top 1 field1 from table2 where table2.id=table1.id)

where table1.id in (condition)

16.比較A,B表是否相等:
if (select checksum_agg(binary_checksum(*)) from A)

(select checksum_agg(binary_checksum(*)) from B)

print '相等'
else
print '不相等'

17.請(qǐng)求其空間使用信息的表、索引視圖或隊(duì)列的限定或非限定名稱

exec sp_spaceused 'tablename'

18.查看硬盤分區(qū):
EXEC master..xp_fixeddrives

文章名稱:SQLServer常用語句
當(dāng)前URL:http://muchs.cn/article28/pieccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作響應(yīng)式網(wǎng)站、網(wǎng)站策劃、、品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)