Declare @Sql Varchar(500)
Create Table #t(
ID Varchar(20)
)
Set @Sql = ''
Select @Sql = @Sql + ' Alter Table #t Add Class_' + Cast(Item As Varchar(20)) + ' Varchar(20) Default ''0'''
From YbInterface.dbo.B1
Group By Item
Print @Sql
--Exec (@sql)
Set @Sql = @Sql + ' Insert Into #t(ID) Select ID from YbInterface.dbo.B1 group by ID'
Print @Sql
Exec (@Sql)
Set @Sql = '' ---这步很重要
Select @Sql = @Sql + ' Update A Set Class_' + Item + ' = B.Amount From #t A Inner Join YbInterface.dbo.B1 B On A.Id = B.ID And B.Item = ''' + Item + ''''
From YbInterface.dbo.B1 A
Group By Item
Print @Sql
Exec (@Sql)
select * from #t
drop table #t