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

2019-01-27 13:25发布

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 insert
2条回答
smile是对你的礼貌
2楼-- · 2019-01-27 13:25

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

insert into mykeyvalues ([Key],[Value]) values ('FooKey', 'FooValue')
查看更多
Ridiculous、
3楼-- · 2019-01-27 13:33

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.

查看更多
登录 后发表回答