SQL Server数据库字段说明的添加修改删除示例


  本文标签:SQL Server 数据库字段说明

  SQL Server数据库字段说明的添加、修改和删除以及查询数据库字段信息和类型的操作示例是本文我们主要要介绍的,接下来我们就开始一一介绍这部分内容,希望能够对您有所帮助  。

  1.查询两个表的字段说明

  

  1. SELECT t.[name] AS [表名],c.[name] AS [字段名],cast(ep.[value]   
  2. as varchar(100)) AS [字段说明]  
  3. FROM sys.tables AS t INNER JOIN sys.columns   
  4. AS c ON t.object_id = c.object_id LEFT JOIN sys.extended_properties AS ep   
  5. ON ep.major_id = c.object_id AND ep.minor_id = c.column_id WHERE ep.class =1   
  6. and t.[name]=table1 or t.[name]=table2  
  7. and c.[name] in (table2字段,table2字段)  
  8. or c.[name] in (table1字段,table1字段) 

  

  2.添加字段的名称

  

  1. EXEC  
  2. sys.sp_addextendedproperty @name=NMS_Description,  
  3. @value=N字段说明 , @level0type=NSCHEMA,@level0name=Ndbo,  
  4. @level1type=NTABLE,@level1name=N表名, @level2type=NCOLUMN,  
  5. @level2name=N字段名 
  6. GO 

  

  3.修改字段的名称

  

  1. BEGIN TRANSACTION  
  2. GO  
  3. DECLARE @v sql_variant  
  4. SET @v = N说明信息 
  5. EXECUTE sys.sp_updateextendedproperty NMS_Description,  
  6. @v, NSCHEMA,Ndbo,NTABLE,N表名, NCOLUMN, N字段名  
  7. GO  
  8. COMMIT 

  

  4.查询数据库字段信息和类型

  

  1. select a.name as zdname,a.length,b.name as zdtype from syscolumns a,systypes b,sysobjects c  
  2. where a.xtype=b.xtype and a.id=c.id and c.nametable --没有过滤系统字段信息  
  3. select a.name,a.length,b.name from syscolumns a,systypes b,sysobjects c   
  4. where a.xtype=b.xtype and a.id=c.id and c.nametable   
  5. AND B.NAME!=SYSNAME           --过滤了系统字段信息  
  6. select a.name,a.length,b.name from syscolumns a,systypes b,sysobjects c  
  7. where a.xtype=b.xtype and a.id=c.id and c.nametable and charindex(sysname,b.name) = 0  
  8. --过滤了系统字段信息 

  

  以上就是SQL Server数据库字段说明的添加、修改和删除操作的代码示例的全部内容,本文就介绍到这里了,希望本次的介绍能够对您有所收获!