Results 1 to 6 of 6

Thread: [2005] SQL Join question

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    [2005] SQL Join question

    I've got 2 tables (USviews & USviewsDescription). I'm trying to get data from both to put into a datatable. I keep getting an error message about the SQL statement being incorrect near the WHERE clause. I've never done JOIN statements before, could someone tell me what's the matter if at all possible?

    vb Code:
    1. sel = "SELECT * FROM USviews NATURAL JOIN USviewsDescription WHERE UV_SessionID = myint"

    UV_SessionID is a column name in the USviews table. Is it just the simple fact my logic is totally wrong and I can't do this or is there another reason? I also tried specifying: WHERE USviews.UV_SessionID = myInt

    and also tried replacing myInt with an Integer value but nothing worked. I'm obviously doing something completely wrong. Can anyone shed some light on it?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] SQL Join question

    vb Code:
    1. sel = "SELECT * FROM USviews NATURAL JOIN USviewsDescription ON UV_SessionID = myInt"

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] SQL Join question

    I would say like this:

    Select * From USviews Inner Join USviewsDesctionion On UV_SessionID Where UV_SessionID = " & myInt.ToString()

    The myInt should be outside the string qoutes to allow the variables value to be used.

    I would really like to see the database structure to eally help out though.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] SQL Join question

    talkro, I got the following error message (although i moved the myInt outside the string as Gary suggested). I got the following error:

    The multi-part identifier "USviews.UV_SessionID" could not be bound.

    Gary, when I tried yours I got this message:
    An expression of non-boolean type specified in a context where a condition is expected

    I think I'm doing something badly wrong here although I've double checked the column names, values and datatypes, they are correct. I'll keep trying, thanks.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] SQL Join question

    What are the database fields types? What is the actuall SQL being generalted? My main point is that myInt needs to outside the double qoutes.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: [2005] SQL Join question

    You need to specify how the tables are joined in the ON clause. As far as I know that means you need to reference a column in both tables-not just one.

    Instead of 'On UV_SessionID' I'd say 'ON USviews.UV_SessionID = USviewsDesctionion.UV_SessionID' assuming that both tables have the same column.

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