分享
 
 
 

根据表名自动生成INSERT,UPDATE,DELETE,SELECT的SQL语句

王朝mssql·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

TBPROC

CREATE Procedure TbProc

@model varchar(2),@pagename varchar(32),@object varchar(32),@autoField varchar(32)=null

as

set nocount on

select @model=upper(@model)

select @pagename=lower(@pagename)

select @object=upper(@object)

declare @head varchar(100),@headfct varchar(105),@para varchar(1500),@content varchar(5000)

declare @paravar varchar(2000) ,@saveStr varchar(3000) ,@deleteStr varchar(500) ,@selectFieldStr varchar(1000) ,@returnPk varchar(250),@pkeyStr varchar(500),@pkeyParaStr varchar(500)

select @head='',@headfct='',@para='',@content=''

exec TbField @object,@autofield,@paravar output ,@saveStr output ,@deleteStr output ,@selectFieldStr output,@pkeyStr output,@pkeyParaStr output

select @head='create procedure p'+@model+@pagename+'_'

--Head

print '注意:单位换算和货币换算都是在存储过程里实现!'

--Sav

select @headfct=@head+'Sav'

select @para=@paravar

select @content=@saveStr

select @headfct+char(13)+@para+char(13)+'as'+char(13)+@content+char(10)+char(13)+'GO'+char(13)

--Tree

select @para=''

select @headfct=@head+'Tree'

select @content=@selectFieldStr

select @headfct+char(13)+@para+char(13)+'as'+char(13)+@content+char(10)+char(13)+'GO'+char(13)

--Del

select @headfct=@head+'Del'

select @para=@pkeyParaStr

--test

--select @paravar,charindex('@IsValid',@paravar)

if charindex('IsValid',@selectFieldStr)>0

select @deleteStr=replace(replace(@deleteStr,'delete','update'),'where','set IsValid=0 where')

select @content=@deleteStr

select @headfct+char(13)+@para+char(13)+'as'+char(13)+@content+char(10)+char(13)+'GO'+char(13)

--Back

select @headfct=@head+'Back'

select @content=@selectFieldStr

select @headfct+char(13)+@para+char(13)+'as'+char(13)+@content+char(10)+char(13)+'GO'+char(13)

GO

TbField

CREATE procedure TbField

@object varchar(32),@autofield varchar(32),@string varchar(2000) output,@saveStr varchar(3000) output,@deleteStr varchar(500) output,@selectFieldStr varchar(1000) output,@pkeyStr varchar(500) output,@pkeyParaStr varchar(500) output

as

set nocount on

select @object=upper(@object)

declare @nameStr varchar(1000),@varStr varchar(1000),@updStr varchar(1500),@pkeyvarStr varchar(500)--,@pkeyParaStr varchar(150),@pkeyStr varchar(250) 作为了输出参数

select @string='',@nameStr='',@varStr='',@updStr='',@pkeyvarStr='',@pkeyStr='',@pkeyParaStr=''

declare @moneyStr varchar(500)

select @moneyStr=''

declare @i smallint

select @i=1

declare @fieldtb table(pk int identity,field varchar(32))

declare @attrtb table(field varchar(32),typename varchar(32),length smallint)

declare @field varchar(32)

declare curfield cursor for

select name from syscolumns where id=object_id(@OBJECT)

open curfield

fetch next from curfield into @field

while @@fetch_status=0

begin

insert @fieldtb(field) values(@field)

select @nameStr=@nameStr+' '+@field +','

select @varStr=@varStr+'@'+@field +','

select @updStr=@updStr+@field+'=@'+@field+','

if len(@updStr)>@i*100

begin

select @updStr=@updStr+char(13)+char(9)+char(9)

select @i=@i+1

end

fetch next from curfield into @field

end

close curfield

deallocate curfield

insert @attrtb

select distinct c.name,replace(replace(d.type_name,'identity',''),'()',''),c.length

from syscolumns c

inner join master.dbo.spt_datatype_info d on c.xtype = d.ss_dtype

where c.id=object_id(@OBJECT)

-----select * from @attrtb --测试

select @i=1

declare @typename varchar(32),@length varchar(5)

declare record cursor for

select a.*

from @fieldtb f inner join @attrtb a on f.field=a.field

order by f.pk

open record

fetch next from record into @field,@typename,@length

while @@fetch_status=0

begin

if @typename not in ('varchar','nvarchar','char','nchar','text','ntext')

begin

select @length=case @typename when 'smalldatetime' then 10

when 'datetime' then 32

when 'bit' then 1

else 16

end

if @typename in ('money','smallmoney')

select @moneyStr=@moneyStr+'@'+@field+'__'+@typename+','

select @typename='varchar'

end

select @string=@string+' @'+@field+' '+@typename+'('+@length+')'+','

if len(@string)>@i*128

begin

select @string=@string+char(13)

select @i=@i+1

end

fetch next from record into @field,@typename,@length

end

close record

deallocate record

--About pkeys

declare @pkeytb table(field varchar(32))

insert @pkeytb

select c.name

from syscolumns c inner join sysindexes i on c.id=i.id

where c.id=object_id(@object) and (i.status & 0x800)=0x800

and (c.name = index_col (@object, i.indid, 1) or

c.name = index_col (@object, i.indid, 2) or

c.name = index_col (@object, i.indid, 3) or

c.name = index_col (@object, i.indid, 4) or

c.name = index_col (@object, i.indid, 5) or

c.name = index_col (@object, i.indid, 6) or

c.name = index_col (@object, i.indid, 7) or

c.name = index_col (@object, i.indid, 8) or

c.name = index_col (@object, i.indid, 9) or

c.name = index_col (@object, i.indid, 10) or

c.name = index_col (@object, i.indid, 11) or

c.name = index_col (@object, i.indid, 12) or

c.name = index_col (@object, i.indid, 13) or

c.name = index_col (@object, i.indid, 14) or

c.name = index_col (@object, i.indid, 15) or

c.name = index_col (@object, i.indid, 16)

)

if (select count(*) from @pkeytb)>1

begin

declare curpkeys cursor for

select field from @pkeytb

open curpkeys

fetch next from curpkeys into @field

while @@fetch_status=0

begin

select @pkeyvarStr=@pkeyvarStr+@field+'=@'+@field+' and '

select @pkeyStr=@pkeyStr+@field+','

select @i=charindex('@'+@field,@string)

if @i>0 select @pkeyParaStr=@pkeyParaStr+substring(@string,@i,charindex(',',@string,@i)-@i)+','

fetch next from curpkeys into @field

end

close curpkeys

deallocate curpkeys

select @pkeyvarStr=left(@pkeyvarStr,len(@pkeyvarStr)-3)

select @pkeyStr=left(@pkeyStr,len(@pkeyStr)-1)

select @pkeyParaStr=left(@pkeyParaStr,len(@pkeyParaStr)-1)

end

else if (select count(*) from @pkeytb)=1

begin

select @field=field from @pkeytb

select @pkeyvarStr=@field+'=@'+@field

select @pkeyStr=@field

select @i=charindex('@'+@field,@string)

if @i>0 select @pkeyParaStr=substring(@string,@i,charindex(',',@string,@i)-@i)

end

if right(@string,1)=char(13)

select @string=left(@string,len(@string)-2)

else

select @string=left(@string,len(@string)-1)

select @nameStr=left(@nameStr,len(@nameStr)-1)

select @varStr=left(@varStr,len(@varStr)-1)

select @varStr=replace(@varStr,'@ModDate','getdate()')

select @varStr=replace(@varStr,'@IsValid',space(7)+'1')

if right(@updStr,1)=char(9)

select @updStr=left(@updStr,len(@updStr)-4)

else

select @updStr=left(@updStr,len(@updStr)-1)

select @updStr=replace(@updStr,'@ModDate','getdate()')

select @updStr=replace(@updStr,'@IsValid','IsValid')

----处理@moneyStr

declare @covNameStr varchar(1000)

select @covNameStr=@nameStr

if @moneyStr<>''

BEGIN

declare @itemStr varchar(50),@itemfield varchar(32),@itemtype varchar(20),@covStr varchar(50)

select @moneyStr=','+@moneyStr

select @moneyStr

select @i=1,@itemStr=substring(@moneyStr,@i+1,charindex(',',@moneyStr,@i+1)-@i-1)

while @itemStr<>''

begin

select @itemfield=left(@itemStr,charindex('__',@itemstr)-1)

select @itemtype=right(@itemStr,len(@itemStr)-charindex('__',@itemStr)-1)

select @covStr='cast('+@itemfield+' as '+@itemtype+')'

select @varStr=replace(@varStr,@itemfield,@covStr)

select @updStr=replace(@updStr,@itemfield,@covStr)

select @covNameStr=replace(@covNameStr,right(@itemfield,len(@itemfield)-1),space(len(@covStr)-len(@itemfield))+right(@itemfield,len(@itemfield)-1))

select @i=charindex(',',@moneyStr,@i+1)

if @i=len(@moneyStr)

break

else select @itemStr=substring(@moneyStr,@i+1,charindex(',',@moneyStr,@i+1)-@i-1)

end

END

--------------------

declare @insertStr varchar(2000),@updateStr varchar(2000),@selectStr varchar(500)--,@deleteStr varchar(500) 作为了输出参数

declare @returnPk varchar(250)--,@selectFieldStr varchar(1000) 作为了输出参数

if @autofield is null

select @insertStr=char(9)+'insert '+@object+'('+@covNameStr+')'+char(13)+char(9)+ 'values '+space(len(@object))+'('+@varStr+')'

else

select @insertStr=space(3)+'begin'+char(13)+char(9)+'declare @count int'+char(13)+char(9)+'select @count=count(*) from '+@object+' where substring('+@autofield+',3,4)=convert(varchar(4),getdate(),12)'+char(13)+char(9)+'select @'+@autofield+'=''XX''+convert(varchar(4),getdate(),12)+cast(@count+1 as varchar(16))'+char(13)+char(13)+char(9)+'insert '+@object+'('+@covNameStr+')'+char(13)+char(9)+ 'values '+space(len(@object))+'('+@varStr+')'+char(13)+space(3)+'end'

select @updateStr=char(9)+'update '+@object+char(13)+char(9)+'set '+@updStr+char(13)+char(9)+'where '+@pkeyvarStr

select @deleteStr='delete '+@object+' where '+@pkeyvarStr

select @selectStr='select * from '+@object+' where '+@pkeyvarStr

select @returnPk='select @'+replace(@pkeyStr,',',',@')

if charindex('IsValid',@covNameStr)>0

select @selectFieldStr='IsValid=1 and '

else

select @selectFieldStr=''

select @selectFieldStr='select '+@nameStr+' from '+@object+' where '+@selectFieldStr+@pkeyvarStr

--declare @saveStr varchar(3000) 作为了输出参数

--select @moneyStr=stuff(@moneyStr,len(@moneyStr),1,char(10)+char(13))

select @saveStr='if not exists('+@selectStr+')'+char(13)+@insertStr+char(13)+'else'+char(13)+@updateStr+char(10)+char(13)+@returnPk

if charindex('@IsValid',@string)>0

select @string=replace(@string,', @IsValid varchar(1)','')

if charindex(','+char(13)+' @IsValid',@string)>0

select @string=replace(@string,','+char(13)+' @IsValid varchar(1)','')

if charindex(','+char(13)+' @ModDate',@string)>0

select @string=replace(@string,','+char(13)+' @ModDate varchar(32)','')

if charindex(', @ModDate',@string)>0

select @string=replace(@string,', @ModDate varchar(32)','')

/*

select @string

select @pkeyParaStr

select @saveStr

--select @insertStr

--select @updateStr

select @deleteStr

--select @selectStr

select @selectFieldStr

*/

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有