Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mmalaka
Starting Member
33 Posts |
Posted - 2013-03-20 : 10:40:45
|
I hope this is not a tricky one...I am building a stored procedure which will go through some table in SQL 2000 database to build a table that shows field names from multiple tables and the count of the distinct valuesmy script is something like thisSELECT @sSQL = 'select distinct(Field1) Field1, count(*) counter from Table1 group by Field1 UNION ALL select distinct(Field1) Field1, count(*) counter from Table2 group by Field1'EXEC sp_executesql @sSQL Now my issue is that at some cases the required field would not be in Table1 or In table2 but for sure it is in one of the tables at least...So How can I add a validation into my script |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-20 : 10:55:08
|
just add a validation likeIF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME =<yourcolumn> AND TABLE_NAME = <yourtable>)your select... ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-21 : 00:28:53
|
Why yu need DISTINCT keyword in your script....Group By clause itself will give you the distinct values'select distinct(Field1) Field1, count(*) counter from Table1 group by Field1 UNION ALL select distinct(Field1) Field1, count(*) counter from Table2 group by Field1'--Chandu |
|
|
|
|
|