Is there a way to pivot a Sybase Attribute-Value table, into an Attribute-per-column result set. Can this be done?
Source table:
| attribute | value |
~~~~~~~~~~~~~~~~~~~~~
| a1 | A1 |
| b2 | BB2 |
| random | X |
For simplicity, assume varchar(255)
on both attribute
and value
columns, unique index on attribute
; and the attribute names are 100% guaranteed to be strings which are valid Sybase column names.
Desired select result:
| a1 | b2 | random |
~~~~~~~~~~~~~~~~~~~~~~~~
| A1 | BB2 | X |
Is this possible to do via SQL in Sybase ASE? (12 or 15)?
NOTES:
Just to be clear: we do NOT know beforehand what the set of attributes is!!!
There is no need to persist the result set beyond a #temp pound table.
The limitation is that there is no Java enabled.
It seems to me that this is impossible to do, since Sybase 15 ASE docs indicate that column names in ALTER TABLE
must not contain dynamic data:
computed_column_expression
is any valid T-SQL expression that does not contain columns from other tables, local variables, aggregate functions, or subqueries. It can be one or a combination of column name, constant, function, global variable, or case expression, connected by one or more operators. You cannot cross-reference between computed columns except when virtual computed columns reference materialize computed columns.
Yes. I've done that using EXEC
SELECT @my_stmt = "ALTER TABLE #result ADD " + @param + " varchar(255) NULL"
EXEC(@my_stmt)