溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

SQL Server常用語(yǔ)句

發(fā)布時(shí)間:2020-05-23 13:57:11 來(lái)源:億速云 閱讀:935 作者:鴿子 欄目:關(guān)系型數(shù)據(jù)庫(kù)

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

2.SQL 查詢(xún)某字段數(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ù)已知字段查詢(xún)表:

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

2>查詢(xún)符合此字段值的記錄:

select * from 表名 where 字段名=字段值 (表名是步驟一查詢(xún)出來(lái)的名稱(chēng))

3.獲取當(dāng)前數(shù)據(jù)庫(kù)中的所有用戶(hù)表
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.查詢(xún)某一個(gè)表的字段和數(shù)據(jù)類(lèi)型
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ǔ)過(guò)程、函數(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ǔ)過(guò)程
select name as 存儲(chǔ)過(guò)程名稱(chēng) from sysobjects where xtype='P'

11.查詢(xún)用戶(hù)創(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語(yǔ)句組合時(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

我們可以直接寫(xiě)成

錯(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. 寫(xiě)法輕松,更新效率高:

update table1

set field1=table2.field1,

field2=table2.field2

from table2

where table1.id=table2.id

  1. 常規(guī)方式,種寫(xiě)法相當(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ì)列的限定或非限定名稱(chēng)

exec sp_spaceused 'tablename'

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI