SqlServer表属性查询

获得表信息:

select
    syscolumns.name as field,
    syscolumns.isnullable as nullis,
    systypes.name as sqltype,
    syscolumns.[length] as lenth,
    ISNULL(sys.identity_columns.is_identity,0) as identi,
    ISNULL(sys.extended_properties.value,‘‘) as summary
from sysobjects
    join syscolumns on sysobjects.id = syscolumns.id
    join systypes on syscolumns.xusertype = systypes.xusertype
    left join sys.identity_columns
    on sys.identity_columns.object_id = syscolumns.id and sys.identity_columns.column_id = syscolumns.colid
    left join sys.extended_properties on sys.extended_properties.major_id = syscolumns.id
    and sys.extended_properties.minor_id = syscolumns.colid
where sysobjects.name = ‘TableName‘

判断表是否存在:

if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[TableName]‘) and OBJECTPROPERTY(id, N‘IsUserTable‘) = 1)
    select ‘true‘
else
    select ‘false‘  
<p><strong>根据表外键名称获得主键表名称</strong></p>
SELECT
外键表ID = b.fkeyid ,
外键表名称 = object_name (b.fkeyid) ,
外键列ID = b.fkey ,
外键列名 = ( SELECT name FROM syscolumns WHERE colid = b.fkey AND id = b.fkeyid) ,
主键表ID = b.rkeyid ,
主键表名= object_name (b.rkeyid) ,
主键列ID = b.rkey ,
主键列名 = ( SELECT name FROM syscolumns WHERE colid = b.rkey AND id = b.rkeyid) ,
级联更新 = ObjectProperty (a.id, ‘ CnstIsUpdateCascade ‘ ) ,
级联删除 = ObjectProperty (a.id, ‘ CnstIsDeleteCascade ‘ )
FROM sysobjects a
 join sysforeignkeys b on a.id = b.constid
 join sysobjects c on a.parent_obj = c.id
where a.xtype = ‘f‘ AND c.xtype = ‘U‘ and a.name = ‘News‘ 

select
object_name (b.fkeyid),
外键列名 = ( SELECT name FROM syscolumns WHERE colid = b.fkey AND id = b.fkeyid)
 from sysobjects as a join  sysforeignkeys as b on a.id=b.constid
where a.xtype=‘F‘

判断字段是否在表中已存在

if exists(select * from syscolumns where id=object_id(‘table‘) and name=‘cloumn‘) select ‘true‘ else select ‘false‘

判断字段是否在表中已存在_Oracle

select 1 from all_Tab_Columns where table_name = upper(‘studentinfo‘) and column_name = upper(‘class‘);

删除指定表中的所有索引

/*
    删除指定表的所有索引,包括主键索引,唯一索引和普通索引

    调用:
    declare @tbName varchar(20)
    set @tbName=‘CP_PATHINFO‘
    exec sp_dropindex @tbName

    vivianfdlpw 2005.9 引用情保留此信息
*/
if exists(select 1 from sysobjects where id=object_id(‘sp_dropindex‘) and xtype=‘P‘)
drop procedure sp_dropindex
go
create procedure sp_dropindex
@tbName varchar(20)=null   --索引名
as

if @tbName is null
begin
     raiserror(‘必须提供@tbName参数‘,12,1)
     return
end

create table #
(
   id int identity,
   index_name varchar(50),
   index_description varchar(1000),
   index_keys varchar(100)
)
insert #(index_name,index_description,index_keys)
exec sp_helpindex @tbName

declare @i int,@sql varchar(100)
set @i=1

while @i<=(select max(id) from #)
begin
      if exists(select 1
                from sysobjects A
                join # B on A.name=B.index_name
                where B.id=@i and A.xtype in (‘PK‘,‘UQ‘))
      begin
           select @sql=‘alter table ‘+@tbName+‘ drop constraint ‘
                       +(select index_name from # where id=@i)
           exec(@sql)
      end
      else
      begin
           select @sql=‘drop index ‘+@tbName+‘.‘
                       +(select index_name from # where id=@i)
           exec(@sql)
      end

      set @i=@i+1
end

drop table #

go

create index IX_SACAG on CP_PATHINFO
(
    REMIND_TODAY
)

    declare @tbName varchar(20)
    set @tbName=‘CP_PATHINFO‘
    exec sp_dropindex @tbName
时间: 2024-10-09 23:10:44

SqlServer表属性查询的相关文章

sqlserver表结构查询语句

SELECT syscolumns.name,systypes.name,syscolumns.isnullable,syscolumns.length FROM syscolumns, systypes WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id= object_id('T_OA_WLWZ_DJB') sqlserver表结构查询语句

MVC5+EF6简单实例---以原有SQLServer数据库两表联合查询为例

工具:VS.net2013.EF6.MVC5.SQLServer2008 参考出处: http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html http://www.cnblogs.com/miro/p/4288184.html http://www.cnblogs.com/dotnetmvc/p/3732029.html 一.准备工作 在SqlServer上创建数据库:Element 模拟两个表并插入数据:SysU

SQLServer 表结构相关查询(快速了解数据库)

-- 表结构查询 SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = a.name, 标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 主键

SQLServer学习(多表连接查询)(四)

(1)双表内部连接查询 ①在where中指定内部连接的条件(这种写法使用的很少) SELECT WorkNo,Name,DeptName,SignImg FROM S_A_User,S_A_UserImg WHERE S_A_User.ID=S_A_UserImg.UserID ORDER BY DeptName,Name ②在from中指定内部连接的条件(推荐使用) SELECT WorkNo,Name,DeptName,SignImg FROM S_A_User INNER JOIN S_A

010.简单查询、分组统计查询、多表连接查询(sql实例)

-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SELECT UNION --SELECT 'HAHA',1,32--UNION ALL --全部显示/显示重复数据 即使集合相同--UNION---------将查询的两个结果集合并.结构必须一致 -->常见面试题 --SELECT 'HEHE',2,33------将查询结果添加到列表中(子查询)IN

openlayers实现wfs属性查询和空间查询

概述: 一直在寻求openlayers中wfs加载和属性查询的相关操作,功夫不负有心人,蓦然回首,那人却在灯火阑珊处,找到了这篇博文:http://blog.csdn.net/longshengguoji/article/details/39377931,试了下,在IE8中正常运行,但是在chrom中涉及到跨域的问题,待后期接解决吧.本文讲解如何通过wfs实现属性的查询与展示. 效果: 初始化状态 属性查询结果 空间查询结果 数据表: 关键代码: 添加wfs图层 wfs = new OpenLa

RDIFramework.NET 中多表关联查询分页实例

RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为指导思想,作为异质系统整合与互操作性.分布式应用提供了可行的解决方案. 分页非常的常见,基本任何项目都会涉及到分页,这没什么好谈的,大多数我们分页对单表的分页比较多,对多表的分页我们可以通过视图来实现,当然还有其他的方式,在这儿,我以一个实例展示下使用我们的RDIFramework.NET来实现多表

Yii2中多表关联查询(join、joinwith) with是不执行sql的

Yii2中多表关联查询(join.joinwith) 我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name customer_id book_id) 图书表 (id book_name author_id) 作者表 (id author_name) 模型定义 下面是这4个个模型的定义,只写出其中的关联 Customer class Customer extends \

转Oracle、MySql、SQLServer 数据分页查询

最近简单的对oracle,mysql,sqlserver2005的数据分页查询作了研究,把各自的查询的语句贴出来供大家学习..... (一). mysql的分页查询 mysql的分页查询是最简单的,借助关键字limit即可实现查询,查询语句通式: /* * sql:可以是单表的查询语句,也可以是多表的联合查询语句 * firstIndex:其实的索引 * pageSize:每页显示的记录数 */ select o.* from (sql) o limit firstIndex,pageSize