T SQL實用查詢之常用SQL語句

2022-01-24 11:52:57 字數 1903 閱讀 4600

刪除資料庫所有的表:

declare @sql varchar(8000

) while (select count(*) from sysobjects where type='

u')>0

begin

select @sql='

drop table

' +name

from sysobjects

where (type = 'u'

) order by

'drop table

' +name

exec(@sql)

end

查詢表的字段資訊:

select (case when a.colorder=1 then d.name else null end) 表名, 

a.colorder 字段序號,a.name 欄位名,

(case when columnproperty( a.id,a.name,'isidentity')=1 then '√'else '' end) 標識,

(case when (select count(*) from sysobjects

where (name in (select name from sysindexes

where (id = a.id) and (indid in

(select indid from sysindexkeys

where (id = a.id) and (colid in

(select colid from syscolumns where (id = a.id) and (name = a.name)))))))

and (xtype = 'pk'))>0 then '√' else '' end) 主鍵,b.name 型別,a.length 占用位元組數,

columnproperty(a.id,a.name,'precision') as 長度,

isnull(columnproperty(a.id,a.name,'scale'),0) as 小數字數,(case when a.isnullable=1 then '√'else '' end) 允許空,

isnull(e.text,'') 預設值,isnull(g.[value], ' ') as [說明]

from syscolumns a

left join systypes b on a.xtype=b.xusertype

inner join sysobjects d on a.id=d.id and d.xtype='u' and d.name<>'dtproperties'

left join syscomments e on a.cdefault=e.id

left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id

left join sys.extended_properties f on d.id=f.class and f.minor_id=0

where b.name is not null

and d.name='tablename'

order by a.id,a.colorder

判斷字段是否包含中文字元:

---乙個中文字元佔2個位元組

select len(type),datalength(type),type from t_visas where len(type)=datalength(type)

查詢表中的某個列是否有重複值出現的sql語句

select 某列,count(1) from table group by 某列 order by count(1)

PostgreSQL實用查詢SQL

參考 postgresql實用查詢sql 檢視 資料庫 www.2cto.com select from pg database 檢視表空間 select from pg tablespace 檢視語言 select from pg language 檢視角色使用者 select from pg u...

SQLSERVER之T SQL查詢(二)

今天我們討論乙個簡單的問題,由於是剛想到怕忘記記錄,所以乙個簡單的問題寫乙個部落格有點兒浪費。不過技術在於理解,在於知其然也要知其所以然,還是記錄一下同時也希望能幫到大家。sql查詢會有很多部落格,希望自己能耐心的寫下去,鞏固自己的知識。select s.sid,s.name from dbo.st...

MySQL Oracle分頁查詢的SQL語句

1.mysql分頁查詢的sql語句 關鍵字 limit select from sys user order by user id limit 0,5 0 第一條資料的位置,mysql是從0開始的 例如第二頁的話就是 limit 5,5 5 每一頁展示資料的條數 開始的位置 int beginnum...