Simpler way to join all unique columns for a join?

  • 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

  • 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.

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply