If Object_Id('tempdb.dbo.#test') Is Not Null
Begin
Drop Table #test;
End
If Object_Id('tempdb.dbo.#finalResult') Is Not Null
Begin
Drop Table #finalResult;
End
If Object_Id('tempdb.dbo.#test') Is Null
Begin
Create Table #test
(
RowId Int Identity(1,1) Primary Key
,Id Int
,Phone Varchar(100)
,Address Varchar(2000)
,PhoneUId Int Default 0
)
End
If Object_Id('tempdb.dbo.#finalResult') Is Null
Begin
Create Table #finalResult
(
Id Int Primary Key
)
End
Declare @MaxPhoneCount Int
,@PhoneLoop Int
,@SqlToRun Varchar(5000)
Select @SqlToRun = ''
Insert Into #test (Id,Phone,Address) Values
(1,'502','100 Main')
,(1,'602','100 Main')
,(2,'502','500 S Main')
,(2,'622','500 S Main')
,(2,'782','500 S Main')
,(3,'444','201 N Point')
,(3,'777','201 N Point')
,(4,'111','999 South');
Update t
Set t.PhoneUId = t1.PhoneUid
From #test As t
Join
(
Select t.RowId
,ROW_NUMBER() Over(Partition By t.Id Order By t.Id) As PhoneUid
From #test As t With (Nolock)
) As t1 On t.RowId = t1.RowId
Select @MaxPhoneCount = Max(tr.PhoneCount)
,@PhoneLoop = 1
From (
Select t.Id
,Count(t.Phone) As PhoneCount
From #test As t With (Nolock)
Group By t.Id
) As tr
Select @SqlToRun = 'Alter Table #finalResult Add '
While (@PhoneLoop <= @MaxPhoneCount)
Begin
Select @SqlToRun = @SqlToRun + (Case When @PhoneLoop > 1 Then ',' Else '' End) + ' Phone' + Cast(@PhoneLoop As Varchar(100)) + ' Varchar(100)'
Select @PhoneLoop = @PhoneLoop + 1
End
Select @SqlToRun = @SqlToRun + ',' + ' Address Varchar(2000)'
----Print (@SqlToRun)
Exec (@SqlToRun)
Insert Into #finalResult(Id,Address)
Select Distinct
t.Id
,t.Address
From #test As t With (Nolock)
Select @SqlToRun = ''
,@PhoneLoop = 1
While (@PhoneLoop <= @MaxPhoneCount)
Begin
Select @SqlToRun = 'Update fr Set fr.Phone' + Cast(@PhoneLoop As Varchar(20)) + ' = t.Phone From #finalResult As fr With (Nolock) ' +
'Join #test As t With (Nolock) On fr.Id = t.Id Where t.PhoneUId =' + Cast(@PhoneLoop As Varchar(20)) + ';'
----Print (@SqlToRun)
Exec (@SqlToRun)
Select @PhoneLoop = @PhoneLoop + 1
,@SqlToRun = ''
End
Select *
From #finalResult As fr With (Nolock)
上面的代碼將用於N個電話的運行,但是性能可能下降時數據超過1,00,000記錄。 –