SQL 常用指令碼

2022-02-20 23:35:27 字數 2951 閱讀 7446

select o.name as tablename,c.name as colname,k.keyno as sort

from sysindexes i

join sysindexkeys k on i.id = k.id and i.indid = k.indid

join sysobjects o on i.id = o.id

join syscolumns c on i.id=c.id and k.colid = c.colid

where exists( select 1 from sysobjects where name = i.name)

and o.name=@tablename--表名稱

order by o.name,k.colid

查詢所有儲存過程包含了哪些表,函式

select

*from sys.all_sql_modules where definition like

'%[name]%

'

查詢作業步驟裡執行了哪些儲存過程

select

*from

[msdb

].[dbo

].[sysjobsteps

]where command like

'%recmsg_orderresult%

'

查詢所有作業資訊

select

*from

[msdb

].[dbo

].[sysjobs

]where job_id =

''

--檢視資料庫後台程序

select

session_id,

--ecid,

--sp.dbid,

--db_name(sp.dbid) as db_nm,

--nt_username,

er.status,

wait_type,

substring (qt.text, er.statement_start_offset/2,

(case

when er.statement_end_offset =-1

then

len(convert(nvarchar(max), qt.text)) *

2else er.statement_end_offset end

- er.statement_start_offset)/

2) as

currentsql,

qt.text

,

--program_name,

--hostname,

start_time,

datediff ( millisecond,start_time, getdate() ) as

exectime

from

sys.dm_exec_requests er

inner

join sys.sysprocesses sp on er.session_id =

sp.spid

qtwhere session_id >

50and session_id not

in (@@spid

) --

and sp.dbid=7

order

bydatediff ( millisecond,start_time, getdate() ) desc

-- 獲取主鍵排序

select o.name as tablename,c.name as colname,k.keyno as

sort

from

sysindexes i

join sysindexkeys k on i.id = k.id and i.indid =

k.indid

join sysobjects o on i.id =

o.id

join syscolumns c on i.id=c.id and k.colid =

c.colid

where

exists( select

1from sysobjects where name =

i.name)

and o.name=

@tablename

--表名稱

order

by o.name,k.colid

-- 獲取資料庫中所有表名及資料條數

select  b.name as tablename ,  

c.row_count as datacount

from sys.indexes a ,

sys.objects b ,

sys.dm_db_partition_stats c

where a.[object_id] = b.[object_id]

and b.[object_id] = c.[object_id]

and a.index_id = c.index_id

and a.index_id < 2

and b.is_ms_shipped = 0

order by b.name

常用SQL查詢指令碼

本文假定讀者已經對資料庫連線技術有所了解,因此只討論有關sql查詢命令的語法。表結構如下 ms access 2000 表名 usertable 欄位名 字段型別 userid 自動計數 長整型 username 文字 user 文字 createdate 日期 時間 預設值為now 一 用sele...

MySQL常用SQL指令碼

1.重新定義表中某個欄位的屬性 alter table user modify column name varchar 200 comment 姓名 2.給表新增列欄位 alter table user add column varchar 200 comment 性別 3.刪除表中的乙個列欄位 a...

常用 SQL指令碼 收集

declare ttable 成績 int,學生 varchar 10 insert tselect 100,小張 union select 70 小力 union select 80 小剛 union select 78 小王 select pid select count 1 from twhe...