Why doesn't the SQL query work?
Hello, can someone help me?
Basic idea:
Search all columns in a table for a word (without specifying the columns directly). Use hexadecimal for uniqueness. I'm running the query in SQLer Mkll version 2.5.6 and Arc32.
My query is as follows:
DECLARE @SearchWord NVARCHAR(55) = '0x4f'; SELECT t.name AS Tabellenname, c.name AS Spaltenname FROM sys.tables t JOIN sys.columns c ON t.object_id = c.object_id WHERE t.name = 'kundenadresse' AND EXISTS ( SELECT 1 FROM kundenadresse WHERE CAST(c.name AS NVARCHAR(MAX)) LIKE '%' + @SearchWord + '%' );
Now comes the error:
poQuery: Error 7200: AQE Error: State = 42000; NativeError = 2115; [SAP][Advantage SQL Engine]Expected lexical element not found: expecting <;> or <,> but found <=> — Location of error in the SQL statement is: 34
It seems like he doesn't understand that.
Has anyone found another solution or an error in the query?
Thank you in advance!
The error you get occurs because you use the comparison operator LIKE in combination with the = operator in the WHERE clause. SQL interprets the = sign in line CAST(c.name AS NVARCHAR(MAX) LIKE ‘%’ + @SearchWord + ‘%’ as an attempt to use a condition as =, and this leads to a syntax error.
Now the error code continues. Can it be all =, because something is wrong in position 34 again? I tried once: As soon as I take the = away in the Declare, he mugs around or gives a new error message, that is to say it is the = there. What can I do or try? Thank you for your previous answer!
Great.
Think the problem has just been solved. Now he’s picking up at the bottom of the Where Charindex:
SQL1 (DD Remote 12.0) Error:
AdsQuery1: Error 7200: AQE Error: State = 42000; NativeError = 2115; [SAP][Advantage SQL Engine]Expected lexical element not found: — Missing table name. There was a problem parsing the table names after the FROM keyword. There was a problem parsing the WHERE clause. — Location of error in the SQL statement is: 259 (line: 16 column: 5)
I don’t think you can explain the variable.
Alternatives: https://www.sqlshack.com/sql-variables-basics-and-usage/