Translate

> > SQL Store Procedure To Class Wrapper

SQL Store Procedure To Class Wrapper

Posted on Wednesday, January 3, 2018 | No Comments



DECLARE @sql NVARCHAR(MAX) = N'EXEC ShopMenu_GermanClub.dbo.SP_ReportRM_PO_Challan ww,admin,Y;';

--SELECT name, system_type_name
--    FROM sys.dm_exec_describe_first_result_set(@sql, NULL, 1) AS col


declare @TableName sysname = 'SP_ReportRM_PO_Challan'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'

select @Result = @Result + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
    select
        replace(col.name, ' ', '_') ColumnName,
       
        case --col.system_type_name
            WHEN (CHARINDEX('nvarchar',system_type_name)>0) then 'string'
            when (system_type_name='bigint') then 'long'
            when (system_type_name='binary') then 'byte[]'
            when (system_type_name='bit') then 'bool'
            when (system_type_name='char') then 'string'
            when (system_type_name='date') then 'DateTime'
            when (system_type_name='datetime') then 'DateTime'
            when (system_type_name='datetime2') then 'DateTime'
            when (system_type_name='datetimeoffset') then 'DateTimeOffset'
            when (CHARINDEX('decimal',system_type_name)>0) then 'decimal'
            when (system_type_name='float') then 'float'
            when (system_type_name='image') then 'byte[]'
            when (system_type_name='int') then 'int'
            when (system_type_name='money') then 'decimal'
            when (system_type_name='nchar') then 'string'
            when (system_type_name='ntext') then 'string'
            when (CHARINDEX('numeric',system_type_name)>0) then 'decimal'
            when (system_type_name='real') then 'double'
            when (system_type_name='smalldatetime') then 'DateTime'
            when (system_type_name='smallint') then 'short'
            when (system_type_name='smallmoney') then 'decimal'
            when (system_type_name='text') THEN 'string'
            when (system_type_name='time') then 'TimeSpan'
            when (system_type_name='timestamp') then 'DateTime'
            when (system_type_name='tinyint') then 'byte'
            when (system_type_name='uniqueidentifier') then 'Guid'
            when (system_type_name='varbinary') then 'byte[]'
            when (system_type_name='varchar') then 'string'
            ELSE ('UNKNOWN_') + col.system_type_name
        end ColumnType,
        case
            when col.is_nullable = 1 and col.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
            then '?'
            else ''
        end NullableSign
    -- name, system_type_name
    FROM sys.dm_exec_describe_first_result_set(@sql, NULL, 1) AS col

) t
--order by ColumnId

set @Result = @Result  + '
}'

print @Result

Leave a Reply

Powered by Blogger.