SQL SERVER中完全可以.下面是我的一段现实中使用的存储过程:
/*建立临时表用于工资调整的过程*/
/*该临时表应包括员工要素及其各项工资的发放金额*/
CREATE PROCEDURE Sp_Create_Temp_Table
@tableName Char(10),
@operation integer, /*1--创建 2---删除 3--装入数据*/
@isall bit, -------------如果全部人员=1
@bmbh integer
AS
Declare @tt varchar(7777)
if @Operation=2
Begin
Select @tt=N' drop table '+ @tableName
Exec(@tt)
End
if @operation=1
Begin
Select @tt=N' create table '+@tableName+' ( 选取 bit,员工编号 char(10),员工姓名 char(10),部门编号 char(10),部门名称 char(10),'
/*创建游标来进行表的建立*/
Declare @hmmc char(20)
Declare je_CurSor Cursor For
Select hmmc from tu_gzhm where inserting>0 order by hmbh
Open je_CurSor
FETCH NEXT FROM je_Cursor into @hmmc
WHILE @@FETCH_STATUS = 0
BEGIN
select @tt=@tt +@hmmc +N' money,'
FETCH NEXT FROM je_CurSor into @hmmc
END
CLOSE je_Cursor
DEALLOCATE je_Cursor
select @tt=Substring(@tt,1,len(@tt)-1)
select @tt=@tt+N')'
Exec(@tt)
End
IF @operation=3 --------------装入数据
Begin
/*检查是否有上次发薪数据*/
/*没有就以基准工资排列*/
select @tt=N' insert into '+@tableName
select @tt=@tt+N' select 0,y.ygbh,y.ygmc,b.bmbh,b.bmmc, '
Declare @hm char(20),@hmje money
Declare je_CurSor Cursor For
Select hmmc,jcgz from tu_gzhm where inserting>0 order by hmbh
Open je_CurSor
FETCH NEXT FROM je_Cursor into @hm,@hmje
WHILE @@FETCH_STATUS = 0
BEGIN
Select @tt=@tt+N' cast('+rtrim(ltrim(cast(@hmje as Char(20))))+' as Money),'
FETCH NEXT FROM je_CurSor into @hm,@hmje
END
CLOSE je_Cursor
DEALLOCATE je_Cursor
select @tt=Substring(@tt,1,len(@tt)-1)
if @isall=1
select @tt=@tt+N' From
tu_empl y left join tu_bmbh b on y.bmbh=b.bmbh and y.inserting>0 and b.inserting>0'
else
select @tt=@tt+N' From
tu_empl y left join tu_bmbh b on y.bmbh=b.bmbh and y.bmbh='+@bmbh+' and y.inserting>0 and b.inserting>0'
Exec(@tt)
Select @tt=N' select * from '+@tableName
Exec(@tt)
End
GO