Results 1 to 6 of 6

Thread: Catch-all Join statement

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    Catch-all Join statement

    I'm just wondering if this is even possible: I'm building a query program for our users. There are 5 tables that they could gather various pieces of data from, and I'm wondering if there is a "FROM" statement that I can just tack on that will gather whatever data they input, so I don't have to worry about building custom FROM statements. I've tried a variety of combinations with INNER JOINS, but if I include more than 2 tables, it doesn't return anything.

    Any ideas?

    edit: Using SQL Server 2K, by the way.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Have you looked at using a Left or Right join instead of an Inner Join? If you ever get your tables joined and return records, I'd stick that in a View, then have your users run their queries against that view, that way you don't have to dynamically create the "From" statement each time they run a query.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  3. #3

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    But aren't views looking at the same records all the time? I want them to be able to dynamically pick the fields that are going to be in the report.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Views and/or SQL statements are the only two options to use when it comes to creating ad-hoc queries. To account for all possible scenarios you may need to create multiple Views. The user chooses the View they want and then they pick the fields they want from the view.

    Dynamically building the sql statement shouldn't be that difficult but it would depend on how the 5 tables are related.

  5. #5
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You would expose all fields from all 5 tables through the view, so that the users could select which fields they wanted to include on the report. Say I wanted to create a view on 2 tables:
    Code:
    Create View vw_Report
    As
    
    Select t1.recID
         , t1.fname
         , t1.lname
         , t2.dept_id
         , t2.dept_name
    From table1 t1 Inner Join table2 t2 On t1.dept_id = t2.dept_id
    Then in your calling application, the users could select any of the fields listed in the Select clause of the view and you could build your report statement like:
    VB Code:
    1. Dim sSql As String
    2.  
    3. sSql = "Select lname, dept_name From vw_Report Where lname ='blah'"
    For your report, you would need to list all fields from all five tables so that they are exposed via the view for your end users. The View is only there to eliminate the need to perform all of the table joins in your client application.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    If you are using SQL 2K, then you need to evaluate why you aren't using STORED PROCEDURES with PARAMETERIZE arguments...

    You can have IF statements, and multiple SELECT statements - it's why you use SQL 2K anyway...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width