You have an error in your SQL syntax; check the manual that corresponds to your MySQL [closed]

你。 提交于 2019-12-10 12:05:22

问题


I am trying to run a mysql query to find all occurences of a text. I have a syntax error but dont know where or how to fix it

I am using sqlyog to execute this script

DECLARE @url VARCHAR(255)

SET @url = '1720'

SELECT 'select * from ' + RTRIM(tbl.name) + ' where ' + 
          RTRIM(col.name) + ' like %' + RTRIM(@url) + '%'
FROM sysobjects tbl
INNER JOIN syscolumns col ON tbl.id = col.id 
AND col.xtype IN (167, 175, 231, 239) -- (n)char and (n)varchar, there may be others to include
AND col.length > 30 -- arbitrary min length into which you might store a URL
WHERE tbl.type = 'U'    -- user defined table

1 queries executed, 0 success, 1 errors, 0 warnings

Query: declare @url varchar(255) set @url = '1720' select 'select * from ' + rtrim(tbl.name) + ' where ' + rtrim(col.name) + ' like %' ...

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare @url varchar(255)


回答1:


For MySQL change this:

DECLARE @url VARCHAR(255)
SET @url = '1720'

to:

SET @url := '1720'

In MySQL, you don't DECLARE user variables, just set them to a value. The equal sign works for assignment, except within a SQL statement, where you need to use the Pascal-style := as the assignment operator. (I typically use the := assignment operator in the context of the SET statement, even though in the equal sign works equally fine.)

However, in looking at your SQL statement, it appears this is Microsoft SQL Server syntax, what with the + operator for concatenation, the references to sysobjects, syscolumns.xtype.

There is no way this statement is going to returning anything useful from MySQL.

That whole statement needs to be re-written to query:

information_schema.tables
information_schema.columns

As an example starting point:

SET @url := 'foobar';

SELECT CONCAT('select * from `',col.table_schema
             ,'`.`',col.table_name
             ,'` where `',col.column_name
             ,'` like ''%',RTRIM(@url),'%''') AS q
  FROM information_schema.columns col 
  JOIN information_schema.tables tbl
    ON tbl.table_schema = col.table_schema
   AND tbl.table_name = col.table_name
   AND tbl.table_type = 'BASE TABLE'
 WHERE col.data_type IN ('varchar','char')
   AND col.character_maximum_length >= 30
   AND col.table_schema = 'mydatabase';

UPDATE:

To include an 'query id' in the resultset of each query, so you can identify which of those queries is returning a row...

SELECT CONCAT('select ',@rn := @rn + 1, ' as q, t.* from `',col.table_schema
             ,'`.`',col.table_name
             ,'` t where `',col.column_name
             ,'` like ''%',RTRIM(@url),'%''') AS q
  FROM (SELECT @rn := 0) r
  JOIN information_schema.columns col 
  JOIN information_schema.tables tbl
    ON tbl.table_schema = col.table_schema
   AND tbl.table_name = col.table_name
   AND tbl.table_type = 'BASE TABLE'
 WHERE col.data_type IN ('varchar','char')
   AND col.character_maximum_length >= 30
   AND col.table_schema = 'mydatabase';



回答2:


Don't forget semicolons:

DECLARE @url VARCHAR(255);
SET @url = '1720';
SELECT  .... ;

Also, @ has special meaning in Mysql (looks like you porting T-SQL script). You don't need to declare it in contrast to local variable (without @ that are used in the body of SP/Function/Trigger)




回答3:


In MySQL you can't use "Declare" without scope, you can only use it in Begin...End block (like: in stored procedures, etc).
You don't need the "Declare..." line, just setting the variable @url is enough for the script to run.



来源:https://stackoverflow.com/questions/12222677/you-have-an-error-in-your-sql-syntax-check-the-manual-that-corresponds-to-your

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!