Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Simpler way to join all unique columns for a join? Expand / Collapse
Author
Message
Posted Wednesday, August 29, 2012 8:47 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 21, 2014 4:37 AM
Points: 25, Visits: 66
Here's the Union that I clearly can't do.... I have 30 tables of inspections, deliniated by *Inspection. I have included the (of course erroring) Select * from a few below. Most columns are identical and this is what I'm traying to make a report of. Is there a way to grab all current tables in the db with columns like '%Inspection' and get all of the unique columns too? Any help would be GREATLY appreciated!


SELECT     'Beach' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType, 
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, Comments, ViolationModule, EHSID, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, TotalViolations,
Enforcement, ReleaseDate, SectionDisplay, SectionModule, SectionNumber, SectionGroup, SectionCategory, SectionDescription, SectionStatus
FROM BeachInspection
UNION ALL
SELECT 'BodyArt' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, Enforcement, ReleaseDate, Comments, ReviewedBy,
ReviewedDate, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM BodyArtInspection
UNION ALL
SELECT 'Campground' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, Enforcement, ReleaseDate, Comments, ReviewedBy,
ReviewedDate, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM CampgroundInspection
UNION ALL
SELECT 'ChildCare' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, ReviewedBy, ReviewDate, Comments, TimeType, TimeIn,
TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM ChildCareInspection
UNION ALL
SELECT 'Food' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, SampleSelection, Samples, Records, TotalMachines,
TotalInspected, NextInspDue, NextManualDue, FollowupInspectionRequired, NextInspection, ObservedSeating, Comments, CriticalControlComments, ViolationModule,
ViolationSubModule, EHSID, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, TotalViolations
FROM FoodInspection
UNION ALL
SELECT 'General' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, Enforcement, ReleaseDate, Comments, ReviewedBy,
ReviewedDate, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments
FROM GeneralInspection
UNION ALL
SELECT 'Hotel' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, Enforcement, ReleaseDate, Comments, ReviewedBy,
ReviewedDate, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM HotelInspection
UNION ALL
SELECT 'LaborCamp' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, Enforcement, ReleaseDate, Comments, ReviewedBy,
ReviewedDate, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM LaborCampInspection
UNION ALL
SELECT 'MobileHome' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, ReviewedBy, ReviewedDate, Comments, TimeType, TimeIn,
TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM MobileHomeInspection
UNION ALL
SELECT 'Pool' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, Regulations, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, MainChlorine, CombinedChlorine, Alkalinity,
MainpH, MainTemp, CyanuricAcid, MainPressure, MainFlow, Enforcement, ReleaseDate, Comments, ReviewedBy, ReviewedDate, TimeType, TimeIn, TimeOut, Min,
Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM PoolInspection
UNION ALL
SELECT 'School' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Name, FacilityType, InspectionType, AllYearRound, BillingType, EHO, InspectionDate,
ComplaintDate, FollowupInspectionRequired, NextInspection, Action, ReleaseDate, ObservedSeating, NumCritical, NumNonCritical, NumRepeat, Billable, DocumentID,
EHSID, NextInspDue, ScheduledInspectionDate, Source, ViolationDisplay, ViolationSection, ViolationSectionDescription, ViolationSubModule, ViolCode,
ViolCorrectedSet, ViolCorrectiveActions, ViolDescription, ViolObservations, ViolRepeat, ViolSection, TotalTimeSpentHours
FROM SchoolInspection
UNION ALL
SELECT 'SepticRemoval' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, FacilityPhysicalSuite, FacilityPhysicalBuilding,
FacilityPhysicalDirection, FacilityPhysicalStreetName, FacilityPhysicalStreetType, FacilityPhysicalStreetSuffix, FacilityPhysicalCity, FacilityPhysicalProvince,
FacilityPhysicalPostalCode, FacilityTelephone, FacilityApprovedNumber, FacilityRegType, OwnersName, OwnersAddress, OwnersCity, OwnersState,
OwnersPostalCode, EHOPhoneNumber, SubOffice, SubOfficeAddress, SubOfficeCity, SubOfficePostalCode, SubOfficePhone, NextInspDue, NextManualDue,
CreatedFromInspID, DisplayFollowUp, FollowupInspectionRequired, NextInspection, Comments, ViolationModule, EHSID, NumCritical, NumNonCritical, NumRepeat,
TotHazardRating, LetterGrade, TotalViolations, HazRatingType, SectionModule, SectionNumber, SectionGroup, SectionCategory, SectionDescription,
SectionStatus
FROM SepticRemovalInspection
UNION ALL
SELECT 'SolidWaste' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, FacilityName, FacilityType, EHO, LogDate, TimeIn,
TimeOut, InspectionStatsHist, TimeHoursHist, TimeSpent, NextInspDue, NextManualDue, FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical,
NumRepeat, ViolationDisplay, CorrectionDisplay, ViolCode, ViolCorrectedSet, ViolCorrectiveActions, ViolCriticalSet, ViolDescription, ViolObservations, ViolRepeat,
ViolSection, Type, LicenseStatusAction, ReleaseDate, ParentID, InspectionDate, TotHazardRating
FROM SolidWasteInspection
UNION ALL
SELECT 'SummerCamp' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, Enforcement, ReleaseDate, Comments, ReviewedBy,
ReviewedDate, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments, LotusDateCreated, LotusDateModified
FROM SummerCampInspection
UNION ALL
SELECT 'Water' AS module, ID, UNID, SourceDatabaseID, SourceDatabaseName, ParentUNID, LastModified, Source, DateCreated, DocumentID, Name, FacilityType, AllYearRound, BillingType,
EHO, InspectionDate, Type, ComplaintDate, OtherType, ScheduledInspectionDate, Billable, BillablePermit, NextInspDue, NextManualDue,
FollowupInspectionRequired, NextInspection, Regulations, NumCritical, NumNonCritical, NumRepeat, TotHazardRating, IssuePermit, IssuePermitComments,
DeficienciesCorrected, DeficienciesCorrectedComments, CommentsChemSample, ChemSampleComments, ActionTaken, ActionComments, InformationExchanged,
InformationExchangedComments, Followup, FollowupComments, IssueBoilAdvisory, IssueBoilAdvisoryComments, WrittenOrder, WrittenOrderComments,
MicroSample, MicroSampleComments, LiftBoilAdvisory, LiftBoilAdvisoryComments, TimeType, TimeIn, TimeOut, Min, Hours, TimeSpent, Module, TrackingComments,
Comments, LotusDateCreated, LotusDateModified
FROM WaterInspection

Post #1351985
Posted Wednesday, August 29, 2012 10:29 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 1:33 PM
Points: 593, Visits: 2,885
Have a look at using the information_schema.colums collection, you can find the tables with the column you are interested in, then get all columns from the table and build your queries from that.


Post #1351997
Posted Wednesday, August 29, 2012 11:47 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:10 PM
Points: 6,237, Visits: 7,392
kinderdesign (8/29/2012)
Most columns are identical and this is what I'm traying to make a report of. Is there a way to grab all current tables in the db with columns like '%Inspection' and get all of the unique columns too? Any help would be GREATLY appreciated!


While you could find all the tables with the name you want (sys.systables and sys.syscolumns), you're going to run into a problem trying to union them together.

The first select in the union will control your column count and the underlying datatypes. You won't be able to alter it after the first select. You'll be able to put together all the identitical columns, but you won't be able to include all the unique ones underneath without creating a 'shell' column of some kind in the first query.

You'll end up having to make enough varchar() columns to cover all eventual possibilities, fill the blanks in when they're not used, and convert anything else into varchar() and drop it into the placeholder columns. Because of that, you won't want to automate the independents, you're going to have to account for them very particularly.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1352013
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse