日期:2014-05-18 浏览次数:20472 次
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 @