How to INSERT to a column whose name is a sql keyword
You can surround column names like that with [ ] brackets. Therefore:
insert into mykeyvalues ([Key],[Value]) values ('FooKey', 'FooValue')
How to deal with SQL column names that look like SQL keywords?
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;
How to insert into a table with a column named 'when' and 'user'
To escape reserved keywords in the name of a column (or table), put the name of the column in square brackets:
INSERT INTO APPT (ApptID,Type,Descrip,[When],Flag,[User],DTS)
how do you insert to a column name that is similar to an SQL statement? in MySQL
These words are called reserved words and in MySQL you will need to escape them using backticks:
insert into yourtable (`condition`)
select yourValue
from yourOtherTable;
My suggestion would be to stay away from using these words for tables and columns.
Selecting a column whose name is a reserved SQL keyword
I fail to see why you need this and I would never use it myself.
declare @T table
(
id int,
name varchar(10),
description varchar(25)
)
insert into @T values
(1, 'kkt', 'kkt description'),
(1, 'skt', 'skt description')
select T2.N.value('*[3]', 'varchar(max)')
from (select *
from @T
for xml path('r'), type) as T1(X)
cross apply T1.X.nodes('/r') as T2(N)
Update
You should do like this instead.
select [desc]
from YourTable
Use []
around column names that is reserved words.
Using SQL keyword in title of table or column
wrap the tableName with backtick as ORDER
is a reserved keyword.
SELECT * FROM `ORDER` ORDER BY NAME;
- MySQL Reserved Keywords List
In my own opinion, using reserved keywords are fine except that you do not forget to handle it properly as it will give you such pain in the neck.
But through the years of designing schema, I never had used reserved keywords :D
Related Topics
To Calculate Sum() Two Alias Named Columns - in SQL
SQL Query - Sum(Case When X Then 1 Else 0) for Multiple Columns
Update Query If Statement for Oracle
Should a Composite Primary Key Be Clustered in SQL Server
Spark Dataframe Nested Case When Statement
Why Is Union Faster Than an or Statement
How to Find the User That Has Both a Cat and a Dog
How to Subtract 2 Dates in Oracle to Get the Result in Hour and Minute
SQL Recursion Without Recursion
Insert into a Row at Specific Position into SQL Server Table with Pk
How to Create Xml Schema from an Existing Database in SQL Server 2008
Interesting Tree/Hierarchical Data Structure Problem
Row_Number() Over Not Fast Enough with Large Result Set, Any Good Solution
Split Comma Separated String Table Row into Separate Rows Using Tsql
What Determines the Locking Order for a Multi-Table Query
Postgresql Insert If Not Exists
Postgres SQL 'Could Not Determine Data Type of Parameter' by Hibernate