sql - Dynamically Join Enum Table with other Table -


I have the following table. These are not real tables but the concept is here.

  Table 1 --------------------------- ------- FieldID | Enumerated Values ​​| TextView ---------------------------------- Col1 | 1 | O ---------------------------------- Col1 | 2 | Test ---------- ------------------------ tomorrow 1 | 3 | George --------------------- ------------- call 2 1 | Random -------------------------------- - Call 2 | 2 | Wesley ---------------------------------- Col3 | 1 | Tompson ---- ------------------------------ Col3 | 2 | Ouo ----------------------------------- Table 2 ------------- ----------------- ---- Call 1 | Call 2 Call 3 ---------------------------------- 1. 2 | 1 - -------------------------------- 2 | 1 | 1 ------------- --------------------- 3 | 1 | 2 ------------------------ ----------   

desired result a view

  ------------ ---------------------- Col1 | Col2 | Col3 ----------------------- ----------- Hey | Wesley | Tompson ---------------------------------- Test | Random | Tompson ------------- --------------------- George | Random | Oo ------------------------ ----------   

So you write something Select

  Select col1.TextValue, col2.TextValue, col3.TextValue FROM Table 2 T2, (Select 1 Table 1 Z FieldID = 'Call 1') Call 1, (Select 1 1 TableIWii fieldID = 'call 2' 2) (Select 1 to 1 tablefield fieldID = 'coal3') Cole 3 Where T2COL 1 = call 1 .EnumeratedValue and t2.col2 = col2.EnumeratedValue and t2.col3 = col3 The index value is   

The problem is that in our real tables there are ~ 20 columns per table. Code I want to find a simple means of writing. I want to connect to Hard Code 20 for each scene which I am doing. I know if there are any options.

With any convertible interactive solution, a dynamic pivot is actually a better way to see it It is not in the form of hard-coded, non-pivot solution, but it includes at least development hours, and table-value functions (which can be given the name of the table and defining in the view Can be optimized for the column name axis First, demo data setup:

  If object_id ('dbo.test_enum') is not a null drop table dbo.test_enum create table dbo. test_enum (FieldID sysname, EnumeratedValue int null, TextValue sysname, Primary key (Field ID, Enumerated Value)) Enter the test_enum (Field ID, Enumerated Value, TextValue) values ​​('Col1', 1, 'Hey'), ('Col1 ',' 2 ',' Test ',' (Col1 ', 3,' George '), (' Col2 ', 1,' Random '), (' Col2 ', 2,' Wesley '), (' Col3 ' 1, 'Thompson'), ('Cole 3', 2, 'OUO'); If object_id ('dbo.test_table') is not a blank drop table dbo.test_table table test_table (id int primary key identification (1,1), col1 int zero, col2 int zero, col3 int is not null); Enter the DBTest_table (call1, call2, call3) values ​​(1,2,1), (2,1,1), (3,1,2)   

next Dynamic Selection Part:

  declare @cols nvarchar = (max) = (select content (select sysobjects from '', '+ quotename (c.name)' '' internal partner sys O.names = 'test_table' and c.name ('Col1', 'Col2', 'Col3'), 1,1, for 'cobject_id = o.object_id where xml path (' ')) on .columns c. ')) Announce @SQL NewWorker (maximum) = N' Select P.ID, '+ @ cols +' (unp.id, unp.fieldID, e.TextValue dbo.test_table unpivot (enumerated for field ID Wells ( '+ @ cols +')) Unp Internal contains e.EnumeratedValue = unp.EnumeratedValues ​​and e.FieldID = unp.FieldID) z axis (minimum (z.TextValue) for z.FieldID IN (on dbo.test_enum e) '+ + cols +')) P 'exec sp_executesql @ sql   

By itself, this will return the desired result set in question, but the definitions of viewing any table / column data set Can be customized to back, and then more customized per table / view.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -