用游标自动建立表

ALTER PROCEDURE test AS SET NOCOUNT ON if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tmp] declare @uname nvarchar(100) select distinct 纳税人税务机关名称 into tmp from test1 declare cur1 cursor for select 纳税人税务机关名称 from tmp open cur1 fetch next from cur1 into @uname while @@fetch_status=0 begin if exists (select * from dbo.sysobjects where id = object_id(@uname) and OBJECTPROPERTY(id, N'IsUserTable') = 1) begin exec('drop table ['+@uname+']') end exec ('select * into ['+@uname+'] from test1 where 纳税人税务机关名称='''+@uname+'''' ) fetch next from cur1 into @uname end close cur1 deallocate cur1 GO

你可能感兴趣的:(object,table,Go)