I am writing a very simple query where I need to retrieve the records based on a column that has two values: 0 and 1. I didn't realize this column has a bit type, therefore when I write the query SQL Server is giving me the message
"Incorrect syntax near the keyword 'Primary'."
My query is simple:
select * from [table name]
where Primary = '1'
I've tried searching the site but couldn't find a good answer. BTW, I only have access to retrieve the data from the table. I can't declare variables or create a stored procedure or any of that stuff. Surely this can't be that complicated. Please assist!
PRIMARY is reserved word and it needs to be quoted:
select * from [table name] where [Primary] = 1;
The problem has nothing to do with the datatype being a bit, the error, in fact, is telling you exactly what the problem is:
Incorrect syntax near the keyword 'Primary'.
Emphasis added.
PRIMARY is a reserved keyword. Ideally don't use keywords for object names, but if you "must" then you must delimit identify the object. In fact, really you should avoid any names for objects that require delimit identifing:
SELECT {Your Columns} --Define the columns, don't use *
FROM dbo.[Table Name] --I hope you don't have white space in your object names too
WHERE [Primary] = 1; --Don't wrap bit/numerical values in quotes.
That's because primary is a reserved word in SQL Server. You would need to quote it:
select * from [table name] where [Primary] = 1
I would warmly recommend changing the column name so it does not conflict with a langauge keyword. There are less than 200 reserved works in SQL Server, which leaves a lot of room for alternatives.
So:
sp_rename '[table name].[primary]', 'prim', 'COLUMN';
Related
I have a column in my database table named UID. For some reason queries fail unless I surround the column name with double quotation marks (" "). None of the other columns require these quotation marks.
For example, this doesn't work:
SELECT user_name FROM user_table WHERE UID = '...'
But this does:
SELECT user_name FROM user_table WHERE "UID" = '...'
Is UID some kind of keyword? Why is it only happening to that column? How do I know if I need to use double quotes for other columns?
By the way, I'm running JDK 1.8_221 and using an oracle JDBC driver if that makes a difference.
Yes, it is about keywords. You can double quote everything (tables, columns) to avoid this but I can understand you don't want to do this.
To have a list of standard keywords: SQL Keywords
But you can see UID is not in this list as I assume it is a reserved keyword by your database implementation. I had the same problem with a table called "order" as it contains orders. ORDER is a keyword so I had to quote it each time.
So best is to test your statements using a SQL client tool.
Since you mention Oracle: Oracle keywords: "You can obtain a list of keywords by querying the V$RESERVED_WORDS data dictionary view."
If your create table command for user_table looks something like this:
create table user_table ("UID" varchar2(10))
then you will have to use quotes around UID in your query. This query:
select * from user_table where UID = 'somestring'
means to use the Oracle predefined UID pseudo column and your table's UID column will not be accessed.
If your table doesn't have a user-defined UID column, then using "UID" should fail.
My guess is your table does indeed have a UID column and when you say it "doesn't work" without using the quotes you probably mean it motivates an ORA-1722.
The type of failure, when using UID without quotes, depends on the content of the string 'somestring'. If the content of that string can be cast as a number then you probably won't get the rows you expect. If it cannot be cast as a number then you'll get an ORA-1722.
As an aside, if you try to execute this, then you'll get an ORA-904:
create table user_table (UID number)
Yes, it is keywords and return
UID returns an integer that uniquely identifies the session user (the
user who logged on).
By default, Oracle identifiers (table names, column names, etc.) are case-insensitive. You can make them case-sensitive by using quotes around them when creating them (eg: SELECT * FROM "My_Table" WHERE "my_field" = 1). SQL keywords (SELECT, WHERE, JOIN, etc.) are always case-insensitive.
You can use it for more information here.
I'm trying to create a simple little view but I'm keep getting errors all over the query. [Accepted by Month] has the error No column was specified for column 1 of 'Accepted by Month'. I have looked up pretty much everything online and I know that I need to give the columns I select an alias but whenever I do that it just gives me an error under the AS I use for the column aliases saying incorrect syntax near. Plus I'm getting errors with the comma in the SELECT statement and an error for FROM both saying incorrect syntax.
Someone show me how I should write this query because I haven't made any progress in over an hour on a simple CREATE VIEW.
USE Database_Name
GO
CREATE VIEW [Accepted by Month] AS
SELECT Case.Accepted, Case.CaseID
FROM Case;
You can use the following to solve your problem:
USE Database_Name
GO
CREATE VIEW [Accepted by Month] AS
SELECT [Case].Accepted, [Case].CaseID
FROM [Case];
The brackets are required if you use keywords or special chars in the column names or identifiers or names with white space.
USE Database_Name
GO
CREATE VIEW [Accepted by Month] (Accepted, CaseID) AS
SELECT Accepted, CaseID
FROM [Case];
I made a mistake and entered:
SELECT * FROM table LIMIT1
instead of
SELECT * FROM table LIMIT 1 (note the space between LIMIT and 1)
in the CLI of MySQL. I expected to receive some kind of parse error, but I was surprised, because the query returned all of the records in the table. My first thought was "stupid MySQL, I bet that this will return error in PostgreSQL", but PostgreSQL also returned all records. Then tested it with SQLite - with the same result.
After some digging, I realized that it doesn't matter what I enter after the table. As long as there are no WHERE/ORDER/GROUP clauses:
SELECT * FROM table SOMETHING -- works and returns all records in table
SELECT * FROM table WHERE true SOMETHING -- doesn't work - returns parse error
I guess that this is a standardized behavior, but I couldn't find any explanation why's that. Any ideas?
Your first query is equivalent to this query using a table alias:
SELECT * FROM yourtable AS LIMIT1
The AS keyword is optional. The table alias allows you to refer to columns of that table using the alias LIMIT1.foo rather than the original table name. It can be useful to use aliases if you wish to give tables a shorter or a more descriptive alias within a query. It is necessary to use aliases if you join a table to itself.
From the SQL lite documentation:
This is why I want DB engine to force the usage of keyword AS for alias names
http://beyondrelational.com/modules/2/blogs/70/posts/10814/should-alias-names-be-preceded-by-as.aspx
SELECT * FROM table LIMIT1;
LIMIT1 This has taken as alias by SQL, cause LIMIT1 is not a reserved literal of SQL.
Something after table name and that is not a reserved keyword always taken as an table alias by SQL.
SELECT * FROM table LIMIT 1;
When you used LIMIT just after the table name, SQL found that as a reserved keyword and worked for it as per the behavior. IF you want to use reserved key words in query It can be done by putting reserved literals in quotes. like..
SELECT * FROM table `LIMIT`;
OR
SELECT * FROM table `LIMIT 1`;
Now all words covered under `` quotes will treated as user defined.
Commonly we did mistake with date, timestamp, limit etc.. keywords by using them as column names.
I know you cannot use a alias column in the where clause for T-SQL; however, has Microsoft provided some kind of workaround for this?
Related Questions:
Unknown Column In Where Clause
Can you use an alias in the WHERE clause in mysql?
“Invalid column name” error on SQL statement from OpenQuery results
One workaround would be to use a derived table.
For example:
select *
from
(
select a + b as aliased_column
from table
) dt
where dt.aliased_column = something.
I hope this helps.
Depending on what you are aliasing, you could turn it into a user defined function and reference that in both places. Otherwise your copying the aliased code in several places, which tends to become very ugly and means updating 3+ spots if you are also ordering on that column.
I am very, very new to MYSQL.I tried to create a table named "option".
My SQL Query is :
create table option(
id int not null primary key auto_increment,
choice varchar(30)
)
While executing this query it shows the following error
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option(
id int not null primary key auto_increment,
choice varchar(30)
)' at line 1
(0 ms taken)
If I try with the table name as "choice" it is working.
can we have the table name as "option" in mysql?
thanks
If you want to have a table name Option, you should be able to, just remember that whenever you use the table in a query, you will have to encase it in ` symbols. Like this.
`option`
The ` key on the top left of your keyboard, with the tilde.
Pick a different name (one that isn't a reserved word in your RDBMS) and save yourself and whoever else might work on it many headaches.
option is a reserved word in Mysql.we can use a reserved word by using the word inside a single quotes.
Better you select the other tablename.Ohterwise maintaining our code will be difficult.
You can use SQL keywords as table names in MySQL if you escape them with back-quotes.
CREATE TABLE `option` (
...
)
It's not normally a good idea to do so, though.
option is a reserved word in MySQL. Save yourself a world of pain and use choice for your table name.
See the MySQL documentation on this. You can do it as follows:
create table `option` (
...
)
Yes you can definitely create a table named option but in every query you will have to use
`option`
instead of plain option. Better improvise a little and create a table named options to save from trouble. Restrain from using mysql reserved words as table name or column name or procedure names.