sqlserver 常見使用sql

2021-06-29 15:12:44 字數 1572 閱讀 2919

1使用某個庫

use hongqiao_oa;

2  查詢當前資料庫有哪些表

select name from master..sysdatabases order by name

select 'select * from '+name+';' from sysobjects where xtype='u' order by name;

select * from sysobjects ;

3 查詢表結構資訊

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='tbl_md_account' --如果只查詢指定表,加上此條件

order by a.id,a.colorder

SQL Server迴圈查詢資料或執行SQL

直接上sql,此實力 適用於修改資料 宣告變數 declare i int declare max int declare groupid int declare userid int set i 1 select max max id from tpersonneltable while i ma...

sql server 2008匯入和匯出sql檔案

匯出表資料和表結構sql檔案 在日常的開發過程中,經常需要匯出某個資料庫中,某些表資料 或者,需要對某個表的結構,資料進行修改的時候,就需要在資料庫中匯出表的sql結構,包括該錶的建表語句和資料儲存語句!在這個時候,就可以利用本方法來操作!步驟閱讀 1開啟sqlserver2008,連線成功後,選擇...

檢視SQLServer最耗資源時間的SQL語句

執行最慢的sql語句 select total elapsed time execution count 1000 n 平均時間ms total elapsed time 1000 n 總花費時間ms total worker time 1000 n 所用的cpu總時間ms total physic...