Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.16 KB | None | 0 0
  1. DECLARE @TableName sysname = 'Entidade'
  2. DECLARE @RESULT VARCHAR(MAX) = 'public class ' + @TableName + '
  3. {'
  4.  
  5. SELECT @RESULT = @RESULT + '
  6.    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'
  7. FROM
  8. (
  9.     SELECT
  10.         REPLACE(col.name, ' ', '_') ColumnName,
  11.         column_id ColumnId,
  12.         CASE typ.name
  13.             WHEN 'bigint' THEN 'long'
  14.             WHEN 'binary' THEN 'byte[]'
  15.             WHEN 'bit' THEN 'bool'
  16.             WHEN 'char' THEN 'string'
  17.             WHEN 'date' THEN 'DateTime'
  18.             WHEN 'datetime' THEN 'DateTime'
  19.             WHEN 'datetime2' THEN 'DateTime'
  20.             WHEN 'datetimeoffset' THEN 'DateTimeOffset'
  21.             WHEN 'decimal' THEN 'decimal'
  22.             WHEN 'float' THEN 'float'
  23.             WHEN 'image' THEN 'byte[]'
  24.             WHEN 'int' THEN 'int'
  25.             WHEN 'money' THEN 'decimal'
  26.             WHEN 'nchar' THEN 'string'
  27.             WHEN 'ntext' THEN 'string'
  28.             WHEN 'numeric' THEN 'decimal'
  29.             WHEN 'nvarchar' THEN 'string'
  30.             WHEN 'real' THEN 'double'
  31.             WHEN 'smalldatetime' THEN 'DateTime'
  32.             WHEN 'smallint' THEN 'short'
  33.             WHEN 'smallmoney' THEN 'decimal'
  34.             WHEN 'text' THEN 'string'
  35.             WHEN 'time' THEN 'TimeSpan'
  36.             WHEN 'timestamp' THEN 'DateTime'
  37.             WHEN 'tinyint' THEN 'byte'
  38.             WHEN 'uniqueidentifier' THEN 'Guid'
  39.             WHEN 'varbinary' THEN 'byte[]'
  40.             WHEN 'varchar' THEN 'string'
  41.             ELSE 'UNKNOWN_' + typ.name
  42.         END ColumnType,
  43.         CASE
  44.             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')
  45.             THEN '?'
  46.             ELSE ''
  47.         END NullableSign
  48.     FROM sys.COLUMNS col
  49.         JOIN sys.types typ ON
  50.             col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
  51.     WHERE object_id = object_id(@TableName)
  52. ) t
  53. ORDER BY ColumnId
  54.  
  55. SET @RESULT = @RESULT  + '
  56. }'
  57.  
  58. print @RESULT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement