dspyank
dspyank

Reputation: 129

SQL Update statement with a column that has a space

I have come across a situation where a column name in the SQL database is [column name] and I am trying to update it by that column.

Selecting is easy: Select [column name] from MyDatabase.

I am trying to do this (parameterized): UPDATE MyDatabase WHERE [column name] = @[column name]

the errors say: column name does not exist: @column

I cant just change the name in the database because it has been around for a while and it will most likely break a bunch of stuff.

Thank you all for your help!

Dave

Upvotes: 1

Views: 4281

Answers (4)

Charles Bretana
Charles Bretana

Reputation: 146469

The parametername does not have to match the column name. (In fact, it cannot have spaces) Just change your parameter name to not include a space.

 UPDATE MyDatabase WHERE [column name] = @columnname

Upvotes: 4

Grigur
Grigur

Reputation: 465

UPDATE MyTable WHERE [column name] = @this_is_parameter_name

Upvotes: 0

Jeremy Smyth
Jeremy Smyth

Reputation: 23493

According to http://technet.microsoft.com/en-us/library/ms176027(v=sql.105).aspx :

Microsoft SQL Server does not recognize variable names and stored procedure parameters that are delimited. These types of identifiers must comply with the rules for regular identifiers.

This means you can't use @[variable name] in your code. You have to make do with @variable_name or similar.

Upvotes: 0

suff trek
suff trek

Reputation: 39777

Parameter/variables cannot have spaces in them. But then again they do not have to match column names.

You can have something like

UPDATE MyDatabase WHERE [column name] = @column_name

Upvotes: 0

Related Questions