| Author |
Topic |
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2012-08-10 : 22:56:41
|
| Using the two tables below, I need to link the Information_Schema.column to any demographics field without specifically naming them. Are there wild card like Demographics.% that I can use to link the two together?The bigger picture is I am trying to link them to another table with the codes and descriptions to be able to update the codes for each ID.Information_schemaTable_Name Column_NameDemographics military_codesDemographics TEST_SCORESDemographics tableID Birthdate military_codes TEST_SCORES101 10/15/1987 03, 04, 05 A, b A CThanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-10 : 23:48:07
|
| so what should be output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2012-08-10 : 23:54:50
|
| i will be using another table that will update the codes to descriptions. I need to be able to link any of these tables to the field name. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-10 : 23:58:21
|
| sorry i didnt get that...whats the relevance of information_schema table in this?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2012-08-11 : 00:08:27
|
| I do not have to use that table, but as an example all of the tables I need layout the 'military_codes' with all of the information ready to use, however there is no way to link back to the actual data field that has the record information in it.I need to be able to get the demographic's table to be able to link up any of the column names back to another table without specifying the exact name every time. There are several table that will show all of the fields such as the schema. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-11 : 00:17:21
|
| sounds like unpivot to me------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2012-08-14 : 13:22:06
|
| I have been reviewing your suggestion, but that seems tio be more for aggregates.I cam see how it reverses columns with rows, but cannot find any good examples.See below:My data -ID First Last SpecialField101 Joe Smith Golf102 Suzie Snowflake TennisData return needed - ID 101 102First Joe SuzieLast Smith SnowflakeSpecial Field Golf TennisThanks! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-14 : 13:36:55
|
you cant confirm unless you try it.see illustration belowdeclare @t table(ID int, [First] varchar(100), [Last] varchar(100), SpecialField varchar(100) ) insert @t values(101,'Joe','Smith','Golf'),(102, 'Suzie' ,'Snowflake', 'Tennis')select *from @tunpivot(columnvalue for columnname IN ([First],[Last],[SpecialField]) )upivot(max(columnvalue) for ID IN ([101],[102]))pcolumnname 101 102--------------------------------First Joe SuzieLast Smith SnowflakeSpecialField Golf Tennis ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2012-08-14 : 14:14:47
|
| It comes back with errors:Msg 170, Level 15, State 1, Line 9Line 9: Incorrect syntax near ','.Msg 170, Level 15, State 1, Line 14Line 14: Incorrect syntax near 'columnvalue'Also this may be fine for when inserting the datavalues, but I want to run it against a table without inserting all of the values. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-14 : 14:53:06
|
that insert was just for illustration. you can run select part alone for your table.the insert code part will error if you try running on version before 2008select *from @tunpivot(columnvalue for columnname IN ([First],[Last],[SpecialField]) )upivot(max(columnvalue) for ID IN ([101],[102]))p put your table name and column names in place of @t and columns------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|