0

I have two tables and need to retrieve some columns from both tables based on the common key which is ProductID. I tried the following but got an error "Incorrect syntax near the keyword 'JOIN'"

string selectProductStatement = 
"SELECT Products.Code, Products.Description,"+
"Products.Category, Products.Price, BookProducts.Author"+
"FROM Products"+
"INNER JOIN BookProducts ON (Products.@ProductID = BookProducts.ProductID)";

SqlCommand comm = new SqlCommand(selectProductStatement, connection);
comm.Parameters.AddWithValue("@productID", productID);

the tables are:

Products

  ProductID
  Code
  Description
  Category
  Price

BookProducts

  BookID PK
  ProductID FK
  Author
1
  • 1
    you're missing some spaces.. after BookProducts.Author and after FROM Products Commented Feb 23, 2012 at 16:55

7 Answers 7

4

You're missing spaces in your SQL:

string selectProductStatement = 
    "SELECT Products.Code, Products.Description,"+
    "Products.Category, Products.Price, BookProducts.Author"+
    " FROM Products"+
    " INNER JOIN BookProducts ON (Products.@ProductID = BookProducts.ProductID)";
Sign up to request clarification or add additional context in comments.

Comments

3

Personally I like to use a literal string for SQL commands to get rid of all the concatination all together. Saves time and headaches!

note the @ symbol before defining the string. Also wrapping commands in a 'using' brace means you cant forget to dispose of it.

 string selectProductStatement = @"           SELECT 
                                              Products.Code, 
                                              Products.Description,
                                              Products.Category, 
                                              Products.Price, 
                                              BookProducts.Author
                                              FROM Products
                                              INNER JOIN BookProducts 
                                              ON (Products.@ProductID = BookProducts.ProductID) ";
            string connectionString = string.Empty;

            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(selectProductStatement, sqlConnection))
                {
                    sqlCommand.Parameters.AddWithValue("@productID", productID);

                    //Etc
                }
            }

Comments

3

There is no space between Products and INNER. Add a space before and after FROM Products on the 3rd line of your query and you'll be fine :-)

Comments

2

Reckon you just need a space between "PRODUCTS" and "INNER" there mate, i.e.

"FROM Products "+
"INNER JOIN BookProducts ON (Products.@ProductID = BookProducts.ProductID)";

Comments

2

Defining the string as a string literal can help with erroneous spacing:

string selectProductStatement = 
 @"SELECT Products.Code, Products.Description, 
Products.Category, Products.Price,
 BookProducts.Author FROM Products 
INNER JOIN BookProducts ON (Products.@ProductID =
BookProducts.ProductID)";

It omits the need - and associated problems/readability of multiple string fragments

Comments

2

Try the following

string selectProductStatement = 
"SELECT Products.Code, Products.Description,"+
"Products.Category, Products.Price, BookProducts.Author"+
" FROM Products "+
"INNER JOIN BookProducts ON (Products.ProductID = BookProducts.ProductID) WHERE Products.ProductId = @productID";

Comments

0
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name;

Above is the general syntax for inner join query. Just follow accordingly. :)

1 Comment

The question had the correct syntax there were spaces missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.