We help IT Professionals succeed at work.

error in SQL Statement Incorrect syntax near '%'.

bibi92
bibi92 asked
on
Medium Priority
651 Views
Last Modified: 2012-05-11
Hello,

I try to execute :

DECLARE @SrvAbonne varchar (50)
DECLARE @DbCible varchar (50)
DECLARE @Dossier varchar (50)

exec ['+@SrvAbonne + '].['+ @Dbcible + '].[dbo].sp_executesql N'
INSERT INTO sage_maj_replsp  
SELECT m.definition + CHAR(13) + ''GO'' + CHAR(13), o.name
FROM   sys.objects o INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id INNER JOIN sys.schemas s
ON s.schema_id = o.schema_id
WHERE  o.type_desc IN ( ''SQL_STORED_PROCEDURE'' )
AND o.name LIKE '%sp_MSdel_' + @Dossier + '%'  
OR  o.name LIKE '%sp_MSdel_' + @Dossier + '%'
ORDER  BY o.name DESC
'

Incorrect syntax near '%'.

Thanks

bibi
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
I understand what you want to achieve with the first part, but what is the stuff regarding CHAR(13) and ''GO''? Anyway, you are missing several doubled single quotes, the first should be after the N of
  sp_executesql N'

Author

Commented:
what is the stuff regarding CHAR(13) and ''GO'' is for execute the ''SQL_STORED_PROCEDURE''

I have quote after sp_executesql N'

Thanks
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Sorry, have been wrong about the N' part. I assume the following should work:
DECLARE @SrvAbonne varchar (50)
DECLARE @DbCible varchar (50)
DECLARE @Dossier varchar (50)

exec @SrvAbonne.@Dbcible.dbo.sp_executesql N'
INSERT INTO sage_maj_replsp  
SELECT m.definition + CHAR(13) + ''GO'' + CHAR(13), o.name 
FROM   sys.objects o INNER JOIN sys.sql_modules m 
ON o.object_id = m.object_id INNER JOIN sys.schemas s 
ON s.schema_id = o.schema_id 
WHERE  o.type_desc IN ( ''SQL_STORED_PROCEDURE'' ) 
AND o.name LIKE ''%sp_MSdel_' + @Dossier + '%''  
OR  o.name LIKE ''%sp_MSdel_' + @Dossier + '%''
ORDER  BY o.name DESC 
'

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Ah, you retrieve the definition of the stored procedure, and want to have the "GO" appended. That is ok. But that code does not execute that procedure, of course.
Here your syntax error removed. But still I could not understand your SELECT clause.

DECLARE @SrvAbonne varchar (50)
DECLARE @DbCible varchar (50)
DECLARE @Dossier varchar (50)
declare @sql nvarchar(MAX)
set @sql = N'INSERT INTO sage_maj_replsp  
SELECT m.definition + CHAR(13) + ''GO'' + CHAR(13), o.name
FROM   sys.objects o INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id INNER JOIN sys.schemas s
ON s.schema_id = o.schema_id
WHERE  o.type_desc IN ( ''SQL_STORED_PROCEDURE'' )
AND o.name LIKE %sp_MSdel_@Dossier% OR  o.name LIKE %sp_MSdel_@Dossier% ORDER  BY o.name DESC'


exec [@SrvAbonne].[@Dbcible].[dbo].sp_executesql @sql

Author

Commented:
exec @SrvAbonne.@Dbcible doesn't work because this is an named instances SRV\TEST

Msg 102, Level 15, State 1, Procedure sage_maj_proc_X3, Line 93
Incorrect syntax near '+'.

But that code does not execute that procedure, of course exactly I have to do this after with execute statement

Thanks
dont forget to pass @Dossier as parameter. You can pass parameter as
exec sp_executesql @sql, N'@Dossier varchar'
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
malikirfan,

That still needs some correction - quotes for the the like arguments, and constructing a string from the expression.


bibi92,

That doesn't make sense to me. Maybe you should phrase what you really want to achieve, so we can present a real solution.

Author

Commented:
Thanks
but this is not execute like a parameter AND o.name LIKE %sp_MSdel_@Dossier% OR  o.name LIKE %sp_MSdel_@Dossier%
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Because it needs to be
AND o.name LIKE ''%sp_MSdel_' + @Dossier + '%'' OR  o.name LIKE ''%sp_MSdel_' + @Dossier + '%''

Author

Commented:
Thanks Qlemo, but I have this error :
declare @sql3 varchar(max)
set @sql3 = 'exec ['+@SrvAbonne + '].['+ @Dbcible + '].[dbo].sp_executesql
 N''INSERT INTO maj_replsp  
SELECT m.definition + CHAR(13) + ''GO'' + CHAR(13), o.name
FROM   sys.objects o INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id INNER JOIN sys.schemas s
ON s.schema_id = o.schema_id
WHERE  o.type_desc IN ( ''SQL_STORED_PROCEDURE'' )
AND o.name LIKE ''%sp_MSupd_'+@Dossier+'%'' OR  o.name LIKE ''%sp_MSdel_' + @Dossier+'%'' ORDER  BY o.name DESC''
'
print(@sql3)
execute (@sql3)

Msg 50000, Level 15, State 1, Procedure maj_proc_Dossier, Line 126
Incorrect syntax near 'GO'. #102
CERTIFIED EXPERT
Top Expert 2012

Commented:
What you do not seem to understand is that GO is not a T-SQL command.  You cannot use it that way.
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2012

Commented:
And for those who think otherwise regarding the GO command, here is a direct quote from SQL Server BOL:
GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.

A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.

Author

Commented:
Thanks bibi
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.