http://topic.csdn.net/u/20080421/12/e87e234f-4a99-4bbe-9c83-527f8add418b.html
use test
go
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
如果2005,可以用nvarchar(Max)替換nvarchar(4000)
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script
(@s nvarchar(4000),@Flag Bit=1)
as
declare @s2 nvarchar(4000),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(4000))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(4000))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(isnumeric(left(Col,charindex(char(9),Col)-1))),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl='' or left(b.Col,charindex(char(9),b.Col)-1)=';' then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
print 'if not object_id(''Tempdb..'+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
print 'Go'
print 'Select * from '+@Tab
go
--Testing
declare @Flag bit,@s nvarchar(4000)
select @s='#T
Name ID Xtype UID
sysrowsetcolumns 4 S 4
sysrowsets 5 S 4
sysallocunits 7 S 4
sysfiles1 8 S 4
syshobtcolumns ; S
'
,
@Flag=1
exec #SQL_Script @s,@Flag
---生成樣本數據格式:
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
Insert #T
select 'sysrowsetcolumns',4,'S',4 union all
select 'sysrowsets',5,'S',4 union all
select 'sysallocunits',7,'S',4 union all
select 'sysfiles1',8,'S',4 union all
select 'syshobtcolumns',null,'S',null
Go
Select * from #T
61 个解决方案
#2
顶
#3
这段可以改用 declare @tmp_table ([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
--这段可以改用 declare @tmp_table ([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
-- 效率更高一些
Create table #T([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
Insert #T
select 'sysrowsetcolumns',4,'S',4 union all
select 'sysrowsets',5,'S',4 union all
select 'sysallocunits',7,'S',4 union all
select 'sysfiles1',8,'S',4 union all
select 'syshobtcolumns',null,'S',null
Go
Select * from #T
#4
不在深圳,帮顶
#5
楼主好样的。。
#6
无语。
#7
严重关注事态进展.
#8
希望方便大家对提问的数据生成。以前写测试数据,确实有点慢而且麻烦。
#9
增加了样本数据中出现 NULL 的情况
/* 請大家關注,招聘繼續
http://topic.csdn.net/u/20080421/12/e87e234f-4a99-4bbe-9c83-527f8add418b.html
*/
use test
go
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
如果2005,可以用nvarchar(Max)替換nvarchar(4000)
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script
(@s nvarchar(4000),@Flag Bit=1)
as
declare @s2 nvarchar(4000),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(4000))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(4000))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(isnumeric(left(Col,charindex(char(9),Col)-1))),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl in('','null') or left(b.Col,charindex(char(9),b.Col)-1)=';' then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
print 'if not object_id(''Tempdb..'+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
print 'Go'
print 'Select * from '+@Tab
go
--Testing
declare @Flag bit,@s nvarchar(4000)
select @s='#T
Name ID Xtype UID
sysrowsetcolumns 4 S 4
sysrowsets 5 S 4
sysallocunits 7 S 4
sysfiles1 NULL S 4
syshobtcolumns ; S
'
,
@Flag=1
exec #SQL_Script @s,@Flag
#10
c c ,
#11
请大家测试下,看还有哪些功能,应该加上去的。
#12
使用小楼的就方便了..也很强大..
#13
up
看看~~~
看看~~~
#14
看看,顶
#15
这是干吗?
#16
顶
up
up
#17
比较经典,
up
抢分哦
up
抢分哦
#18
对,这个确实用来抢分用。
#19
顶
#20
增加了有效數字的判斷,例如“7,123,012.59”
use test
go
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(4000),@Flag Bit=1)
as
declare @s2 nvarchar(4000),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(4000))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(4000))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(case when left(Col,charindex(char(9),Col)-1) like '%,%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1)) end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl in('','null') or left(b.Col,charindex(char(9),b.Col)-1)=';' then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
print 'if not object_ID(''Tempdb..'+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
print 'Go'
print 'Select * from '+@Tab
go
--Testing
declare @Flag bit,@s nvarchar(4000)
select @s='#T
Name ID Xtype UID
sysrowsetcolumns 4 S 4
sysrowsets 5 S 4
sysallocunits 7,123,012.59 S 4
sysfiles1 null S 4
syshobtcolumns ; S
',@Flag=1
exec #SQL_Script @s,@Flag
----結果:
--> --> (Ben)生成測試數據
if not object_ID('Tempdb..#T') is null
drop table #T
Go
Create table #T([Name] nvarchar(16),[ID] nvarchar(12),[Xtype] nvarchar(1),[UID] int)
Insert #T
select 'sysrowsetcolumns','4','S',4 union all
select 'sysrowsets','5','S',4 union all
select 'sysallocunits','7,123,012.59','S',4 union all
select 'sysfiles1','null','S',4 union all
select 'syshobtcolumns',null,'S',null
Go
Select * from #T
#21
顶..我也准备回广东.
MSN: mstop@live.cn
MSN: mstop@live.cn
#22
厉害
#23
TO: MSTOP
我上班时间都在线,详细,我们可以在msn讨论。你一般什么时候在线 ?
我上班时间都在线,详细,我们可以在msn讨论。你一般什么时候在线 ?
#24
。。。。。。
#25
.
#26
up
#27
保存
#28
20080527 增加了表變量,臨時表,正式表判斷 V2.0
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
%%20080527 增加了表變量,臨時表,正式表判斷
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(Max),@Flag Bit=1)
as
declare @s2 nvarchar(Max),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000),@TabType nvarchar(100)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(Max))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(Max))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(case when charindex(',',left(Col,charindex(char(9),Col)-1))>0 or left(col,2) like '0[^.]%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1))end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','Null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl ='' or left(b.Col,charindex(char(9),b.Col)-1) in(';','null') then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
if left(@Tab,1)='@'
print 'declare '+@Tab+' table('+@ColName+')'
else
begin
if left(@Tab,1)='#'
set @TabType='Tempdb..'
else
set @TabType=''
print 'if not object_id('''+@TabType+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
end
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
if left(@Tab,1)<>'@'
print 'Go'
print 'Select * from '+@Tab
go
#29
jf
#30
表變量:
declare @s nvarchar(max)
set @s='@tb
YGBH YGXM RQ
00001 张三 20080226
00001 张三 20080302
00002 李四 20080228
00002 李四 20080301
00003 王五 20080228
00003 王五 20080301
00003 王五 20080306
'
exec #SQL_Script @s
--> --> (Ben)生成測試數據
declare @tb table([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert @tb
select '00001','?三','20080226' union all
select '00001','?三','20080302' union all
select '00002','李四','20080228' union all
select '00002','李四','20080301' union all
select '00003','王五','20080228' union all
select '00003','王五','20080301' union all
select '00003','王五','20080306'
Select * from @tb
go
#31
臨時表:
declare @s nvarchar(max)
set @s='#tb
YGBH YGXM RQ
00001 张三 20080226
00001 张三 20080302
00002 李四 20080228
00002 李四 20080301
00003 王五 20080228
00003 王五 20080301
00003 王五 20080306
'
exec #SQL_Script @s
--> --> (Ben)生成測試數據
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert #tb
select '00001','?三','20080226' union all
select '00001','?三','20080302' union all
select '00002','李四','20080228' union all
select '00002','李四','20080301' union all
select '00003','王五','20080228' union all
select '00003','王五','20080301' union all
select '00003','王五','20080306'
Go
Select * from #tb
#32
jf可以吗
#33
MARK
#34
增加繁體簡體出現問號的問題
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
%%20080527 增加了表變量,臨時表,正式表判斷
增加繁體簡體出現問號的問題。
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(Max),@Flag Bit=1)
as
declare @s2 nvarchar(Max),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000),@TabType nvarchar(100)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(Max))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select N'+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(Max))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(case when charindex(',',left(Col,charindex(char(9),Col)-1))>0 or left(col,2) like '0[^.]%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1))end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','Null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl ='' or left(b.Col,charindex(char(9),b.Col)-1) in(';','null') then 'null'
when @IsChar=1 then case when @IsDate=0 then 'N' else '' end +quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
if left(@Tab,1)='@'
print 'declare '+@Tab+' table('+@ColName+')'
else
begin
if left(@Tab,1)='#'
set @TabType='Tempdb..'
else
set @TabType=''
print 'if not object_id('''+@TabType+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
end
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
if left(@Tab,1)<>'@'
print 'Go'
print 'Select * from '+@Tab
go
declare @s nvarchar(max)
set @s=N'#tb
YGBH YGXM RQ
00001 张三 20080226
00001 张三 20080302
00002 李四 20080228
00002 李四 20080301
00003 王五 20080228
00003 王五 20080301
00003 王五 20080306
'
exec #SQL_Script @s
#35
--> --> (Ben)生成測試數據
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert #tb
select N'00001',N'张三','20080226' union all
select N'00001',N'张三','20080302' union all
select N'00002',N'李四','20080228' union all
select N'00002',N'李四','20080301' union all
select N'00003',N'王五','20080228' union all
select N'00003',N'王五','20080301' union all
select N'00003',N'王五','20080306'
Go
Select * from #tb
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert #tb
select N'00001',N'张三','20080226' union all
select N'00001',N'张三','20080302' union all
select N'00002',N'李四','20080228' union all
select N'00002',N'李四','20080301' union all
select N'00003',N'王五','20080228' union all
select N'00003',N'王五','20080301' union all
select N'00003',N'王五','20080306'
Go
Select * from #tb
#36
哈,好贴,Mark~
#37
以前回答问题写个测试数据都得半天,有了这个,SQL版的抢分更猛烈了。。。
#38
关注...
#39
看看!
#40
轻轻的问一声:谁能说说这个是用来干什么用的?
#41
用來生成測試數據
#42
高
#43
.....
#44
支持
#45
mark
#46
学习
#47
没懂啥意思
#48
增強日期的判斷
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
%%20080527 增加了表變量,臨時表,正式表判斷
增加繁體簡體出現問號的問題。
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(Max),@Flag Bit=1)
as
declare @s2 nvarchar(Max),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000),@TabType nvarchar(100)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(Max))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
begin
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
while patindex('%['+char(9)+char(32)+']%',@ColName)=1
select @ColName=stuff(@ColName,1,1,'')
end
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select N'+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(Max))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(replace(replace(left(Col,charindex(char(9),Col)-1),'~',char(32)),'`',char(9)))),
@isnumeric=min(case when charindex(',',left(Col,charindex(char(9),Col)-1))>0 or left(col,2) like '0[^.]%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1))end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','Null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl ='' or left(b.Col,charindex(char(9),b.Col)-1) in(';','null') then 'null'
when @IsChar=1 then case when @IsDate=0 then 'N' else '' end +quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
if left(@Tab,1)='@'
print 'declare '+@Tab+' table('+@ColName+')'
else
begin
if left(@Tab,1)='#'
set @TabType='Tempdb..'
else
set @TabType=''
print 'if not object_id('''+@TabType+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
end
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
if left(@Tab,1)<>'@'
print 'Go'
else
print ' '
print 'Select * from '+@Tab
go
declare @s nvarchar(max)
set @s=N'#tb
姓名 考勤时间
小刘 2007/12/1~07:39
小刘 2007/12/1~11:39
小刘 2007/12/1~12:39
小刘 2007/12/1~17:39
小李 2007/12/1~07:39
小李 2007/12/1~11:39
小李 2007/12/1~12:39
'
exec #SQL_Script @s
#49
--> --> (Ben)生成測試數據
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([姓名] nvarchar(2),[考勤时间] Datetime)
Insert #tb
select N'小刘','2007/12/1 07:39' union all
select N'小刘','2007/12/1 11:39' union all
select N'小刘','2007/12/1 12:39' union all
select N'小刘','2007/12/1 17:39' union all
select N'小李','2007/12/1 07:39' union all
select N'小李','2007/12/1 11:39' union all
select N'小李','2007/12/1 12:39'
Go
Select * from #tb
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([姓名] nvarchar(2),[考勤时间] Datetime)
Insert #tb
select N'小刘','2007/12/1 07:39' union all
select N'小刘','2007/12/1 11:39' union all
select N'小刘','2007/12/1 12:39' union all
select N'小刘','2007/12/1 17:39' union all
select N'小李','2007/12/1 07:39' union all
select N'小李','2007/12/1 11:39' union all
select N'小李','2007/12/1 12:39'
Go
Select * from #tb
#50
mark
#1
#2
顶
#3
这段可以改用 declare @tmp_table ([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
--这段可以改用 declare @tmp_table ([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
-- 效率更高一些
Create table #T([Name] nvarchar(16),[ID] int,[Xtype] nvarchar(1),[UID] int)
Insert #T
select 'sysrowsetcolumns',4,'S',4 union all
select 'sysrowsets',5,'S',4 union all
select 'sysallocunits',7,'S',4 union all
select 'sysfiles1',8,'S',4 union all
select 'syshobtcolumns',null,'S',null
Go
Select * from #T
#4
不在深圳,帮顶
#5
楼主好样的。。
#6
无语。
#7
严重关注事态进展.
#8
希望方便大家对提问的数据生成。以前写测试数据,确实有点慢而且麻烦。
#9
增加了样本数据中出现 NULL 的情况
/* 請大家關注,招聘繼續
http://topic.csdn.net/u/20080421/12/e87e234f-4a99-4bbe-9c83-527f8add418b.html
*/
use test
go
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
如果2005,可以用nvarchar(Max)替換nvarchar(4000)
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script
(@s nvarchar(4000),@Flag Bit=1)
as
declare @s2 nvarchar(4000),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(4000))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(4000))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(isnumeric(left(Col,charindex(char(9),Col)-1))),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl in('','null') or left(b.Col,charindex(char(9),b.Col)-1)=';' then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
print 'if not object_id(''Tempdb..'+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
print 'Go'
print 'Select * from '+@Tab
go
--Testing
declare @Flag bit,@s nvarchar(4000)
select @s='#T
Name ID Xtype UID
sysrowsetcolumns 4 S 4
sysrowsets 5 S 4
sysallocunits 7 S 4
sysfiles1 NULL S 4
syshobtcolumns ; S
'
,
@Flag=1
exec #SQL_Script @s,@Flag
#10
c c ,
#11
请大家测试下,看还有哪些功能,应该加上去的。
#12
使用小楼的就方便了..也很强大..
#13
up
看看~~~
看看~~~
#14
看看,顶
#15
这是干吗?
#16
顶
up
up
#17
比较经典,
up
抢分哦
up
抢分哦
#18
对,这个确实用来抢分用。
#19
顶
#20
增加了有效數字的判斷,例如“7,123,012.59”
use test
go
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(4000),@Flag Bit=1)
as
declare @s2 nvarchar(4000),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(4000))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(4000))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(case when left(Col,charindex(char(9),Col)-1) like '%,%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1)) end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl in('','null') or left(b.Col,charindex(char(9),b.Col)-1)=';' then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
print 'if not object_ID(''Tempdb..'+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
print 'Go'
print 'Select * from '+@Tab
go
--Testing
declare @Flag bit,@s nvarchar(4000)
select @s='#T
Name ID Xtype UID
sysrowsetcolumns 4 S 4
sysrowsets 5 S 4
sysallocunits 7,123,012.59 S 4
sysfiles1 null S 4
syshobtcolumns ; S
',@Flag=1
exec #SQL_Script @s,@Flag
----結果:
--> --> (Ben)生成測試數據
if not object_ID('Tempdb..#T') is null
drop table #T
Go
Create table #T([Name] nvarchar(16),[ID] nvarchar(12),[Xtype] nvarchar(1),[UID] int)
Insert #T
select 'sysrowsetcolumns','4','S',4 union all
select 'sysrowsets','5','S',4 union all
select 'sysallocunits','7,123,012.59','S',4 union all
select 'sysfiles1','null','S',4 union all
select 'syshobtcolumns',null,'S',null
Go
Select * from #T
#21
顶..我也准备回广东.
MSN: mstop@live.cn
MSN: mstop@live.cn
#22
厉害
#23
TO: MSTOP
我上班时间都在线,详细,我们可以在msn讨论。你一般什么时候在线 ?
我上班时间都在线,详细,我们可以在msn讨论。你一般什么时候在线 ?
#24
。。。。。。
#25
.
#26
up
#27
保存
#28
20080527 增加了表變量,臨時表,正式表判斷 V2.0
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
%%20080527 增加了表變量,臨時表,正式表判斷
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(Max),@Flag Bit=1)
as
declare @s2 nvarchar(Max),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000),@TabType nvarchar(100)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(Max))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select '+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(Max))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(case when charindex(',',left(Col,charindex(char(9),Col)-1))>0 or left(col,2) like '0[^.]%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1))end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','Null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl ='' or left(b.Col,charindex(char(9),b.Col)-1) in(';','null') then 'null'
when @IsChar=1 then quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
if left(@Tab,1)='@'
print 'declare '+@Tab+' table('+@ColName+')'
else
begin
if left(@Tab,1)='#'
set @TabType='Tempdb..'
else
set @TabType=''
print 'if not object_id('''+@TabType+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
end
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
if left(@Tab,1)<>'@'
print 'Go'
print 'Select * from '+@Tab
go
#29
jf
#30
表變量:
declare @s nvarchar(max)
set @s='@tb
YGBH YGXM RQ
00001 张三 20080226
00001 张三 20080302
00002 李四 20080228
00002 李四 20080301
00003 王五 20080228
00003 王五 20080301
00003 王五 20080306
'
exec #SQL_Script @s
--> --> (Ben)生成測試數據
declare @tb table([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert @tb
select '00001','?三','20080226' union all
select '00001','?三','20080302' union all
select '00002','李四','20080228' union all
select '00002','李四','20080301' union all
select '00003','王五','20080228' union all
select '00003','王五','20080301' union all
select '00003','王五','20080306'
Select * from @tb
go
#31
臨時表:
declare @s nvarchar(max)
set @s='#tb
YGBH YGXM RQ
00001 张三 20080226
00001 张三 20080302
00002 李四 20080228
00002 李四 20080301
00003 王五 20080228
00003 王五 20080301
00003 王五 20080306
'
exec #SQL_Script @s
--> --> (Ben)生成測試數據
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert #tb
select '00001','?三','20080226' union all
select '00001','?三','20080302' union all
select '00002','李四','20080228' union all
select '00002','李四','20080301' union all
select '00003','王五','20080228' union all
select '00003','王五','20080301' union all
select '00003','王五','20080306'
Go
Select * from #tb
#32
jf可以吗
#33
MARK
#34
增加繁體簡體出現問號的問題
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
%%20080527 增加了表變量,臨時表,正式表判斷
增加繁體簡體出現問號的問題。
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(Max),@Flag Bit=1)
as
declare @s2 nvarchar(Max),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000),@TabType nvarchar(100)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(Max))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select N'+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(Max))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(left(Col,charindex(char(9),Col)-1))),
@isnumeric=min(case when charindex(',',left(Col,charindex(char(9),Col)-1))>0 or left(col,2) like '0[^.]%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1))end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','Null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl ='' or left(b.Col,charindex(char(9),b.Col)-1) in(';','null') then 'null'
when @IsChar=1 then case when @IsDate=0 then 'N' else '' end +quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
if left(@Tab,1)='@'
print 'declare '+@Tab+' table('+@ColName+')'
else
begin
if left(@Tab,1)='#'
set @TabType='Tempdb..'
else
set @TabType=''
print 'if not object_id('''+@TabType+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
end
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
if left(@Tab,1)<>'@'
print 'Go'
print 'Select * from '+@Tab
go
declare @s nvarchar(max)
set @s=N'#tb
YGBH YGXM RQ
00001 张三 20080226
00001 张三 20080302
00002 李四 20080228
00002 李四 20080301
00003 王五 20080228
00003 王五 20080301
00003 王五 20080306
'
exec #SQL_Script @s
#35
--> --> (Ben)生成測試數據
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert #tb
select N'00001',N'张三','20080226' union all
select N'00001',N'张三','20080302' union all
select N'00002',N'李四','20080228' union all
select N'00002',N'李四','20080301' union all
select N'00003',N'王五','20080228' union all
select N'00003',N'王五','20080301' union all
select N'00003',N'王五','20080306'
Go
Select * from #tb
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([YGBH] nvarchar(5),[YGXM] nvarchar(2),[RQ] Datetime)
Insert #tb
select N'00001',N'张三','20080226' union all
select N'00001',N'张三','20080302' union all
select N'00002',N'李四','20080228' union all
select N'00002',N'李四','20080301' union all
select N'00003',N'王五','20080228' union all
select N'00003',N'王五','20080301' union all
select N'00003',N'王五','20080306'
Go
Select * from #tb
#36
哈,好贴,Mark~
#37
以前回答问题写个测试数据都得半天,有了这个,SQL版的抢分更猛烈了。。。
#38
关注...
#39
看看!
#40
轻轻的问一声:谁能说说这个是用来干什么用的?
#41
用來生成測試數據
#42
高
#43
.....
#44
支持
#45
mark
#46
学习
#47
没懂啥意思
#48
增強日期的判斷
if object_id('Tempdb..#SQL_Script') is not null
drop proc #SQL_Script
go
/****************************************************************************************************
%%臨時存儲過程名:#SQL_Script
%%參數:@s 分拆符串,@Flag=1 指定列名 @Flag=0 自動生成列名
%%功能:
%%備注@s格式: 空格以"~"替代,制表符以"`"替代,空值時以";"替代;用空格或制表符作為列的分隔,
連續空格、制表符只計算一個,每一行結尾用不用輸入,如果在中間位置列為null時用";"替換
%%20080527 增加了表變量,臨時表,正式表判斷
增加繁體簡體出現問號的問題。
*****************************************************************************************************
%%編寫:Ben 2008-05-19
*****************************************************************************************************/
create proc #SQL_Script(@s nvarchar(Max),@Flag Bit=1)
as
declare @s2 nvarchar(Max),@Tab sysname,@ColName nvarchar(1000),@Print nvarchar(1000),@TabType nvarchar(100)
,@i int,@Col int,@TabID int,@IsDate Bit,@Isnumeric Bit,@Len int,@MaxLen int,@NewID Bit
,@Char Bit,@Type sysname,@IsChar Bit
select @i=charindex(char(13)+char(10),@s),@Tab=rtrim(left(@s,@i-1)),@s=stuff(@s,1,@i+1,''),
@Tab=left(@Tab,len(@Tab)-patindex('%[^'+char(9)+char(32)+']%',reverse(@Tab))+1)--得到表名
if object_id('Tempdb..#') is not null
drop table #
create table #(ID int identity,Col nvarchar(Max))
if right(@s,2)!=char(13)+char(10)
set @s=@s+char(13)+char(10)
--替換中間空格為一個
select @i=patindex('%'+char(32)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(32),char(32)),@i=patindex('%'+char(32)+char(32)+'%',@s)
--替換中間空格+制表符為一個制表符
select @i=patindex('%'+char(32)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(9),char(9)),@i=patindex('%'+char(32)+char(9)+'%',@s)
--替換中間制表符+空格為一個制表符
select @i=patindex('%'+char(9)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(32),char(9)),@i=patindex('%'+char(9)+char(32)+'%',@s)
--去掉每一行開始的空格
select @i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(32),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(32)+'%',@s)
--去掉每一行結束的空格
select @i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(32)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(32)+char(13)+char(10)+'%',@s)
--去掉每一行開始的制表符
select @i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(13)+char(10)+char(9),char(13)+char(10)),@i=patindex('%'+char(13)+char(10)+char(9)+'%',@s)
--去掉每一行結束的制表符
select @i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(13)+char(10),char(13)+char(10)),@i=patindex('%'+char(9)+char(13)+char(10)+'%',@s)
--替換空格為制表符
select @s=replace(@s,char(32),char(9))
--替換中間制表符為1個
select @i=patindex('%'+char(9)+char(9)+'%',@s)
while @i>0
select @s=replace(@s,char(9)+char(9),char(9)),@i=patindex('%'+char(9)+char(9)+'%',@s)
if @Flag=1
begin
select @i=charindex(char(13)+char(10),@s),@ColName=rtrim(left(@s,@i-1))+char(9),@s=stuff(@s,1,@i+1,'')
while patindex('%['+char(9)+char(32)+']%',@ColName)=1
select @ColName=stuff(@ColName,1,1,'')
end
select @i=charindex(char(13)+char(10),@s),@s=@s+char(13)+char(10)
while @i>0 and left(@s,@i-1)>''
begin
select @s2=quotename(left(@s,@i-1),'''') ,@s=stuff(@s,1,charindex(char(13)+char(10),@s)+1,''),@i=charindex(char(13)+char(10),@s)
set nocount on
exec('insert # select N'+@s2)
end
--取列的類型
declare @T table(ID int,Col nvarchar(Max))
set nocount on
insert @T select ID,Col+char(9) from #
update # set Col=null
select @Col=0
if @Flag=0
set @ColName=''
while (select max(len(Col)) from @T)>0
begin
select @IsDate=min(isdate(replace(replace(left(Col,charindex(char(9),Col)-1),'~',char(32)),'`',char(9)))),
@isnumeric=min(case when charindex(',',left(Col,charindex(char(9),Col)-1))>0 or left(col,2) like '0[^.]%' then 0 else isnumeric(left(Col,charindex(char(9),Col)-1))end),
@Len=max(Coalesce(len(right(left(Col,charindex(char(9),Col)-1),charindex('.',reverse(left(Col,charindex(char(9),Col)-1))))),0)),
@MaxLen=max(len(left(Col,charindex(char(9),Col)-1))),
@NewID=min(case when len(left(Col,charindex(char(9),Col)-1))=36 then 1 else 0 end),
@Char=max(case when left(Col,charindex(char(9),Col)-1)='.' then 1 else 0 end),
@Col=@Col+1
from @T
where COl>'' and len(COl)>1
and left(Col,charindex(char(9),Col)-1) not in(';','Null')
if @IsDate=1
select @Type='Datetime',@IsChar=1
else IF @isnumeric=1 and @Char=0
IF @Len>0
select @Type='decimal(18,'+rtrim(@Len-1)+')',@IsChar=0
else
select @Type='int',@IsChar=0
else if @NewID=1
select @Type='uniqueidentifier',@IsChar=1
else
select @Type='nvarchar('+rtrim(@MaxLen)+')',@IsChar=1
update a
set Col=Coalesce(a.Col+',','')+case when b.COl ='' or left(b.Col,charindex(char(9),b.Col)-1) in(';','null') then 'null'
when @IsChar=1 then case when @IsDate=0 then 'N' else '' end +quotename(left(b.Col,charindex(char(9),b.Col)-1),'''')
else left(b.Col,charindex(char(9),b.Col)-1) end
from
# a
join
@T b on a.ID=b.ID
if @Flag=1
set @ColName=stuff(@ColName,charindex(char(9),@ColName),1,']'+char(32)+@Type+',[')
else
set @ColName=@ColName+',[Col'+rtrim(@Col)+'] '+@Type
update @T set Col=stuff(Col,1,charindex(char(9),Col),'') where COl>'' and len(COl)>1
end
if @Flag=1
set @ColName='['+left(@ColName,len(@ColName)-2)
else
set @ColName=stuff(@ColName,1,1,'')
update # set Col=replace(replace(COl,'~',char(32)),'`',char(9))
set @ColName=replace(replace(@ColName,'~',char(32)),'`',char(9))
select @Col=1,@i=max(ID) from #
print replicate(char(45)+char(45)+char(62)+char(32),2)+N'(Ben)生成測試數據'
print ''
if left(@Tab,1)='@'
print 'declare '+@Tab+' table('+@ColName+')'
else
begin
if left(@Tab,1)='#'
set @TabType='Tempdb..'
else
set @TabType=''
print 'if not object_id('''+@TabType+@Tab+''') is null'
print char(9)+'drop table '+@Tab
print 'Go'
print 'Create table '+@Tab+'('+@ColName+')'
end
print 'Insert '+@Tab
while exists(select 1 from #)
begin
select @Print='select '+Col+case when ID=@i then '' else ' union all' end from # where ID=@Col
delete # where ID=@Col
print @Print
set @COl=@COl+1
end
if left(@Tab,1)<>'@'
print 'Go'
else
print ' '
print 'Select * from '+@Tab
go
declare @s nvarchar(max)
set @s=N'#tb
姓名 考勤时间
小刘 2007/12/1~07:39
小刘 2007/12/1~11:39
小刘 2007/12/1~12:39
小刘 2007/12/1~17:39
小李 2007/12/1~07:39
小李 2007/12/1~11:39
小李 2007/12/1~12:39
'
exec #SQL_Script @s
#49
--> --> (Ben)生成測試數據
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([姓名] nvarchar(2),[考勤时间] Datetime)
Insert #tb
select N'小刘','2007/12/1 07:39' union all
select N'小刘','2007/12/1 11:39' union all
select N'小刘','2007/12/1 12:39' union all
select N'小刘','2007/12/1 17:39' union all
select N'小李','2007/12/1 07:39' union all
select N'小李','2007/12/1 11:39' union all
select N'小李','2007/12/1 12:39'
Go
Select * from #tb
if not object_id('Tempdb..#tb') is null
drop table #tb
Go
Create table #tb([姓名] nvarchar(2),[考勤时间] Datetime)
Insert #tb
select N'小刘','2007/12/1 07:39' union all
select N'小刘','2007/12/1 11:39' union all
select N'小刘','2007/12/1 12:39' union all
select N'小刘','2007/12/1 17:39' union all
select N'小李','2007/12/1 07:39' union all
select N'小李','2007/12/1 11:39' union all
select N'小李','2007/12/1 12:39'
Go
Select * from #tb
#50
mark