I want to run an openquery statement and inside it i want 130 column names generated starting from V001 to V139. These columns already exist in table, i only want number to be generated from 1 to 139. And i want to pivot them together in a single row by time.
Deliverable should be Time,[[login to view URL]],[[login to view URL]], ..[[login to view URL]],...,[[login to view URL]]
I have following code which only generate 1 to 9 columns and not in one row.
I want this in 1 day...
DECLARE @TSQL varchar(8000), @VAR char(5),@wwVersion char(6) ,@Counter int,@Counterv1 int
SELECT @VAR = 'delta'
SELECT @wwVersion = 'Latest'
SELECT @Counter = 0
SELECT @Counterv1 = 10
while @counter < 9
Begin
set @counter = @counter + 1
-- SELECT @Counterstr = 'V00' + rtrim(cast(@counter as char))
print @counter
SELECT @TSQL = 'SELECT * FROM OPENQUERY(INSQL,''SELECT IF ' + rtrim(cast(@counter as char)) + ' <= 10 THEN [CPW_V00' + rtrim(cast(@counter as char)) +'.WTOperationState]
ELSE [CPW_V0' + rtrim(cast(@counter as char)) +'.WTOperationState]
FROM WideHistory WHERE wwRetrievalMode = ''''' + @VAR + '''''
AND wwVersion = ''''' + @wwVersion + '''''
''
)
'
EXEC (@TSQL)
End