One of my columns is called from. I can't change the name because I didn't make it.
Am I allowed to do something like SELECT from FROM TableName or is there a special syntax to avoid the SQL Server being confused?
Wrap the column name in brackets like so, from becomes [from].
select [from] from table;
It is also possible to use the following (useful when querying multiple tables):
select table.[from] from table;
If it had been in PostgreSQL, use double quotes around the name, like:
select "from" from "table";
Note: Internally PostgreSQL automatically converts all unquoted commands and parameters to lower case. That have the effect that commands and identifiers aren't case sensitive. sEleCt * from tAblE; is interpreted as select * from table;. However, parameters inside double quotes are used as is, and therefore ARE case sensitive: select * from "table"; and select * from "Table"; gets the result from two different tables.
These are the two ways to do it:
Use back quote as here:
SELECT `from` FROM TableName
You can mention with table name as:
SELECT TableName.from FROM TableName
While you are doing it - alias it as something else (or better yet, use a view or an SP and deprecate the old direct access method).
SELECT [from] AS TransferFrom -- Or something else more suitable
FROM TableName
Your question seems to be well answered here, but I just want to add one more comment to this subject.
Those designing the database should be well aware of the reserved keywords and avoid using them. If you discover someone using it, inform them about it (in a polite way). The keyword here is reserved word.
More information:
"Reserved keywords should not be used
as object names. Databases upgraded
from earlier versions of SQL Server
may contain identifiers that include
words not reserved in the earlier
version, but that are reserved words
for the current version of SQL Server.
You can refer to the object by using
delimited identifiers until the name
can be changed."
http://msdn.microsoft.com/en-us/library/ms176027.aspx
and
"If your database does contain names
that match reserved keywords, you must
use delimited identifiers when you
refer to those objects. For more
information, see Identifiers (DMX)."
http://msdn.microsoft.com/en-us/library/ms132178.aspx
In Apache Drill, use backquotes:
select `from` from table;
If you ARE using SQL Server, you can just simply wrap the square brackets around the column or table name.
select [select]
from [table]
I have also faced this issue.
And the solution for this is to put [Column_Name] like this in the query.
string query= "Select [Name],[Email] from Person";
So it will work perfectly well.
Hi I work on Teradata systems that is completely ANSI compliant. Use double quotes " " to name such columns.
E.g. type is a SQL reserved keyword, and when used within quotes, type is treated as a user specified name.
See below code example:
CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)
--type is a SQL reserved keyword
TYPE
--see? now to retrieve the column you would use:
SEL "type" FROM alpha1
I ran in the same issue when trying to update a column which name was a keyword. The solution above didn't help me. I solved it out by simply specifying the name of the table like this:
UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')
The following will work perfectly:
SELECT DISTINCT table.from AS a FROM table
Some solid answers—but the most-upvoted one is parochial, only dealing with SQL Server. In summary:
If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS.
Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing:
For SQL Server use [square_braces] around the name. This works in an ODBC connection too.
For MySQL use `back_ticks`.
Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.
Dotting the offending word onto the table name may also work.
You can put your column name in bracket like:
Select [from] from < ur_tablename>
Or
Put in a temprary table then use as you like.
Example:
Declare #temp_table table(temp_from varchar(max))
Insert into #temp_table
Select * from your_tablename
Here I just assume that your_tablename contains only one column (i.e. from).
In MySQL, alternatively to using back quotes (`), you can use the UI to alter column names. Right click the table > Alter table > Edit the column name that contains sql keyword > Commit.
select [from] from <table>
As a note, the above does not work in MySQL
Judging from the answers here and my own experience. The only acceptable answer, if you're planning on being portable is don't use SQL keywords for table, column, or other names.
All these answers work in the various databases but apparently a lot don't support the ANSI solution.
Simple solution
Lets say the column name is from ; So the column name in query can be referred by table alias
Select * from user u where u.from="US"
In Oracle SQL Developer, pl/sql you can do this with double quotes but if you use double quotes you must type the column names in upper case. For example, SELECT "FROM" FROM MY_TABLE
Okay, this may be silly, but i can't make CI to stop quoting my table. I use this code in my model :
$oracle->select('id',FALSE);
$oracle->from('ms_item');
That code will result Select statement as follow:
SELECT id FROM "ms_item"
The problem is, Oracle can't find the table when it have quotes or double quotes. The CI said that second parameter in select() will remove any quote generated by Query Builder, but i don't know how to remove the quote generated by from(), please help...
Thank you...
For note, i'm using Oracle 11g and oci8_11g.dll
Try writing the table name in upper case.
$oracle->from('MS_ITEM');
When a table is created, unless you quote it, it will be stored as an uppercase table name. When you are quoting it ( as CI wants to do), it's performing a case sensitive match
I am trying to migrate one of my Laravel project to Oracle Database (11g). I installed this package to communicate with the database. Everything seems to working If I write queries, but the automatic query builder makes invalid queries. Like:
The registration uses this validation to post:
$validator = Validator::make(Input::all(),array(
'createInputEmail' => 'required|email|unique:User,email|max:254',
'createInputDisplayName' => 'max:24|min:4|alpha_num',
'createInputPassword1' => 'required|max:60',
'createInputPassword2' => 'required|same:createInputPassword1'
));
The problem here is the unique check. I get this error:
oci_execute(): ORA-00903: invalid table name (SQL: select count(*) as aggregate from User where email = asd#asd.sd)
If I copy this query to SQL Developer to test and apply quotes to the table name it works.
So a correct query:
select count(*) as aggregate from "User" where email = asd#asd.sd;
//dont worry about the unquoted email address
I can't put quotes to the rules when I am defining the validator, because it gets escaped.
I tried:
'required|email|unique:\'User\',email|max:254',
'required|email|unique:\"User\",email|max:254',
'required|email|unique:"User",email|max:254',
'required|email|unique:""User"",email|max:254', <-- the first 2 got escaped, last 2 didn't
'required|email|unique:'User',email|max:254',
That's an interesting case. For most Oracle Developer there's a standard to make all table name plurals (users vs user). That would help you to avoid this issue.
Basically, your problem is not just the quotes, you are using an Oracle Reserverd Words: User. More information about Oracle Reserverd Words here: http://docs.oracle.com/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm
If you are the responsible to design the database do the following ASAP:
Avoid using Oracle Reserved Words as table name.
Keep table name as plural, to avoid that particular case.
If the database cannot be altered, you will have to keep looking for a workaround to this issue.
I have a workaround in mind that you need to test it, edit your model User.php and change the table name to include the quotes.
protected $name = '"User"';
I have tested and the quotes appeared in the generated SQL query but it fails to execute because I'm using MySQL and all table name are wrapped with `.
Im using ZF2 with DoctrineModule and DoctrineORMModule.
Problem: I got some Tablenames in my Database that are equal to MYSQL Commands
Doctrine generates Tablenames in SQL Statements without the "`"'s
SELECT ... FROM references
But I want it to be like this Format
SELECT ... FROM `references`
to be safe..
How to Fix it in Doctrine 1 to fix my Problem I had done this:
http://i.stack.imgur.com/UN5KB.png
(link: http://www.doctrine-project.org/documentation/manual/1_0/fr/configuration:naming-convention-attributes:table-name-format)
I do not find any Fix for this Problem in Doctrine2.
Thanks for helping.
In Doctrine2 there is no automatic identifier quoting. What you could do in this case is directly putting the quotes inside your mappings, like following:
#Table(name="`references`")
That should do the trick, but won't work with schemas and SQLite in 2.2.
I created a table named order. (Yes, bad choice I see). If I make a #OneToMany relation I get a mysql syntax error because Doctrine does not write the name in "`". Can't believe it...
How to fix this?
I had to use:
#ORM\Table(name=" `order` ")
And yes, I had to include the spaces between the quotes and the backtics. Very strange behavior.
You should enable quote_identifier with doctrine