| Author |
Topic |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-20 : 01:49:14
|
| Hi everyone,I have one table having column names 'column' and 'values'in that table in 'column' column has Column names of another table named mstCompanyEstablisment and standard value for that column.Now i need to check whether row value of that table mstCompanyEstablishment is matching with the standard values of the first table or not if not i need true or false accordingly.If its not sufficient to understand problem then please ask the question.Vabhav T |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-20 : 01:54:46
|
| why is your system designed this way? this would involve dynamic sql to search within each of columns of table mstCompanyEstablishment for value stored as per the other table.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-20 : 02:22:50
|
| See in the table mstCompanyEstablishment only one row will be there for one id and we are going to fetch only one row by one idand we have to match that column values with the values of the first standard table for corresponding column name which is stored in table in row formatfor exin first standard table data is something like thatColumnName ValuePFPercent 12CPFPercent 8.33PFCeilling 6500in the table mstcompanyestablishment data is something like thatID PFPercent CPFPercent PFCeiling1 12 8.33 60002 12.5 8.33 6500Now we have to check data in mstEstablishment that for each id data is correct with standart or not say for id one PFCeiling is incorrect as its value in standard table is 6500Vabhav T |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 02:38:09
|
| We have lookup table like that - where we just want a "friendly description" for Status codes and the like. (We could use a separate table for each column's lookups, but that would bloat our table-count hugely, and such status-code lookup only have 10-or-so distinct values. Not ideal for FKeys though! |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 02:41:16
|
EDIT: Corrected to swap the tables aroundI think this will generate the SQL to make the test:SELECT 'SELECT ID 'UNION ALLSELECT DISTINCT ', [T_' + ColumnName + '] = CASE WHEN [' + ColumnName + '] IS NULL THEN NULL WHEN ' + ColumnName + '_VAL.ColumnName IS NULL THEN ''False'' ELSE ''True'' END'FROM dbo.StandardTableUNION ALLSELECT 'FROM mstcompanyestablishment'UNION ALLSELECT DISTINCT 'LEFT OUTER JOIN dbo.StandardTable AS ' + ColumnName + '_VAL ON ' + ColumnName + '_VAL.ColumnName = ''' + ColumnName + '''' + ' AND ' + ColumnName + '_VAL.Value = ' + ColumnNameFROM dbo.mstcompanyXXXestablishment |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-20 : 02:57:41
|
sql 2005 approachreturns id , field info and value that deferred from standardDECLARE @Lookup table(ColumnName varchar(100),Value Numeric(10,2))INSERT INTO @LookupSELECT 'PFPercent', 12 UNION ALLSELECT 'CPFPercent', 8.33 UNION ALLSELECT 'PFCeiling', 6500DECLARE @mstcompanyestablishment table(ID int,PFPercent Numeric(10,2),CPFPercent Numeric(10,2),PFCeiling Numeric(10,2))INSERT INTO @mstcompanyestablishmentSELECT 1, 12, 8.33, 6000 UNION ALLSELECT 2, 12.5, 8.33, 6500SELECT m.ID,m.Category,m.ValueFROM (SELECT ID,Category,ValueFROM @mstcompanyestablishment tUNPIVOT(Value FOR Category IN ([PFPercent],[CPFPercent],[PFCeiling]))u) mLEFT JOIN @Lookup lON l.ColumnName= m.CategoryAND l.Value=m.ValueWHERE l.ColumnName IS NULLoutput-----------------------------ID Category Value1 PFCeiling 6000.002 PFPercent 12.50 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 03:00:39
|
| Can you do it without knowing the column names before hand though? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-20 : 03:01:48
|
quote: Originally posted by Kristen Can you do it without knowing the column names before hand though?
you can by using dynamic sql------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 03:04:03
|
| Seems reasonable, thanks. I assumed there was no "non-dynamic-SQL" method using Pivot (although that would be useful!) thanks for confirming. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-20 : 03:08:04
|
quote: Originally posted by Kristen Seems reasonable, thanks. I assumed there was no "non-dynamic-SQL" method using Pivot (although that would be useful!) thanks for confirming.
You're welcome You need to determine the columns at runtime so you need to generate the column list and add it dynamically inside unpivot query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-20 : 03:40:12
|
| Visakh's query is solution of my problem...and yes that is true i will have to generate column list also because that was the sample data in my case i have lot of columns in my tables thanks visakh...Vabhav T |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-20 : 03:41:46
|
| But i could nt understand kirsten's queryVabhav T |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-20 : 03:45:57
|
quote: Originally posted by vaibhavktiwari83 Visakh's query is solution of my problem...and yes that is true i will have to generate column list also because that was the sample data in my case i have lot of columns in my tables thanks visakh...Vabhav T
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 04:32:35
|
| "But i could nt understand kirsten's query"Just change the "StandardTable" to your actual table name and run it. Its only SELECT statements, it won't do any harm ...... otherwise I'm wasting my time trying to help you." i will have to generate column list also because that was the sample data in my case i have lot of columns in my tables"I figured that, which is why I wrote my query to solve that problem for you. |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-20 : 05:02:01
|
| Hey kristen dont be angry...I changed your query with proper table name but its giving errorMsg 207, Level 16, State 1, Line 4Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 4Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 4Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 10Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 11Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 11Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 12Invalid column name 'ColumnName'.Msg 207, Level 16, State 1, Line 12Invalid column name 'ColumnName'.Vabhav T |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 06:37:11
|
Sorry, I got your mstcompanyestablishment and StandardTables the wrong way round. I've changed the original code (above) to correct this,.Here's the code using Visakh's data:CREATE TABLE #StandardTable(ColumnName varchar(100),Value Numeric(10,2))INSERT INTO #StandardTableSELECT 'PFPercent', 12 UNION ALLSELECT 'CPFPercent', 8.33 UNION ALLSELECT 'PFCeiling', 6500CREATE TABLE #mstcompanyestablishment(ID int,PFPercent Numeric(10,2),CPFPercent Numeric(10,2),PFCeiling Numeric(10,2))INSERT INTO #mstcompanyestablishmentSELECT 1, 12, 8.33, 6000 UNION ALLSELECT 2, 12.5, 8.33, 6500 UNION ALLSELECT 3, 12, 8.33, 6500 -- This row is OKSELECT 'SELECT ID 'UNION ALLSELECT DISTINCT ', [T_' + ColumnName + '] = CASE WHEN [' + ColumnName + '] IS NULL THEN NULL WHEN ' + ColumnName + '_VAL.ColumnName IS NULL THEN ''False'' ELSE ''True'' END'FROM dbo.#StandardTableUNION ALLSELECT 'FROM #mstcompanyestablishment'UNION ALLSELECT DISTINCT 'LEFT OUTER JOIN dbo.#StandardTable AS ' + ColumnName + '_VAL ON ' + ColumnName + '_VAL.ColumnName = ''' + ColumnName + '''' + ' AND ' + ColumnName + '_VAL.Value = ' + ColumnNameFROM dbo.#StandardTableGenerates this code:SELECT ID , [T_CPFPercent] = CASE WHEN [CPFPercent] IS NULL THEN NULL WHEN CPFPercent_VAL.ColumnName IS NULL THEN 'False' ELSE 'True' END, [T_PFCeiling] = CASE WHEN [PFCeiling] IS NULL THEN NULL WHEN PFCeiling_VAL.ColumnName IS NULL THEN 'False' ELSE 'True' END, [T_PFPercent] = CASE WHEN [PFPercent] IS NULL THEN NULL WHEN PFPercent_VAL.ColumnName IS NULL THEN 'False' ELSE 'True' ENDFROM #mstcompanyestablishmentLEFT OUTER JOIN dbo.#StandardTable AS CPFPercent_VAL ON CPFPercent_VAL.ColumnName = 'CPFPercent' AND CPFPercent_VAL.Value = CPFPercentLEFT OUTER JOIN dbo.#StandardTable AS PFCeiling_VAL ON PFCeiling_VAL.ColumnName = 'PFCeiling' AND PFCeiling_VAL.Value = PFCeilingLEFT OUTER JOIN dbo.#StandardTable AS PFPercent_VAL ON PFPercent_VAL.ColumnName = 'PFPercent' AND PFPercent_VAL.Value = PFPercentwhich gives these resultsID T_CPFPercent T_PFCeiling T_PFPercent ----------- ------------ ----------- ----------- 1 True False True2 True True FalseCleanup:DROP TABLE #StandardTableGODROP TABLE #mstcompanyestablishmentGO |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-02-20 : 06:41:26
|
P.S. If you want to only include rows that have at least one FALSE then add this to the initial query:UNION ALLSELECT 'WHERE 1=0 'UNION ALLSELECT DISTINCT ' OR ([' + ColumnName + '] IS NOT NULL AND ' + ColumnName + '_VAL.ColumnName IS NULL)'FROM dbo.StandardTable |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-20 : 06:55:15
|
| Thanks KirstenVabhav T |
 |
|
|
|