views:

90

answers:

2

Hi i am using the following dynamic sql:

declare @cmd nvarchar(4000)

set @cmd= 

'select ''exec msdb.dbo.sp_update_job @job_id=''''''
+ convert(nvarchar(255), job_id)+ '''''',

@owner_login_name=''''sa'''';''
from msdb..sysjobs'


exec sp_executesql @cmd

but all it is doing is printing as exec .......

I want to execute the results not just print it.

What should i do?

Regards

Manjot

+3  A: 

Remove the 'Select' ! (and also remove some escaped single quotes) In other words...

set @cmd=
   'exec msdb.dbo.sp_update_job @job_id=''' 
   + convert(nvarchar(255), job_id)  
   + ''', @owner_login_name=''sa'';'

Note: not quite sure where this last thing fits (probably at the end of the sp_update_job statement.

   -- from msdb..sysjobs'

Essentially what was going on, with the SELECT statement was that the dynamic SQL being executed was a select statement which happen to select a string dynamically crafted. SQL would then print that string, just as if the SELECT statement had been, say:

select 'Once upon a time, a very poor lumberjack..."

(Whatever is in the string after select doesn't affect SQL in any way)

Now, as is more obvious with Remus' nice formatting of the question, the dyanamic SQL statement itself seems to be wrong, but at least SQL will try to execute it, now...

mjv
But i need to get data from msdb..sysjobs so i need select statement. How would you write my code?
Manjot
I don't think the full purpose of the command Manjot posted is understood. He's not trying to execute a single dynamic SQL statement, he is trying to execute the stored procedure for every row in the result of the select. If you look at your first code snippet, can you explain where the job_id mentioned in the convert line comes from?
Aaron Bertrand
@Manjot I understand. I was commenting on why the exec didn't work, but seeing your need you may not need any dynamic code at all. ('ll be right back...)
mjv
Thanks.And.... I am a "she" not "he". :-)
Manjot
@Manjot. I returned too late, apparently Aaron provided you with a way of doing what your project effectively required. Good thing! Collective apologies for assuming you were a "he", I'm often careful to use "he/she" when in doubt, but I too sometimes forget as well, an easy thing to do, as your gender appears to be generally under-represented on SO. Welcome and bring more of your girl friends ;-)
mjv
Sorry Manjot, like mjv, bad assumption on my part.
Aaron Bertrand
No problem.:-)Normally i dont correct people for "he" on an online forum but people on this website sound more friendly, and not strictly formal so, I thought lets speak out.Thanks again.Really appreciate it.
Manjot
+3  A: 

How about:

DECLARE @sql NVARCHAR(MAX);

SELECT @sql = COALESCE(@sql, '') + '
    EXEC msdb.dbo.sp_update_job @job_id = ''' 
    + CONVERT(NVARCHAR(255), job_id) 
    + ''', @owner_login_name = ''sa'';'
FROM msdb.dbo.sysjobs;

EXEC sp_sqlexec @sql;

NOTE that sp_sqlexec is undocumented and unsupported. To do this in a supported way, why not just create a cursor, it is much easier to follow and debug (no sea of red and no "invisible" SQL):

DECLARE @j UNIQUEIDENTIFIER;

DECLARE c CURSOR FOR
    SELECT job_id FROM msdb.dbo.sysjobs;

OPEN c;

FETCH NEXT FROM c INTO @j;

WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC msdb.dbo.sp_update_job
     @job_id = @j,
     @owner_login_name = 'sa';

    FETCH NEXT FROM c INTO @j;
END

DEALLOCATE c;

Or ever easier, since msdb.dbo.sysjobs is not technically a system table, and you can edit it directly:

UPDATE msdb.dbo.sysjobs 
    SET owner_sid = SUSER_SID('sa');
Aaron Bertrand
Thanks.The last bit helped me.By the way, what is COALESCE?
Manjot
http://msdn.microsoft.com/en-us/library/ms190349.aspx
Aaron Bertrand
Excellent example of where a cursor can be a good thing.
HLGEM
I thought that cursors are always bad
Manjot
No, not true. In most cases, a set-based solution is better. But there are cases where cursors are preferred, such as maintenance tasks like this, cases where you need to execute a stored procedure per row, and even some complex update/rolling sum operations.
Aaron Bertrand