sqlserver中利用存储过程打印出指定表的实体类
2021/10/18 2:15:30
本文主要是介绍sqlserver中利用存储过程打印出指定表的实体类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
CREATE PROC [dbo].[sp_createEntity] @tableName NVARCHAR(128) AS DECLARE @tableID INT; DECLARE @columnName NVARCHAR(64); DECLARE @dataType NVARCHAR(64); DECLARE @isNullable BIT; DECLARE @columnDesc NVARCHAR(256) = ''; DECLARE @columnMapDataType NVARCHAR(64)= ''; DECLARE @tempAttribute NVARCHAR(256)= ''; DECLARE @tempAttributeSummary NVARCHAR(1024)= ''; set @tableName=replace ( @tableName , '[' , '' ) set @tableName=replace ( @tableName , ']' , '' ) SET @tableID = -1; SELECT @tableID = object_id , @tableName = name FROM sys.tables WHERE LOWER(name) = LOWER(@tableName); IF ( @tableID = -1 ) BEGIN PRINT ( '输入的表名不存在!' ); END; DECLARE curtable CURSOR FOR SELECT CONVERT(NVARCHAR(50), A.name) AS columnName , CONVERT(NVARCHAR(50), LOWER(B.name)) AS dataType , A.is_nullable AS isNullable, CONVERT(NVARCHAR(256), C.value) AS columnDesc FROM sys.columns AS A LEFT JOIN sys.types AS B ON A.user_type_id = B.user_type_id LEFT JOIN sys.extended_properties AS C ON A.object_id = C.major_id AND A.column_id = C.minor_id WHERE A.object_id = @tableID; OPEN curtable; FETCH NEXT FROM curtable INTO @columnName, @dataType, @isNullable, @columnDesc; WHILE @@FETCH_STATUS = 0 BEGIN IF ( CHARINDEX(@dataType, 'datetime,smalldatetime') > 0 ) BEGIN SET @columnMapDataType = 'DateTime'; END; IF ( CHARINDEX(@dataType, 'bigint') > 0 ) BEGIN SET @columnMapDataType = 'long'; END; IF ( CHARINDEX(@dataType, 'tinyint') > 0 ) BEGIN SET @columnMapDataType = 'byte'; END; IF ( CHARINDEX(@dataType, 'int') = 1 ) BEGIN SET @columnMapDataType = 'int'; END; IF ( CHARINDEX(@dataType, 'smallint') = 1 ) BEGIN SET @columnMapDataType = 'short'; END; IF ( CHARINDEX(@dataType, 'float,real') > 0 ) BEGIN SET @columnMapDataType = 'float'; END; IF ( CHARINDEX(@dataType, 'binary,varbinary,image,timestamp') > 0 ) BEGIN SET @columnMapDataType = 'byte[]'; END; IF ( CHARINDEX(@dataType, 'bit') > 0 ) BEGIN SET @columnMapDataType = 'bool'; END; IF ( CHARINDEX(@dataType, 'decimal,smallmoney,money,numeric') > 0 ) BEGIN SET @columnMapDataType = 'decimal'; END; IF ( CHARINDEX(@dataType, 'uniqueidentifier') > 0 ) BEGIN SET @columnMapDataType = 'Guid'; END; IF ( CHARINDEX(@dataType, 'char,varchar,nchar,nvarchar,text,ntext') > 0 ) BEGIN SET @columnMapDataType = 'string'; END; IF ( CHARINDEX(@dataType, 'xml') > 0 ) BEGIN SET @columnMapDataType = 'XmlDocument'; END; IF ( @isNullable = 1 AND CHARINDEX(@columnMapDataType, 'string,XmlDocument') = 0 ) BEGIN SET @columnMapDataType = @columnMapDataType + '?'; END; SET @tempAttributeSummary = CHAR(10) + '/// <summary>' + CHAR(10) + '/// ' + @columnDesc + CHAR(10) + '/// </summary>'; SET @tempAttribute = 'public ' + @columnMapDataType + ' ' + @columnName + ' { get; set; }'; PRINT @tempAttributeSummary; PRINT @tempAttribute; FETCH NEXT FROM curtable INTO @columnName, @dataType, @isNullable, @columnDesc; END; CLOSE curtable; DEALLOCATE curtable; GO
测试:
exec sp_createEntity '表名'
这篇关于sqlserver中利用存储过程打印出指定表的实体类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)