Solved

error on sql dynamic with uniqueidentifier

Posted on 2011-02-13
8
432 Views
Last Modified: 2012-05-11
Hello,

declare @sql5 varchar(max)

set @sql5 = 'DECLARE @job_id uniqueidentifier

SELECT
            @job_id = job_id

FROM
            '+ @abonne + '.msdb.dbo.sysjobs_view
WHERE
            name like N''' + @publication + '''
print @job_id
IF @job_id IS NOT NULL BEGIN
      EXEC ('+ @abonne +'.msdb.dbo.sp_delete_job @job_id = @job_id)
END

'
print @sql5
execute @sql5



The name 'DECLARE @job_id uniqueidentifier

SELECT
            @job_id = job_id

FROM
            [serverdest].msdb.dbo.sysjobs_view
WHERE
            name like N'%jobname%'
print @job_id
IF @job_id IS NOT NULL BEGIN
      EXEC ([serverdest].msdb.dbo.sp_delete_job @job_id = @job_id)
END

' is not a valid identifier.

How can I resolve this problem?

Thanks

Bibi
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34883973
Please try the following:

declare @sql5 varchar(max)

set @sql5 = 'DECLARE @job_id uniqueidentifier

SELECT
            @job_id = job_id

FROM
            '+ @abonne + '.msdb.dbo.sysjobs_view
WHERE
            name like N''' + @publication + '''
print @job_id
IF @job_id IS NOT NULL BEGIN
      EXEC ('''+ @abonne +'.msdb.dbo.sp_delete_job @job_id = '' + @job_id )
END

'
print @sql5
execute (@sql5)

Open in new window

0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34883981
Hi,

Change execute @sql5

for

execute (@sql5)
0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34883984
Ah, too slow :)
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:bibi92
ID: 34884344
Thanks but I have this error :
Msg 168, Level 15, State 1, Line 1
The floating point value '2886E768' is out of the range of computer representation (8 bytes).
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
Regards
bibi
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 34884567
Please try the following:

set @sql5 = 'DECLARE @job_id uniqueidentifier

SELECT
            @job_id = job_id

FROM
            '+ @abonne + '.msdb.dbo.sysjobs_view
WHERE
            name like N''' + @publication + '''
print @job_id
IF @job_id IS NOT NULL BEGIN
      EXEC ('''+ @abonne +'.msdb.dbo.sp_delete_job @job_id = '''''' + @job_id + '''''''' )
END

'
print @sql5
execute (@sql5)

Open in new window

0
 

Author Comment

by:bibi92
ID: 34884587
Doesn't work, the print @sql5 :
print @job_id
IF @job_id IS NOT NULL BEGIN
      EXEC ('server].msdb.dbo.sp_delete_job @job_id = ''' + @job_id + '''' )
END
Thanks

Regards
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34887980
I think the solution was reached based on information from the posts. Specially the "execute (@sql5)" tip. So, I believe points should be given.
0
 

Author Closing Comment

by:bibi92
ID: 34889791
Sorry Thanks bibi
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question