How to Insert to a Column Whose Name Is a SQL Keyword

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



Leave a reply



Submit