September 20, 2010

Generating Class from table -TSQL

 
 
declare @TABLE_NAME varchar(30) ='Y2010_11_Acc_T_Ledger' ;
declare @str varchar(Max)  ='';
select @str= @str+ 'private '+case
when DATA_TYPE ='varchar' then 'string'
when DATA_TYPE ='datetime' then 'DateTime'
when DATA_TYPE ='date' then 'DateTime'
when DATA_TYPE ='bit' then 'bool'
when DATA_TYPE ='char' then 'char[]'
when DATA_TYPE ='numeric' then 'float'
else DATA_TYPE end
+'  _'+Column_Name++';'+CHAR(13) + CHAR(10)  from Information_schema.Columns with (nolock) where TABLE_NAME =@TABLE_NAME
print @str+CHAR(13) + CHAR(10)+CHAR(13) + CHAR(10)
 
set @str=''
select @str= @str+ 'public '+case
when DATA_TYPE ='varchar' then 'string'
when DATA_TYPE ='datetime' then 'DateTime'
when DATA_TYPE ='date' then 'DateTime'
when DATA_TYPE ='bit' then 'bool'
when DATA_TYPE ='char' then 'char[]'
when DATA_TYPE ='numeric' then 'float'
else DATA_TYPE end
+' '+Column_Name+CHAR(13) + CHAR(10) +'{'+CHAR(13) + CHAR(10)
+' get { return _'+Column_Name+'; } '+CHAR(13)+ CHAR(10)
+' set { _'+Column_Name+'= value;} '+CHAR(13) +CHAR(10)+'}'+CHAR(13) + CHAR(10)+CHAR(13) + CHAR(10) from Information_schema.Columns with (nolock) where TABLE_NAME =@TABLE_NAME

print @str