sql - how to select columns as rows? -


So, I'm searching around and I've got things similar to my problem, but I've helped more Need is a real solution

I am trying to create a query that will return 2 columns of data, the first column should be a list of column names and the second should be the value of that column.

It looks

  Column 1 Column 2 ------- ------- Column A value_of_kalimem column B value_off_columni .... .. .  

I'm pretty sure that it needs to get the dynamic SQL, but I do not know how to start a query.

Any help is appreciated!

This should work for any table, but in my example, I just made one test one You must set the table name within your name @ YourTableNet Additionally, you need to set your line where the results are to be limited in one line, otherwise the output is looking odd with several rows mixed together.

Try this:

  Test your test (Rowwidi int main primary key identification (1,1), call 1 int tap, call 2 varchar (30), call 3 (20), call 4 money, status value four (1), xyz_123 int) your testtable (call1, col2, col3, col4, status value, xyz_123) value (1234, 'wow wow!', 'This is a long examination Enter '', '1234.56,' A ', 98765) into your testtable (col1, col2, col3, col4, editview, Xyz_123) value (543,' Oh no! ',' Short test ', 0,' I ' 12) Finally select the catch and catch * Test your table Set your Tables = 'y.ROWID = 1' SELECT @YourQuery = STUFF ((SELECT '' YourTestTable 'SET @ YourTableTable' SELECT @YourQuery varchar (maximum) SEC @ YourTableName = 'YourTestTable' Information from UNION '+' SELECT '"' + COLUMN_NAME + ', Convert (Behavior (Max),' + COLUMN_NAME + ') FROM' + @ YourTableName + 'y' + ISNULL ('WHERE' + Your TableWhere, '') From SOCEMA.COLUMNS to Table_name = @YourTableName for XML Butt ('')), 1, 7, '') @YourQuery EXEC (@YourQuery)  

Output:

  rovid call 1 call 2 col 3 calls 4 status value xyz_123 ----------- ----------- --------- ----------- ---------- -------------------- --------- ----------- ----------- ----------- 1 1234 Wau Weevil! This is a long test! 1234.56 a 98765 2 543 Oh no! Short test 0.00 I 12 Select 'Test 1', Convert (Max (Max), Call 1, select 'Ro' ', Convert (Max, Rowid) from your Test Tables Y ou.ROWID = 1 Union Select' ) From your Test Tables w = 1 Select the union 'col2', convert (varchar (max), col2) from your TestTable y y.ROWID = 1 select the union 'col3', convert (varchar (max), col3) YourTestTable y to y.RowID = Select 1 union from 'col4', convert (varchar (max), col4) from your testtables y, select y.ROWID = 1 union 'StatSWell', Convert (Max, StatusWell) from your test YU UROOD = 1 union Select 'Xyz_123', convert your test tables from varchar (max), xyz_123) where y.RowID = 1 ----------- --------- -------- ------- col1 1234 col2 wow wee! Col3 This is a long test! Col4 1234.56 Rowidide 1 Status Value Axyz_123 98765  

Edit

For SQL Server 2000 compatibility, you change varchar (max) Should be enabled with Varchar (8000) and use it above the code instead of the SELECT @YourQuery query:

  SELECT @ YourQuery = ISNULL (@ YourQuery + 'UNION', '') + 'SELECT' '' + COLUMN_NAME + ', convert (behave (max),' + COLUMN_NAME + ') FROM' + @ YourTableName + 'y' + ISNULL ('WHERE' + Your TableWhere , '') INSUMMIT_SCHEMA .COLUMNS WHERE Table_Name = @YourTableName  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -