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!

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
5 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
GrayCoder
1 year ago

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.

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 CHARINDEX(@SearchWord, CONVERT(NVARCHAR(MAX), c.name, 1)) > 0
);


elmex7
1 year ago

I don’t think you can explain the variable.

Alternatives: https://www.sqlshack.com/sql-variables-basics-and-usage/