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;

sql server 特殊sql

上一篇:Hibernate复习(三)配置文件详解


下一篇:Linux学习笔记——硬盘分区和安装双系统