首页 > 数据库 >SP_CreateModel,sql server 自动生成表的实体类的通用存储过程

SP_CreateModel,sql server 自动生成表的实体类的通用存储过程

时间:2023-07-25 11:24:46浏览次数:40  
标签:CreateModel 实体类 string decimal SP when col typ id

--调用该sp, 执行语句即可

--EXEC dbo.SP_CreateModel 't_Buss_LargeScreenTotalInfoByMonth'

 

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
ALTER PROCEDURE SP_CreateModel(
@TableName sysname
)
AS
BEGIN
--declare @TableName sysname = 'T1'
declare @Result varchar(max) = '
/// <summary>
/// ' + @TableName +

'
/// </summary>
public class ' + @TableName + '
{'

select @Result = @Result + '
/// <summary>
/// ' + CONVERT(NVARCHAR(500), ISNULL(ColName, '无')) +

'
/// </summary>
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
SELECT
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
prop.value ColName,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'float'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'char'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'double'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'DateTime'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id
where object_id = object_id(@TableName)
) t
--order by ColumnId

set @Result = @Result + '
}'

print @Result
END
GO

 

标签:CreateModel,实体类,string,decimal,SP,when,col,typ,id
From: https://www.cnblogs.com/itjeff/p/17579288.html

相关文章

  • CF1466I The Riddle of the Sphinx
    基本思路明示了在二进制下考虑问题,我们大体的思路就是从高往低依次确定最大的数二进制下每一位上的值。以下所述的「前缀」均指一个二进制数从高位到低位的一部分,一个元素的「前\(k\)位」表示二进制从高位到低位的前\(k\)位,\(res\)表示当前记录的最大前缀的长度。先看看操......
  • SpringSecurity整合Thymeleaf和后台的权限
    1.首先,我们需要在pom文件中添加以下依赖:<!--thymeleafspringsecurity5依赖--><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId></dependency><!--thymeleaf依赖-->&l......
  • IBM SSPS软件下载-中文简体官版下载 常用软件
    spssMac官方版是款功能比较实用的数据统计分析工具。spss正式版采用类似Excel表格的方式输入与管理数据,且数据接口也较为通用,用户可方便的从其他数据库中读入数据。并且spss还可以分布于通讯、医疗、银行、证券、保险、制造、商业、市场研究、科研教育等多个领域和行业。软件地址:......
  • spss-数据统计分析软件-spss下载 v21.0官方版 常用软件
    spss20.0中文正式版特色——它包含能够解析缺失数据的程序,不然缺失数据会对结果的有效性产生负面影响。——它支持企业所使用的所有常见的数据源。——统计功能和程序与数据分离开来,可减少错误风险。——开放的技术支持使用外部编程语言,因此您能够添加或定制附加功能。——各种模块......
  • RTSP/Onvif视频服务器LntonNVR(源码版)平台鉴权功能的应用场景和意义
    LntonNVR平台是基于RTSP/Onvif协议的视频接入、处理及分发平台,可以分发出RTSP、RTMP、WS-FLV、HTTP-FLV、HLS、WebRTC等格式的视频流,还可以提供API接口,便于用户调用集成。用户也能够直接将流地址放到第三方播放器进行播放。感兴趣的朋友也可以去CSDN(https://blog.csdn.net/LNTON)......
  • Authentication in asp.net core异常汇总
     services.AddAuthentication("Microsoft")      .AddMicrosoftIdentityWebApp(Configuration,"AzureAd","Microsoft");InvalidOperationException:Nosign-outauthenticationhandlerisregisteredforthescheme'OpenIdCon......
  • 建立模型类的方法学习 DecimalField ,models.SmallIntegerField ,get_pay_type_display(
    1. total_amount=models.DecimalField(max_digits=10,decimal_places=2,verbose_name="订单总价",default=0)DecimalField:这是一个Django模型字段类型,用于存储十进制数值,通常用于表示货币和其他需要高精度计算的数值max_digits=10:这是DecimalField中的一个参数,用于指......
  • SpringBoot中使用测试框架MockMvc来模拟HTTP请求测试Controller接口
    场景Java中进行单元测试junit.Assert断言、Mockito模拟对象、verify验证模拟结果、Java8中lambda的peek方法使用:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/127492361上面讲了开发过程中一些测试方法。如果需要在代码中直接测试某个Controller接口,除了每次启......
  • Spring
    概念Spring是什么Spring是分层的JavaSE/EE应用full-stack轻量级开源框架,以IoC(InverseOfControl:反转控制)和AOP(AspectOrientedProgramming:面向切面编程)为核心。提供了表现层SpringMVC和持久层SpringJDBCTemplate以及业务层事务管理等众多的企业级应用技术,还能整合......
  • SpringMVC
    概述SpringMVC是一种基于Java的实现MVC设计模型的请求驱动类型的轻量级Web框架,属于SpringFrameWork的后续产品,已经融合在SpringWebFlow中。SpringMVC已经成为目前最主流的MVC框架之一,并且随着Spring3.0的发布,全面超越Struts2,成为最优秀的MVC框架。它通过一套注......