How to INSERT to a column whose name is a sql keyw

2019-01-27 13:19发布

问题:

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

回答1:

You can surround column names like that with [ ] brackets. Therefore:

insert into mykeyvalues ([Key],[Value]) values ('FooKey', 'FooValue')


回答2:

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.



标签: sql insert