Link to home
Start Free TrialLog in
Avatar of hrolsons
hrolsonsFlag for United States of America

asked on

SQL syntax

Can someone tell me why this:

UPDATE dbo.table1 SET dbo.table1.field1 = "setting1"
WHERE ((dbo.table1.field1 Like "%setting2%") AND (dbo.table1.field2=1));

Results in this:

Msg 207, Level 16, State 1, Line 2
Invalid column name '%setting2%'.
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Use single quotes in queries.

However, you can't use like with a column name. The name has to be hardcoded unless you use dynamic SQL.
ASKER CERTIFIED SOLUTION
Avatar of Ashok
Ashok
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The first comment in this question is correct.

as a small aside, whatever query designer is being used here is adding unnecessary table references and parentheses.  This is much easier to read.
UPDATE dbo.table1 
SET field1 = 'setting1'
WHERE field1 Like '%setting2%' AND field2=1

Open in new window

hrolsons

I really must object to the accept in this question, as the first comment correctly answered your question, and was ignored in the accept.   The second answer was also correct, albeit by posting the code instead of explaining the reason.

EE protocol dicates that if two answers are correct, either the first one is awarded points, or there is a split among all correct answers.

Let me know if you are willing to re-open the question to award at least half points to the first answer.  Failing that, I'll do it myself.

Thanks in advance.
Avatar of hrolsons

ASKER

I agree about the single quotes but did not understand:

"However, you can't use like with a column name. The name has to be hardcoded unless you use dynamic SQL."

Because when I changed it from double to single quotes, it worked.
>did not understand: ... "However, you can't use like with a column name. The name has to be hardcoded unless you use dynamic SQL."

Any chance you could have asked?  There is an expectation that askers work with the experts that provide them solutions.
He said "You can't" and I disagreed because when I changed the quotes, it worked, so he was obviously mistaken.
Very well.  

I was wondering about that myself.
Msg 207, Level 16, State 1, Line 2
Invalid column name '%setting2%'.

In SQL Server it is possible to reference fields by column aliases like so:

select
  field_1 as name_here
, field_2 as [another name]
, field_3 as "yet another name" --<< using double quotes

In the query, because you originally used double quotes, the string "%setting2%" was interpreted as a column alias, and that is why you got that particular error message.

lsavidge was pointing out that field names (and/or column aliases) cannot be dynamically inserted into SQL (unless you construct a whole sql statement then execute that statement).  i.e. finding a column names that really is like %setting2% could not be dynamically inserted into that location of the query.

It took me quite a lot of words to elaborate - but that I think is point that was being made.

{+ edit - sorry}