1. 程式人生 > 其它 >sql server 特殊sql

sql server 特殊sql


select t.type,t.name 表名 from sysobjects t where t.type in ('U','v' ,'TR') and t.name like '%%';

select t.type,t.name 表名,f.name 列名 from sysobjects t, syscolumns f where t.id = f.id and t.type in ('U','v' ,'TR');

select t.type,t.name 表名,f.name 列名 from sysobjects t, syscolumns f where t.id = f.id and t.type in ('U','v' ,'TR') and t.name = '';
select t.type,t.name 表名,f.name 列名 from sysobjects t, syscolumns f where t.id = f.id and t.type in ('U','v' ,'TR') and t.name like '%%';

select t.type,t.name 表名,f.name 列名 from sysobjects t, syscolumns f where t.id = f.id and t.type in ('U','v' ,'TR') and f.name = '';
select t.type,t.name 表名,f.name 列名 from sysobjects t, syscolumns f where t.id = f.id and t.type in ('U','v' ,'TR') and f.name like '%%';

select name from sysobjects where type = 'v' --全部檢視
select name from sysobjects where type = 'v' and substring(name,1,3)<>'sys' --使用者建立的檢視

-- 查詢最後一分鐘執行的sql語句
SELECT TOP 100
QS.last_execution_time AS '執行時間',
qs.creation_time as '首次執行時間',
ST.text AS '執行的SQL語句',
QS.execution_count AS '執行次數',
QS.total_elapsed_time AS '耗時',
QS.total_logical_reads AS '邏輯讀取次數',
QS.total_logical_writes AS '邏輯寫入次數',
QS.total_physical_reads AS '物理讀取次數',
qp.query_plan as 'xml'
-- QS.*
FROM sys.dm_exec_query_stats QS
CROSS APPLY
sys.dm_exec_sql_text(QS.sql_handle) ST
CROSS APPLY
sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE QS.last_execution_time BETWEEN dateadd(Minute,-1,getdate()) AND dateadd(Minute,+2,getdate())
ORDER BY
last_execution_time asc,creation_time asc, QS.total_elapsed_time DESC;

-- 全庫查詢指定的字串
declare @Str nvarchar(max), @tableName varchar(50), @colName varchar(50), @rowCount int
select a.name tableName, b.name Colname, 0 as IsFound into #t1
from sysobjects a join syscolumns b on a.id=b.id join systypes c on b.xtype=c.xtype
where a.[type]='U' and c.name in ('varchar') --這裡是設定欄位的型別,以縮小範圍
declare _c1 cursor for select Colname, tableName from #t1
open _c1
fetch next from _c1 into @colName, @tableName
while @@FETCH_STATUS=0 begin
--print @Str
select @Str='select @rowCount=count(1) from ['+@tableName+'] where ['+@colName+'] = ''要查詢的內容''' --這裡是要查詢的內容
exec sp_executesql @Str, N'@rowCount int output', @rowCount output
if @rowCount>0 update #t1 set IsFound=1 where ColName=@colName and tableName=@tableName
fetch next from _c1 into @colName, @tableName
end
close _c1
deallocate _c1
select * from #t1 where IsFound=1
drop table #t1;