PCB MS SQL 通过表名查询各字段信息和vb.net C# module类代码

正式表:各字段内容获取

DECLARE @tabname VARCHAR(20)
SET @tabname = ‘ppeflow‘

SELECT  @tabname AS ‘表名‘
     ,(CASE
            WHEN T.name = ‘varchar‘ or T.name = ‘char‘ THEN T.name + ‘(‘ + CAST(COLUMNPROPERTY(C.id,C.name,‘PRECISION‘) as  VARCHAR(20))+ ‘)‘
            WHEN T.name = ‘numeric‘ THEN T.name + ‘(‘ + CAST(COLUMNPROPERTY(C.id,C.name,‘PRECISION‘) as  VARCHAR(20)) + ‘,‘ +  CAST(isnull(COLUMNPROPERTY(c.id,c.name,‘Scale‘),0) as  VARCHAR(20)) + ‘)‘
            WHEN T.name = ‘decimal‘ THEN T.name + ‘(‘ + CAST(COLUMNPROPERTY(C.id,C.name,‘PRECISION‘) as  VARCHAR(20)) + ‘,‘ +  CAST(isnull(COLUMNPROPERTY(c.id,c.name,‘Scale‘),0) as  VARCHAR(20)) + ‘)‘
             ELSE T.name
      END) AS ‘字段类型与容量‘
     ,C.name as [字段名],T.name as [字段类型]
     ,convert(bit,C.IsNullable)  as [可否为空]
     ,convert(bit,case when exists(SELECT 1 FROM sysobjects where xtype=‘PK‘ and parent_obj=c.id and name in (
         SELECT name FROM sysindexes WHERE indid in(
             SELECT indid FROM sysindexkeys WHERE id = c.id AND colid=c.colid))) then 1 else 0 end)
                 as [是否主键]
     ,convert(bit,COLUMNPROPERTY(c.id,c.name,‘IsIdentity‘)) as [自动增长]
     ,C.Length as [占用字节]
     ,COLUMNPROPERTY(C.id,C.name,‘PRECISION‘) as [长度]
     ,isnull(COLUMNPROPERTY(c.id,c.name,‘Scale‘),0) as [小数位数]
     ,ISNULL(CM.text,‘‘) as [默认值]
     ,isnull(ETP.value,‘‘) AS [字段描述]  

     ,(‘Property ‘ + C.name + ‘ AS ‘ +
        CASE T.name
                  WHEN  ‘varchar‘ THEN  ‘String‘
                  WHEN  ‘nvarchar‘ THEN  ‘String‘
                  WHEN  ‘char‘ THEN  ‘String‘
                  WHEN  ‘numeric‘ THEN  ‘Decimal‘
                  WHEN  ‘decimal‘ THEN  ‘Decimal‘
                  WHEN  ‘datetime‘ THEN  ‘DateTime‘
                  WHEN  ‘int‘ THEN  ‘Integer‘
                  WHEN  ‘smallint‘ THEN  ‘Short‘
                  WHEN  ‘real‘ THEN  ‘Single‘
                  WHEN  ‘float‘ THEN  ‘Double‘
                  WHEN  ‘bit‘ THEN  ‘Boolean‘
                  WHEN  ‘uniqueidentifier‘ THEN  ‘Guid‘
        END ) ‘VB.net‘

     ,(‘public ‘ +
        CASE T.name
                  WHEN  ‘varchar‘ THEN  ‘string‘
                  WHEN  ‘nvarchar‘ THEN  ‘string‘
                  WHEN  ‘char‘ THEN  ‘string‘
                  WHEN  ‘numeric‘ THEN  ‘decimal‘
                  WHEN  ‘decimal‘ THEN  ‘decimal‘
                  WHEN  ‘datetime‘ THEN  ‘DateTime‘
                  WHEN  ‘int‘ THEN  ‘int‘
                  WHEN  ‘smallint‘ THEN  ‘short‘
                  WHEN  ‘real‘ THEN  ‘float‘
                  WHEN  ‘float‘ THEN  ‘double‘
                  WHEN  ‘bit‘ THEN  ‘bool‘
                  WHEN  ‘uniqueidentifier‘ THEN  ‘Guid‘
        END
        + ‘ ‘ + C.name  + ‘ { get; set; }‘) ‘C#‘

     --,ROW_NUMBER() OVER (ORDER BY C.name) AS [Row]
     --,REPLACE(REPLACE(REPLACE(‘row_T_WF_MAIN("‘+c.name+‘") = ‘  +  STUFF(STUFF(isnull(CM.text,‘‘),1,1,‘‘),LEN(STUFF(isnull(CM.text,‘‘),1,1,‘‘)),1,‘‘),‘‘‘‘,‘"‘),‘getdate()‘,‘Date.Now()‘),‘newid()‘,‘Guid.NewGuid‘)AS‘row‘
FROM syscolumns C
INNER JOIN systypes T ON C.xusertype = T.xusertype
left JOIN sys.extended_properties ETP   ON  ETP.major_id = c.id AND ETP.minor_id = C.colid AND ETP.name =‘MS_Description‘
left join syscomments CM on C.cdefault=CM.id
WHERE C.id = object_id(@tabname)  

--AND convert(bit,C.IsNullable) = 0  --不为空
--AND ISNULL(CM.text,‘‘) = ‘‘--默认值为空

临时表:各字段内容获取

  SELECT  

     C.name as [字段名],T.name as [字段类型]
     ,convert(bit,C.IsNullable)  as [可否为空]
     ,convert(bit,case when exists(SELECT 1 FROM sysobjects where xtype=‘PK‘ and parent_obj=c.id and name in (
         SELECT name FROM sysindexes WHERE indid in(
             SELECT indid FROM sysindexkeys WHERE id = c.id AND colid=c.colid))) then 1 else 0 end)
                 as [是否主键]
     ,convert(bit,COLUMNPROPERTY(c.id,c.name,‘IsIdentity‘)) as [自动增长]
     ,C.Length as [占用字节]
     ,COLUMNPROPERTY(C.id,C.name,‘PRECISION‘) as [长度]
     ,isnull(COLUMNPROPERTY(c.id,c.name,‘Scale‘),0) as [小数位数]
     ,ISNULL(CM.text,‘‘) as [默认值]
     ,isnull(ETP.value,‘‘) AS [字段描述]
     ,(‘Property ‘ + C.name + ‘ AS ‘ +
        CASE T.name
                  WHEN  ‘varchar‘ THEN  ‘string‘
                  WHEN  ‘char‘ THEN  ‘string‘
                  WHEN  ‘numeric‘ THEN  ‘Decimal‘
                  WHEN  ‘decimal‘ THEN  ‘Decimal‘
                  WHEN  ‘datetime‘ THEN  ‘DateTime‘
                  WHEN  ‘int‘ THEN  ‘Integer‘
                  WHEN  ‘real‘ THEN  ‘Single‘
                  WHEN  ‘float‘ THEN  ‘Double‘
        END ) ‘VB.net‘

     ,(‘public ‘ +
        CASE T.name
                  WHEN  ‘varchar‘ THEN  ‘string‘
                  WHEN  ‘char‘ THEN  ‘string‘
                  WHEN  ‘numeric‘ THEN  ‘decimal‘
                  WHEN  ‘decimal‘ THEN  ‘decimal‘
                  WHEN  ‘datetime‘ THEN  ‘DateTime‘
                  WHEN  ‘int‘ THEN  ‘int‘
                  WHEN  ‘real‘ THEN  ‘float‘
                  WHEN  ‘float‘ THEN  ‘double‘
                  WHEN  ‘bit‘ THEN  ‘bool‘
        END
        + ‘ ‘ + C.name  + ‘ { get; set; }‘) ‘C#‘
     --,ROW_NUMBER() OVER (ORDER BY C.name) AS [Row]
FROM tempdb..syscolumns C
INNER JOIN systypes T ON C.xusertype = T.xusertype
left JOIN sys.extended_properties ETP   ON  ETP.major_id = c.id AND ETP.minor_id = C.colid AND ETP.name =‘MS_Description‘
left join syscomments CM on C.cdefault=CM.id
WHERE C.id = object_id(‘tempdb..#tab2‘)   

获取内容样式如下:

原文地址:https://www.cnblogs.com/pcbren/p/9248282.html

时间: 2024-11-07 23:47:04

PCB MS SQL 通过表名查询各字段信息和vb.net C# module类代码的相关文章

SQL Server表名为什么要加方括号?

我刚安装了Microsoft SQL Server 2000个人版,发现它的SQL语句怎么和标准不一样.比如我使用最简单的查询语句"select * from user"查询表user,结果它提示语法错误,后来系统自动给我纠正过来变成"select * from [user]",查询才成功执行了.也就是说要给表名添加方括号才是正确的格式. 这个不是必须要加,但表名或字段名如果引用了sqlserver中的关键字,数据库会不识别这到底是关键字还是表名(或字段名)时就必须

SQL语句对于多表联合查询重复字段的特殊语法处理

SQL语句对于多表联合查询重复字段的特殊语法处理 原创 2016年01月13日 14:22:36 5309 [sql] view plain copy SELECT m.*,d_name,r_name,m2.m_account as shangji_account FROM Member m,Member m2,Role r,Depart d WHERE m.m_rid=r.id and m.m_did=d.id and m.m_shangji=m2.id ORDER BY m.id DESC

图解SQL多表关联查询

图解SQL多表关联查询 网上看了篇文章关于多表连接的,感觉很好,记录下来,以便日后自己学习  内连接 左连接 右连接 全外连接

SQL两表关联查询&批量修改字段值

SQL关联查询&修改字段,正确范例如下: --批量修改报告单位名称&更新时间 --tt和tp两表关联查询,将符合条件的tt表中的principal字段更新到tp表的ruperson字段 merge into nhis34.t_publicplaces tp using standard.t_organization tt on (tt.orgcode = tp.r_orgcode and tp.create_time > '2015-05-07 00:00:00') when mat

SQL多表连接查询

SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中不会这样建表,实际开发中这两个表会有自己不同的主键.) 一.外连接 外连接可分为:左连接.右连接.完全外连接. 1.左连接  left join 或 left outer join SQL语句:select * from student left join course on student.

Mybatis中如何在SQL语句表名中使用参数

insert into prefix_${table_name} (a, b, c) values (#{a}, #{b}, #{c}) ${} 表示直接使用字面量(literal value) #{} 表示这个是个参数 如果 table_name 是 "ABC" 则 ${table_name} 是 ABC #{table_name} 是 "ABC" Mybatis中如何在SQL语句表名中使用参数,布布扣,bubuko.com

SQL实现表名更改,列名更改,约束更改

SQL实现表名更改,列名更改,约束更改 (转) 修改表名 格式:sp_rename tablename,newtablename sp_rename tablename,newtablename 修改字段名 格式:sp_rename 'tablename.colname',newcolname,'column' sp_rename 'tablename.colname',newcolname,'column' 添加字段 格式:alter table table_name add new_colu

sql数据库表名做参数

declare @tablename nvarchar(50)declare @count intset @tablename = 'SplitzRank'declare @sqlstr nvarchar(512)set @sqlstr = 'select @count=count(*) from '[email protected]select @sqlstrexec sp_executesql @sqlstr,N'@count int output',@count outputselect

PCB MS SQL 排序应用---SQL相邻数据区间值求解

其中一篇 博文中有写<PCB MS SQL 排序应用---相邻数据且相同合并处理>此篇有也应相用也同的技巧,实现相邻数据区间值求解 示例: 原数据:处理前 求出区间值:处理后 SQL 代码实现 DECLARE @table table(RowNo INT,FlowTechName VARCHAR(20)) INSERT INTO @table(RowNo,FlowTechName) VALUES(1,'沉铜') INSERT INTO @table(RowNo,FlowTechName) VA