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%'.
hrolsonsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
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.
0
AshokSr. Software EngineerCommented:
UPDATE dbo.table1 SET dbo.table1.field1 = 'setting1'
WHERE ((dbo.table1.field1 Like '%setting2%') AND (dbo.table1.field2=1));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
hrolsonsAuthor Commented:
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.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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.
0
hrolsonsAuthor Commented:
He said "You can't" and I disagreed because when I changed the quotes, it worked, so he was obviously mistaken.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Very well.  

I was wondering about that myself.
0
PortletPaulfreelancerCommented:
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}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.