发布时间:2010-07-01 14:46 来源:未知
以下的文章主要是对SQL Server临时表的创建的实际操作步骤,以及在实际操作中大家要用到的实际应用代码的介绍,我在一个信誉度很好的网站找到一个关于其相关内容今天拿出来供大家分享。
Create Table #Test(a int)
假如传来的SELECT语句不是以'select'开头,自动修改
If Left(Lower(Ltrim(@Select_Command)),6) <> 'select' Select @Select_Command = 'Select ' + @Select_Command
将开头‘SELECT’去掉
Select @Select_Command_Temp = Lower(Ltrim(@Select_Command)) If Left(@Select_Command_Temp,6) = 'select' Select @Select_Command_Temp = Right(@Select_Command_Temp,Len(@Select_Command_Temp) - 7)
取各保留字位置,以便获得表的列表
Select @From_Pos = CHARINDEX(' from ',@Select_Command_Temp) Select @Where_Pos = CHARINDEX(' where ',@Select_Command_Temp) Select @Having_Pos = CHARINDEX(' having ',@Select_Command_Temp) Select @Groupby_Pos = CHARINDEX(' groupby ',@Select_Command_Temp) Select @Orderby_Pos = CHARINDEX(' orderby ',@Select_Command_Temp) If @Where_Pos > 0 Select @Temp_Pos = @Where_Pos If @Having_Pos > 0 And @Having_Pos < @Temp_Pos Select @Temp_Pos = @Having_Pos If @Groupby_Pos > 0 And @Groupby_Pos < @Temp_Pos Select @Temp_Pos = @Groupby_Pos If @Orderby_Pos > 0 And @Orderby_Pos < @Temp_Pos Select @Temp_Pos = @Orderby_Pos
SQL Server临时表的创建中取表列表
If @Temp_Pos > 0 Begin Select @Table_List = SUBSTRING(@Select_Command_Temp,@From_Pos + 6 ,@Temp_Pos - @From_Pos - 1) End Else Begin Select @Table_List = SUBSTRING(@Select_Command_Temp,@From_Pos + 6 ,Len(@Select_Command_Temp) - @From_Pos - 1) End Select @Column_Syntax = ''
只列出栏位
Select @Select_Command_Temp = Left(@Select_Command_Temp,@From_Pos - 1) While Len(@Select_Command_Temp) > 0 Begin
取逗号位置
Select @Temp_Pos = CHARINDEX(',',@Select_Command_Temp)
初次取栏位名称
If @Temp_Pos > 0 Begin Select @Column_Name = Left(@Select_Command_Temp,@Temp_Pos - 1) End Else Begin Select @Column_Name = @Select_Command_Temp End
取表名和栏位名(可能是‘*’)
If CHARINDEX('.',@Column_Name) > 0 Begin Select @Table_Name = Left(@Column_Name,CHARINDEX('.',@Column_Name) - 1) Select @Column_Name = Right(@Column_Name,Len(@Column_Name) - CHARINDEX('.',@Column_Name)) End Else Begin Select @Table_Name = @Table_List End
栏位出现'*'
If CHARINDEX('*',@Column_Name) > 0 Begin Select @Column_Name = '' Select @Loop_Seq = 1
SQL Server临时表的创建中大家要取栏位个数
Select @Column_Count = Count(*) From SysColumns Where Id = Object_Id(@Table_name) While @Loop_Seq <= @Column_Count Begin
取栏位名称,栏位类型,长度,精度,小数位
Select @Column_Name_Temp = SysColumns.Name, @Column_Type_Temp = Lower(SysTypes.Name), @Column_Length_Temp = SysColumns.Length, @Column_Xprec_Temp = SysColumns.Xprec, @Column_Xscale_Temp = SysColumns.Xscale From SysColumns,SysTypes Where SysColumns.Id = Object_Id(@Table_name) And SysColumns.Colid = @Loop_Seq And SysColumns.XuserType = SysTypes.XuserType
形成栏位语法表达式
Select @Column_Syntax_Temp = Case When @Column_Type_Temp In ('datetime','image','int') Then @Column_Name_Temp + ' ' + @Column_Type_Temp
以上的相关内容就是对SQL Server临时表的创建的介绍,望你能有所收获。