升星——公布自动生成测试数据存储过程——昨夜小楼

Limpire 2008-05-16 03:00:04
加精
create procedure cs
@input varchar(max)
as
set nocount on
if patindex('%[@#$]%',@input)=0 return
select @input=replace(@input,' ',' '),@input=ltrim(rtrim(@input))
select top 94 code=identity(tinyint,33,1),m=cast(null as varchar(2)),w=cast(null as varchar(1)) into # from syscolumns
select @input=replace(@input collate chinese_prc_cs_as_ks_ws,nchar(code+65248),char(code)) from #
truncate table #
insert into # select char(13),char(10)
insert into # select char(9) ,','
insert into # select ' ',','
insert into # select '|' ,','
insert into # select '¦' ,','
insert into # select '。','.'
insert into # select '·','.'
insert into # select char(39),''
select @input=replace(@input collate chinese_prc_cs_as_ks_ws,m,w) from #
drop table #
while patindex('%[^,][;]%',@input)>0 set @input=stuff(@input,patindex('%[^,][;]%',@input)+1,1,',;')
while patindex('%[;][^,]%',@input)>0 set @input=stuff(@input,patindex('%[;][^,]%',@input),1,';,')
set @input=replace(@input,';','null')
while charindex(',,',@input)>0 set @input=replace(@input,',,',',')
set @input=replace(@input,char(10)+',',char(10))
set @input=replace(@input,','+char(10),char(10))
while charindex(char(10)+char(10),@input)>0 set @input=replace(@input,char(10)+char(10),char(10))
if left(@input,1)=char(10) set @input=right(@input,len(@input)-1)
if right(@input,1)<>char(10) set @input=@input+char(10)
declare @tab sysname -- 表名:@=变量表;#=临时表;$=实体表
declare @tid tinyint
declare @yes bit
declare @cid smallint
declare @col varchar(1000)
declare @max smallint
declare @type sysname
declare @lenp smallint
declare @lens smallint
declare @sql varchar(8000)
declare @tabs table (id int identity,name sysname)
declare @data table (id int identity,data varchar(8000))
declare @temp table (id int,temp varchar(1000))
declare @code table (id int,code varchar(8000))
if charindex('$',@input)>0
begin
set @tab=substring(@input,charindex('$',@input)+1,charindex(char(10),@input,charindex('$',@input))-charindex('$',@input)-1)
if object_id(@tab) is not null
begin
raiserror(N'数据库中已存在名为 ''%s'' 的对象。',16,1,@tab)
return
end
end
while patindex('%[@#$]%',@input)>0
begin
select @tab=left(@input,charindex(char(10),@input)-1),@tid=isnull(@tid,0)+1,@yes=1,@cid=1,@input=right(@input,len(@input)-charindex(char(10),@input))
if left(@tab,1)='0' select @tab=stuff(@tab,1,1,''),@yes=0
if len(@tab)=1 set @tab=@tab+'T'+ltrim(@tid)
if left(@tab,1)='$' set @tab=quotename(stuff(@tab,1,1,''))
insert into @tabs values (@tab)
if @yes=0 set @col=null
else select @col=left(@input,charindex(char(10),@input)-1)+',',@input=right(@input,len(@input)-charindex(char(10),@input)),@col=replace(@col,',',':')
while charindex(char(10),@input)>0
begin
insert into @data select left(@input,charindex(char(10),@input)-1)
set @input=right(@input,len(@input)-charindex(char(10),@input))
if left(@input,1) in ('@','#','$') or left(@input,2) in ('0@','0#','0$') break
end
delete from @data where patindex('%[^,-]%',data)=0
select @max=max(len(data)-len(replace(data,',',''))) from @data
update @data set data=data+replicate(',null',@max-len(data)+len(replace(data,',','')))+','
set @max=isnull(len(@col)-len(replace(@col,':','')),0)
insert into @code select id,null from @data order by id
while exists (select 1 from @data where charindex(',',data)>0)
begin
insert into @temp select id,nullif(left(data,charindex(',',data)-1),'null') from @data order by id
update @data set data=right(data,len(data)-charindex(',',data))
if exists (select 1 from @temp a inner join @data b on a.id=b.id and a.temp is not null and left(b.data,4) not in ('','null')) and not exists (select 1 from @temp a inner join @data b on a.id=b.id and a.temp is not null and left(b.data,4) not in ('','null') and isdate(a.temp+space(1)+left(b.data,charindex(',',b.data)-1))=0)
begin
update a set a.temp=a.temp+space(1)+left(b.data,charindex(',',b.data)-1) from @temp a inner join @data b on a.id=b.id where a.temp is not null and left(b.data,charindex(',',b.data)-1)<>'null'
update @data set data=right(data,len(data)-charindex(',',data))
end
if not exists (select 1 from @temp where temp is not null) set @type=' sql_variant'
else if not exists (select 1 from @temp where temp is not null and isnumeric(temp)=0)
begin
if exists (select 1 from @temp where patindex('%[Ee]%',temp)>0) set @type=' float'
else if exists (select 1 from @temp where charindex('.',temp)>0)
begin
select @lenp=max(charindex('.',case when left(temp,1)='-' then right(temp,len(temp)-1) else temp end))-1,@lens=max(charindex('.',reverse(temp)))-1 from @temp where charindex('.',temp)>0
if @lenp+@lens>@@max_precision set @type=' float'
else set @type=' numeric('+ltrim(@lenp+@lens)+','+ltrim(@lens)+')'
end
else if exists (select 1 from @temp where len(temp)>1 and left(temp,1)='0') select @type=' varchar('+ltrim(max(datalength(temp)))+')' from @temp where temp is not null
else if exists (select 1 from @temp where temp is not null and len(temp)<>8 or isdate(temp)=0)
begin
select @lenp=isnull(max(len(temp)),0) from @temp where left(temp,1)<>'-'
select @lens=-isnull(max(len(temp)-1),0) from @temp where left(temp,1)='-'
if @lenp <= abs(@lens) select @lenp=@lenp+@lens,@lens=@lenp-@lens,@lenp=@lenp-@lens
if abs(@lenp)>38 set @type=' varchar('+ltrim(case when @lenp>0 then @lenp else abs(@lenp)+1 end)+')'
else if exists (select 1 from @temp where cast(temp as numeric(38,0)) not between -9223372036854775808 and 9223372036854775807) set @type=' numeric('+ltrim(abs(@lenp))+',0)'
else if exists (select 1 from @temp where cast(temp as numeric(38,0)) not between -2147483648 and 2147483647) set @type=' bigint'
else set @type=' int'
end
else set @type=' datetime'
end
else if not exists (select 1 from @temp where temp is not null and isdate(temp)=0) set @type=' datetime'
else select @type=' varchar('+ltrim(max(datalength(temp)))+')' from @temp where temp is not null
if charindex(':',@col)>0 set @col=stuff(@col,charindex(':',@col),1,@type+case when @cid=@max then '' else ',' end)
else set @col=isnull(@col+',','')+'c'+ltrim(@cid)+@type
update a set a.code=isnull(a.code+',','select ')+case when substring(@type,2,3) in ('flo','num','big','int') then isnull(b.temp,'null') else isnull(quotename(b.temp,char(39)),'null') end from @code a inner join @temp b on a.id=b.id
delete from @temp
set @cid=@cid+1
end
if @max>@cid-1
begin
select @col=stuff(@col,len(@col),1,' sql_variant'),@col=replace(@col,':',' sql_variant,')
update @code set code=code+replicate(',null',@max-@cid+1)
end
set @sql=null
select @sql=isnull(@sql+' union all'+char(13)+char(10),'')+code from @code order by id
print '--> 测试数据: '+@tab
print case left(@tab,1) when '@' then 'declare '+@tab+' table' when '#' then 'if object_id(''tempdb.dbo.'+@tab+''') is not null drop table '+@tab+char(13)+char(10)+'create table '+@tab else 'if object_id('''+@tab+''') is not null drop table '+@tab+char(13)+char(10)+'create table '+@tab end+' ('+@col+')'
print 'insert into '+@tab
print @sql
delete from @data
delete from @code
end
set @sql=null
select @sql=isnull(@sql+char(13)+char(10),char(13)+char(10))+'select * from '+name from @tabs order by id
print @sql
set nocount off
go
...全文
1622 151 打赏 收藏 转发到动态 举报
写回复
用AI写文章
151 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbgn 2010-09-06
  • 打赏
  • 举报
回复
哇靠,还有这东东,开眼界了,我说我怎么老是抢不到呢。
破折号 2010-01-09
  • 打赏
  • 举报
回复
upup~!
ycagri 2009-10-28
  • 打赏
  • 举报
回复
很好,很强大
ycagri 2009-10-28
  • 打赏
  • 举报
回复
很好,很强大
Fallen934985 2009-08-25
  • 打赏
  • 举报
回复
强人啊!!!
fuxiaoyang13 2009-08-21
  • 打赏
  • 举报
回复
学习!!!!!
csdyyr 2009-08-21
  • 打赏
  • 举报
回复
收藏!
振乾 2009-08-21
  • 打赏
  • 举报
回复
大哥 我还是不太清楚 这是干嘛用的
王向飞 2009-07-24
  • 打赏
  • 举报
回复
太厉害了 顶
dsk22657 2008-07-02
  • 打赏
  • 举报
回复
mark
kye_jufei 2008-06-30
  • 打赏
  • 举报
回复
嚴重支持
Limpire 2008-06-29
  • 打赏
  • 举报
回复
-- Limpire(昨夜小楼)

select * from sys.objects
--
2008-06-29 21:24:48.327
Limpire 2008-06-18
  • 打赏
  • 举报
回复

/* * * * * * * * * * * * * * * * * * * * * * * *
* 类别:普通行转列针对性代码 *
* 提供:Limpire(昨夜小楼) *
* 时间:2008-06-18 08:26:55.830 *
* * * * * * * * * * * * * * * * * * * * * * * */

--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (name varchar(8),item varchar(8),qty int)
insert into #T
select 'Jack','a',1 union all
select 'Jack','b',2 union all
select 'Jack','c',3 union all
select 'Paul','a',10 union all
select 'Paul','b',20 union all
select 'Paul','c',30

--> SQL 2000 静态:
select name,
[a]=max(case item when 'a' then qty else '' end),
[b]=max(case item when 'b' then qty else '' end),
[c]=max(case item when 'c' then qty else '' end)
from #T group by name
/*
name a b c
-------- -------- -------- --------
Jack 1 2 3
Paul 10 20 30
*/

--> SQL 2000 动态:
declare @cols varchar(8000)
select @cols=isnull(@cols+',', '')+'['+item+']=max(case item when '''+item+''' then qty else '''' end)' from #T group by item
exec ('select name, '+@cols+' from #T group by name')

--> SQL 2005 静态:
select * from
(select name,item,qty from #T) as a
pivot
(max(qty) for item in ([a],[b],[c])) as b

--> SQL 2005 动态:
declare @dynamic varchar(8000)
select @dynamic=isnull(@dynamic+',', '')+'['+item+']' from #T group by item
exec
('
select * from
(select name,item,qty from #T) as a
pivot
(max(qty) for item in ('+@dynamic+')) as b
')

changweishao 2008-06-18
  • 打赏
  • 举报
回复
好长,好强大,强烈支持
Limpire 2008-06-17
  • 打赏
  • 举报
回复
/* * * * * * * * * * * * * * * * * * * * * * * *
* 类别:分组提取 TOP (n) 条记录针对性总结代码 *
* 提供:Limpire(昨夜小楼) *
* 时间:2008-06-17 20:36:21.907 *
* * * * * * * * * * * * * * * * * * * * * * * */
Limpire 2008-06-17
  • 打赏
  • 举报
回复
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * 类别:分组提取 TOP (n) 条记录针对性总结代码 * *
* * 提供:Limpire(昨夜小楼) * *
* * 时间:2008-06-17 20:26:47.233 * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * */
Limpire 2008-06-17
  • 打赏
  • 举报
回复
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* ┌─────────────────────────────────────────────┐ *
* │ 时间:2008-06-17 20:17:07.090 │ *
* └─────────────────────────────────────────────┘ *
* * * * * * * * * * * * * * * * * * * * * * * * * */
fuda_1985 2008-05-30
  • 打赏
  • 举报
回复
真强!
哎,路漫漫其修远兮,吾将上下而求索啊。
看来要走的路挺长的!
kobe8tn 2008-05-27
  • 打赏
  • 举报
回复
好东西 很长很强大
songfuqiang 2008-05-22
  • 打赏
  • 举报
回复
收藏!
加载更多回复(131)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧