Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Simpler way to join all unique columns for a join?


Simpler way to join all unique columns for a join?

Author
Message
kinderdesign
kinderdesign
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
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


dogramone
dogramone
SSChasing Mays
SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)

Group: General Forum Members
Points: 630 Visits: 3621
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.



Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5713 Visits: 7660
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search