自定义实体的代码自动生成

来源:互联网 发布:mac自带压缩软件 编辑:程序博客网 时间:2024/04/29 07:36

if(object_id('generatecode')) is not null drop proc generatecode
go
CREATE proc generatecode
    
@table varchar(50)
as

declare @sqlstr varchar(2000)    
create table #tmp    
(    
    column_name 
varchar(20),    
    type_name 
varchar(20),    
    length  
varchar(10)    
)    

create table #text(n_text varchar(1000))

select @sqlstr ='      
insert into #tmp (column_name,type_name,length) select name,type_name,a.prec    
    from syscolumns a ,master.dbo.spt_datatype_info b    
    where id = object_id(N
'''+@table+'''
    and OBJECTPROPERTY(id, N
''IsUserTable'') =1    
    and  a.xtype=b.ss_dtype 
    and  a.length = isnull(b.fixlen, a.length)    
    AND isnull(b.AUTO_INCREMENT,0) = isnull(ColumnProperty (a.id, a.name, 
''IsIdentity''),0)    
    AND (b.ODBCVer is null or b.ODBCVer = 2)    
'     
execute(@sqlstr)

declare @name varchar(20)
declare @type varchar(20)
declare cur cursor scroll for select column_name,type_name from #tmp
open cur
fetch next from cur into @name,@type
while @@fetch_status = 0
begin
    
insert into #text values('private '+ case when @type='varchar' or @type='nvarchar' or @type='char' or @type='text' or @type='ntext' then 'String ' + @name + ';'
                
when @type='int' or @type='int identity' then 'int ' + @name + ';'
                
when @type='datetime' or @type='smalldatetime' then 'DateTime ' + @name + ';' 
                
when @type='bit' then 'bool ' + @name + ';' 
                
when @type='decimal' or @type='numeric' or @type='money' or @type='smallmoney' then 'decimal ' + @name + ';' 
                
when @type='float' then 'double ' + @name + ';' 
                
when @type='binary' or @type='image' or @type='varbinary' or @type='timestamp' then 'byte[] ' + @name + ';' 
                
when @type='real' then 'single ' + @name + ';' 
                
end
    )
    
fetch next from cur into @name,@type
end

fetch absolute 1 from cur into @name,@type
while @@fetch_status = 0
begin
    
insert into #text values('public '+ case when @type='varchar' or @type='nvarchar' or @type='char' or @type='text' or @type='ntext' then 'String ' + upper(@name)
                
when @type='int' or @type='int identity' then 'int ' + upper(@name)
                
when @type='datetime' or @type='smalldatetime' then 'DateTime ' + upper(@name)
                
when @type='bit' then 'bool ' + upper(@name
                
when @type='decimal' or @type='numeric' or @type='money' or @type='smallmoney' then 'decimal ' + @name
                
when @type='float' then 'double ' + @name
                
when @type='binary' or @type='image' or @type='varbinary' or @type='timestamp' then 'byte[] ' + @name
                
when @type='real' then 'single ' + @name
                
end
    )
    
insert into #text values('{')
    
insert into #text values('get{return this.' + @name + ';}')
    
insert into #text values('set{this.' + @name + ' = value;}')
    
insert into #text values('}')
    
fetch next from cur into @name,@type
end
close cur
deallocate cur
select * from #text
drop table #tmp
drop table #text
GO

exec generatecode 'userinfo'



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1181548

[点击此处收藏本文]   guoguo19811025发表于 2006年09月05日 21:17:00


 
emo 发表于2006-09-06 11:53:00  IP: 61.29.197.*
恩,有个小错误,@type='numberic' ,应该是numeric吧?

 
vfp_system 发表于2006-09-06 22:58:00  IP: 58.20.32.*
不错,省了我好多事啦。生成的代码只要小小改动即可用。

 
蝈蝈 发表于2006-09-07 08:12:00  IP: 221.6.19.*
感谢emo帮我指出错误,即刻更改,谢谢!