error in SQL Statement Incorrect syntax near '%'.

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
bibi92Asked:
Who is Participating?
 
Anthony PerkinsConnect With a Mentor Commented:
It should be as simple as the code below (although it would be far more efficient and maintainable using sp_executesql).
set @sql3 = 'exec ['+@SrvAbonne + '].['+ @Dbcible + '].[dbo].sp_executesql 
 N''INSERT INTO maj_replsp  
SELECT m.definition, 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)

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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'
0
 
bibi92Author 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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
malikirfan28Commented:
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
0
 
bibi92Author 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
0
 
malikirfan28Commented:
dont forget to pass @Dossier as parameter. You can pass parameter as
exec sp_executesql @sql, N'@Dossier varchar'
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
bibi92Author Commented:
Thanks
but this is not execute like a parameter AND o.name LIKE %sp_MSdel_@Dossier% OR  o.name LIKE %sp_MSdel_@Dossier%
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Because it needs to be
AND o.name LIKE ''%sp_MSdel_' + @Dossier + '%'' OR  o.name LIKE ''%sp_MSdel_' + @Dossier + '%''
0
 
bibi92Author 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
0
 
Anthony PerkinsCommented:
What you do not seem to understand is that GO is not a T-SQL command.  You cannot use it that way.
0
 
Anthony PerkinsCommented:
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.
0
 
bibi92Author Commented:
Thanks bibi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.