0

I am using PHP and when I tried to use the following query on SQL Server, it gives me a syntax error saying there is an invalid use near the key word WHERE. If I don't use the WHERE Status='Open' part in my query, this works fine without any error. Does the SQL server considering Status or Open as keywords?

$query = "SELECT * FROM ElectronicShop ORDER BY Completed WHERE Status='Open'";

Error:

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 156 [code] => 156 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]
Incorrect syntax near the keyword 'WHERE'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'WHERE'. ) )
1
  • Read some books about SQL Commented Aug 1, 2014 at 14:34

1 Answer 1

2

The WHERE clause must be before the ORDER BY clause (with any database, this is not related to SQL Server).

$query = "SELECT * FROM ElectronicShop WHERE Status='Open' ORDER BY Completed";
Sign up to request clarification or add additional context in comments.

3 Comments

Unless you are using the Linq to Entities in .net ;-)
@Kevin Thank you for showing me with an example. Now it says, The data types text and varchar are incompatible in the equal to operator. Should I cast the Open to something else?
Status type is Text ? Why are you using this type ? I would prefer nvarchar.

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.