I need a table that stores key-value pairs, so I created one with a column called "Key" and a column called "Value".
This fails:
insert into mykeyvalues (Key,Value) values ('FooKey', 'FooValue')
"Incorrect syntax near the keyword 'key'."
Maybe I shouldn't call it "Key", but I just wonder if it is possible to work with a column whose name is a sql keyword?
Thanks
SQL AS keyword is used to give an alias to table or column names in the queries.
You use column-ref to reference a column (database field), optionally qualified by a table and/or schema name. The column reference comprises the data type of the database field that the column represents (see Data Types).
SQL Server does not have to distinguish this as a reserved keyword. Transact-SQL reserved keywords can be used as identifiers or names of databases or database objects, such as tables, columns, views, and so on. Use either quoted identifiers or delimited identifiers.
You can surround column names like that with [ ] brackets. Therefore:
insert into mykeyvalues ([Key],[Value]) values ('FooKey', 'FooValue')
Use either backticks (`) or double quotes (") around the identifiers in your query. For example:
INSERT INTO mykeyvalues ("Key", "Value") values ('FooKey', 'FooValue')
But in the long-run, this just reduces portability. It's easier to use a different name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With