sql系统中的存储过程,从中可以找到查询各种信息的语句

简介: create procedure sys.sp_help @objname nvarchar(776) = NULL -- object name we're after as -- PRELIMINARY set...
create procedure sys.sp_help  
 @objname nvarchar(776) = NULL  -- object name we're after  
as  
 -- PRELIMINARY  
 set nocount on  
 declare @dbname sysname  
  ,@no varchar(35), @yes varchar(35), @none varchar(35)  
 select @no = 'no', @yes = 'yes', @none = 'none'  
  
 -- If no @objname given, give a little info about all objects.  
 if @objname is null  ----传入空值
 begin  
  -- DISPLAY ALL SYSOBJECTS --  
        select  
            'Name'          = o.name,  
            'Owner'         = user_name(ObjectProperty( object_id, 'ownerid')),  
            'Object_type'   = substring(v.name,5,31)  
        from sys.all_objects o, master.dbo.spt_values v  
        where o.type = substring(v.name,1,2) collate database_default and v.type = 'O9T'  
        order by [Owner] asc, Object_type desc, Name asc  
  
  print ' '  
  
  -- DISPLAY ALL USER TYPES  
  select  
   'User_type' = name,  
   'Storage_type' = type_name(system_type_id),  
   'Length'  = max_length,  
   'Prec'  = Convert(int,TypePropertyEx(user_type_id, 'precision')),  
   'Scale'  = Convert(int,TypePropertyEx(user_type_id, 'scale')),  
   'Nullable'  = case when is_nullable = 1 then @yes else @no end,  
   'Default_name' = isnull(object_name(default_object_id), @none),  
   'Rule_name'  = isnull(object_name(rule_object_id), @none),  
   'Collation'  = collation_name  
  from sys.types  
  where user_type_id > 256  
  order by name  
  
  return(0)  
 end  
  
 -- Make sure the @objname is local to the current database.  
 select @dbname = parsename(@objname,3)  
 if @dbname is null  
  select @dbname = db_name()  
 else if @dbname <> db_name()  
  begin  
   raiserror(15250,-1,-1)  
   return(1)  
  end  
  
 -- @objname must be either sysobjects or systypes: first look in sysobjects  
 declare @objid int  
 declare @sysobj_type char(2)  
 select @objid = object_id, @sysobj_type = type from sys.all_objects where object_id = object_id(@objname)  
  
 -- IF NOT IN SYSOBJECTS, TRY SYSTYPES --  
 if @objid is null  
 begin  
  -- UNDONE: SHOULD CHECK FOR AND DISALLOW MULTI-PART NAME  
  select @objid = type_id(@objname)  
  
  -- IF NOT IN SYSTYPES, GIVE UP  
  if @objid is null  
  begin  
   raiserror(15009,-1,-1,@objname,@dbname)  
   return(1)  
  end  
  
  -- DATA TYPE HELP (prec/scale only valid for numerics)  
  select  
   'Type_name' = name,  
   'Storage_type' = type_name(system_type_id),  
   'Length'  = max_length,  
   'Prec'   = Convert(int,TypePropertyEx(user_type_id, 'precision')),  
   'Scale'   = Convert(int,TypePropertyEx(user_type_id, 'scale')),  
   'Nullable'   = case when is_nullable=1 then @yes else @no end,  
   'Default_name' = isnull(object_name(default_object_id), @none),  
   'Rule_name'  = isnull(object_name(rule_object_id), @none),  
   'Collation'  = collation_name  
  from sys.types  
  where user_type_id = @objid  
  
  return(0)  
 end  
  
 -- FOUND IT IN SYSOBJECT, SO GIVE OBJECT INFO  
 select  
  'Name'    = o.name,  
  'Owner'    = user_name(ObjectProperty( object_id, 'ownerid')),  
        'Type'              = substring(v.name,5,31),  
  'Created_datetime' = o.create_date  
 from sys.all_objects o, master.dbo.spt_values v  
 where o.object_id = @objid and o.type = substring(v.name,1,2) collate database_default and v.type = 'O9T'  
  
 print ' '  
  
 -- DISPLAY COLUMN IF TABLE / VIEW  
 if exists (select * from sys.all_columns where object_id = @objid)  
 begin  
  
  -- SET UP NUMERIC TYPES: THESE WILL HAVE NON-BLANK PREC/SCALE  
  declare @numtypes nvarchar(80)  
  select @numtypes = N'tinyint,smallint,decimal,int,real,money,float,numeric,smallmoney'  
  
  -- INFO FOR EACH COLUMN  
  print ' '  
  select  
   'Column_name'   = name,  
   'Type'     = type_name(user_type_id),  
   'Computed'    = case when ColumnProperty(object_id, name, 'IsComputed') = 0 then @no else @yes end,  
   'Length'     = convert(int, max_length),  
   'Prec'     = case when charindex(type_name(system_type_id), @numtypes) > 0  
          then convert(char(5),ColumnProperty(object_id, name, 'precision'))  
          else '     ' end,  
   'Scale'     = case when charindex(type_name(system_type_id), @numtypes) > 0  
          then convert(char(5),OdbcScale(system_type_id,scale))  
          else '     ' end,  
   'Nullable'    = case when is_nullable = 0 then @no else @yes end,  
   'TrimTrailingBlanks' = case ColumnProperty(object_id, name, 'UsesAnsiTrim')  
          when 1 then @no  
          when 0 then @yes  
          else '(n/a)' end,  
   'FixedLenNullInSource' = case  
      when type_name(system_type_id) not in ('varbinary','varchar','binary','char')  
       then '(n/a)'  
      when is_nullable = 0 then @no else @yes end,  
   'Collation'  = collation_name  
  from sys.all_columns where object_id = @objid  
  
  -- IDENTITY COLUMN?  
  if @sysobj_type in ('S ','U ','V ','TF') and @objid > 0  
  begin  
   print ' '  
   declare @colname sysname  
   select @colname = col_name(@objid, column_id) from sys.identity_columns where object_id = @objid  
   select  
    'Identity'    = isnull(@colname,'No identity column defined.'),  
    'Seed'    = ident_seed(@objname),  
    'Increment'   = ident_incr(@objname),  
    'Not For Replication' = ColumnProperty(@objid, @colname, 'IsIDNotForRepl')  
   -- ROWGUIDCOL?  
   print ' '  
   select @colname = null  
   select @colname = name from sys.columns where object_id = @objid and is_rowguidcol = 1  
   select 'RowGuidCol' = isnull(@colname,'No rowguidcol column defined.')  
  end  
 end  
  
 -- DISPLAY ANY PARAMS  
 if exists (select * from sys.all_parameters where object_id = @objid)  
 begin  
  -- INFO ON PROC PARAMS  --查询存储过程参数--object_id=object_id(@TableName)
  print ' '  
  select  
   'Parameter_name' = name,  
   'Type'   = type_name(user_type_id),  
   'Length'   = max_length,  
   'Prec'   = case when type_name(system_type_id) = 'uniqueidentifier' then precision  
        else OdbcPrec(system_type_id, max_length, precision) end,  
   'Scale'   = OdbcScale(system_type_id, scale),  
   'Param_order'  = parameter_id,  
   'Collation'   = convert(sysname, case when system_type_id in (35, 99, 167, 175, 231, 239)  
      then ServerProperty('collation') end)  
  
  from sys.all_parameters where object_id = @objid  
 end  
  
 -- DISPLAY TABLE INDEXES & CONSTRAINTS  
 if @sysobj_type in ('S ','U ')  
 begin  
  print ' '  
  EXEC sys.sp_objectfilegroup @objid  
  print ' '  
  EXEC sys.sp_helpindex @objname  
  print ' '  
  EXEC sys.sp_helpconstraint @objname,'nomsg'  
  if (select count(*) from sysdepends where depid = @objid and deptype = 1) = 0  
  begin  
   raiserror(15647,-1,-1,@objname) -- No views with schemabinding reference table '%ls'.  
  end  
  else  
  begin  
            select distinct 'Table is referenced by views' = obj.name from sys.objects obj, sysdepends deps  
    where obj.type ='V' and obj.object_id = deps.id and deps.depid = @objid  
     and deps.deptype = 1 group by obj.name  
  
  end  
 end  
 else if @sysobj_type in ('V ') and @objid > 0  
 begin  
  -- VIEWS DONT HAVE CONSTRAINTS, BUT PRINT THESE MESSAGES BECAUSE 6.5 DID  
  print ' '  
  raiserror(15469,-1,-1,@objname) -- No constraints defined  
  print ' '  
  raiserror(15470,-1,-1,@objname) -- No foreign keys reference table '%ls'.  
  EXEC sys.sp_helpindex @objname  
 end  
  
 return (0) -- sp_help  

sp_help null

相关文章
|
2月前
|
SQL 监控 关系型数据库
一键开启百倍加速!RDS DuckDB 黑科技让SQL查询速度最高提升200倍
RDS MySQL DuckDB分析实例结合事务处理与实时分析能力,显著提升SQL查询性能,最高可达200倍,兼容MySQL语法,无需额外学习成本。
|
2月前
|
SQL 存储 关系型数据库
MySQL体系结构详解:一条SQL查询的旅程
本文深入解析MySQL内部架构,从SQL查询的执行流程到性能优化技巧,涵盖连接建立、查询处理、执行阶段及存储引擎工作机制,帮助开发者理解MySQL运行原理并提升数据库性能。
|
6月前
|
SQL 数据挖掘 数据库
第三篇:高级 SQL 查询与多表操作
本文深入讲解高级SQL查询技巧,涵盖多表JOIN操作、聚合函数、分组查询、子查询及视图索引等内容。适合已掌握基础SQL的学习者,通过实例解析INNER/LEFT/RIGHT/FULL JOIN用法,以及COUNT/SUM/AVG等聚合函数的应用。同时探讨复杂WHERE条件、子查询嵌套,并介绍视图简化查询与索引优化性能的方法。最后提供实践建议与学习资源,助你提升SQL技能以应对实际数据处理需求。
494 1
|
2月前
|
SQL 监控 关系型数据库
SQL优化技巧:让MySQL查询快人一步
本文深入解析了MySQL查询优化的核心技巧,涵盖索引设计、查询重写、分页优化、批量操作、数据类型优化及性能监控等方面,帮助开发者显著提升数据库性能,解决慢查询问题,适用于高并发与大数据场景。
|
1月前
|
SQL 关系型数据库 MySQL
(SQL)SQL语言中的查询语句整理
查询语句在sql中占了挺大一部分篇幅,因为在数据库中使用查询语句的次数远多于更新与删除命令。而查询语句比起其他语句要更加的复杂,可因为sql是数据库不可或缺的一部分,所以即使不懂,也必须得弄懂,以上。
215 0
|
3月前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
283 18
|
2月前
|
SQL Web App开发 安全
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
150 0
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
|
3月前
|
SQL 容灾 安全
云时代SQL Server的终极答案:阿里云 RDS SQL Server如何用异地容灾重构系统可靠性
在数字化转型的浪潮中,数据库的高可用性已成为系统稳定性的生命线。作为经历过多次生产事故的资深开发者,肯定深知传统自建SQL Server架构的脆弱性——直到遇见阿里云 RDS SQL Server,其革命性的异地容灾架构彻底改写了游戏规则。
|
8月前
|
SQL 运维 监控
SQL查询太慢?实战讲解YashanDB SQL调优思路
本文是Meetup第十期“调优实战专场”的第二篇技术文章,上一篇《高效查询秘诀,解码YashanDB优化器分组查询优化手段》中,我们揭秘了YashanDB分组查询优化秘诀,本文将通过一个案例,助你快速上手YashanDB慢日志功能,精准定位“慢SQL”后进行优化。

热门文章

最新文章