简体   繁体   中英

Select query to retrieve the value of primary key for a specific row in a table

I am struggling to retrieve the value of primary key for a table. We are using MS SQL Server 2005. The database was designed years back by somebody else (he didn't follow the normalization rules at all). He used Key (which is a keyword in sql server) as the column name for primary key of a table. So I cannot use query like this: select key from table_name where column2 = ?

Could anyone help to write a query to get the value of the primary key for a specific row something like this: select primary_key from tbale_name where column2 = ?

Yes you can, simply wrap column names in backticks:

select `key` from `table_name` where `column2` = ?

Alternatively, depending on your DB, you might use square brackets:

select table_name.[key] from table_name where table_name.[column2] = ?

Edit: I see you said "MS SQL". I think that one works with the square brackets. MySQL accepts the backtick syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM