-
-
Save hur1can3/8411e35f19bcb98e400ee3bd484cb7c5 to your computer and use it in GitHub Desktop.
Modified original GIST to accommodate c# nullable types (?). And also come from a #temptable. Use lowercase for primitive types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if object_id('tempdb..#TempTableClassName') is not null | |
| drop table #TempTableClassName | |
| SELECT TOP 1 * | |
| INTO #TempTableClassName | |
| FROM table | |
| WHERE id = 1; | |
| declare @TempTable VARCHAR(255)= '#TempTableClassName%'; | |
| declare @tableName varchar(200) | |
| declare @columnName varchar(200) | |
| declare @nullable varchar(50) | |
| declare @datatype varchar(50) | |
| declare @maxlen int | |
| declare @sType varchar(50) | |
| declare @sProperty varchar(200) | |
| DECLARE table_cursor CURSOR FOR | |
| SELECT TABLE_NAME | |
| FROM [TEMPDB].[INFORMATION_SCHEMA].[TABLES] | |
| WHERE TABLE_NAME LIKE @TempTable; | |
| OPEN table_cursor | |
| FETCH NEXT FROM table_cursor | |
| INTO @tableName | |
| WHILE @@FETCH_STATUS = 0 | |
| BEGIN | |
| PRINT 'public class ' + REPLACE(REPLACE(@TempTable, '#', ''), '%', '') + ' {' | |
| DECLARE column_cursor CURSOR FOR | |
| SELECT COLUMN_NAME, IS_NULLABLE, DATA_TYPE, isnull(CHARACTER_MAXIMUM_LENGTH,'-1') | |
| from [TEMPDB].[INFORMATION_SCHEMA].[COLUMNS] | |
| WHERE [TABLE_NAME] = @tableName | |
| order by [ORDINAL_POSITION] | |
| OPEN column_cursor | |
| FETCH NEXT FROM column_cursor INTO @columnName, @nullable, @datatype, @maxlen | |
| WHILE @@FETCH_STATUS = 0 | |
| BEGIN | |
| -- datatype | |
| select @sType = case @datatype | |
| when 'int' then | |
| case when @nullable = 'YES' then 'int?' | |
| else 'int' | |
| END | |
| when 'decimal' then | |
| case when @nullable = 'YES' then 'decimal?' | |
| else 'decimal' | |
| END | |
| when 'money' then | |
| case when @nullable = 'YES' then 'decimal?' | |
| else 'decimal' | |
| END | |
| when 'char' then 'string' | |
| when 'nchar' then 'string' | |
| when 'varchar' then 'string' | |
| when 'nvarchar' then 'string' | |
| when 'uniqueidentifier' then | |
| case when @nullable = 'YES' then 'Guid?' | |
| else 'Guid' | |
| END | |
| when 'datetime' then | |
| case when @nullable = 'YES' then 'DateTime?' | |
| else 'DateTime' | |
| END | |
| when 'bit' then | |
| case when @nullable = 'YES' then 'bool?' | |
| else 'bool' | |
| END | |
| else 'string' | |
| END | |
| SELECT @sProperty = 'public ' + @sType + ' ' + @columnName + ' { get; set;}' | |
| PRINT @sProperty | |
| print '' | |
| FETCH NEXT FROM column_cursor INTO @columnName, @nullable, @datatype, @maxlen | |
| END | |
| CLOSE column_cursor | |
| DEALLOCATE column_cursor | |
| print '}' | |
| print '' | |
| FETCH NEXT FROM table_cursor | |
| INTO @tableName | |
| END | |
| CLOSE table_cursor | |
| DEALLOCATE table_cursor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment