Select @S='Select DDMC,ZT,LJSJ,CS'+@S+@S1+@S2+@S3+@S4+@S5+@S6+@S7+' From #T Group By SENSORID,DDMC,ZT,LJSJ,CS Order By SENSORID'
EXEC(@S)
这样写不如就用一个变量,改成exec('Select DDMC,ZT,LJSJ,CS'+@S+@S1+@S2+@S3+@S4+@S5+@S6+@S7+' From #T Group By SENSORID,DDMC,ZT,LJSJ,CS Order By SENSORID')
EXEC(@S)
这样写不如就用一个变量,改成exec('Select DDMC,ZT,LJSJ,CS'+@S+@S1+@S2+@S3+@S4+@S5+@S6+@S7+' From #T Group By SENSORID,DDMC,ZT,LJSJ,CS Order By SENSORID')
Select
ID=(Select Count(*) From TB_KGLBJCX2 Where kssk<=A.kssk And SENSORID=A.SENSORID),*
Into #T
From TB_KGLBJCX2 ADeclare @S Nvarchar(4000)
Declare @S1 Nvarchar(4000)
Declare @S2 Nvarchar(4000)
Declare @S3 Nvarchar(4000)
Declare @S4 Nvarchar(4000)
Declare @S5 Nvarchar(4000)
Declare @S6 Nvarchar(4000)
Declare @S7 Nvarchar(4000)Declare @c int
set @c=0
set @s=''
set @s1=''
set @s2=''
set @s3=''
set @s4=''
set @s5=''
set @s6=''
set @s7=''Declare @I Int,@J Int
Set @S=''
Set @J=1
Select @I=Max(ID) From #T
While @J<=@I
Begin
if @c=0
begin
Select @S=@S+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
if len(@s)>=3000
set @c=@c+1
end
else --好像上次写有else的,写不写不同的,楼主慢慢想想
if @c=1
begin
Select @S1=@S1+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
if len(@s1)>=3000
set @c=@c+1
end
else
if @c=2
begin
Select @S2=@S2+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
if len(@s2)>=3000
set @c=@c+1
end
else
if @c=3
begin
Select @S3=@S3+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
end
else
if @c=4
begin
Select @S4=@S4+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
if len(@s4)>=3000
set @c=@c+1
end
else
if @c=5
begin
Select @S5=@S5+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
if len(@s5)>=3000
set @c=@c+1
end
else
if @c=6
begin
Select @S6=@S6+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
if len(@s6)>=3000
set @c=@c+1
end
else
if @c=7
begin
Select @S7=@S7+',Max(Case ID When '+Rtrim(@J) +N' Then DNCXX Else '''' End) As 第'+Rtrim(@J)
+N'次最大值'
end Set @J=@J+1
End
exec('Select DDMC,ZT,LJSJ,CS'+@S+@S1+@S2+@S3+@S4+@S5+@S6+@S7+' From #T Group By SENSORID,DDMC,ZT,LJSJ,CS Order By SENSORID')
GO