Question : ' when building string

can you guide how you can accommodate for ' when building strings. for example, in the below, if the job already has a ' in its name, then it fails.
1:
2:
3:
4:
5:
6:
7:
8:
SELECT 'EXEC MSDB.dbo.sp_update_job ' + char(13) +
'@job_name = ' + char(39) + j.[Name] + char(39) + ',' + char(13) +
'@owner_login_name = ' + char(39) + 'sa' + char(39) + char(13) + char(13)
FROM MSDB.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
WHERE l.[name] <> 'sa'
ORDER BY j.[name]

Answer : ' when building string

try the below:
1:
2:
3:
4:
5:
6:
7:
8:
9:
SELECT 
	'EXEC MSDB.dbo.sp_update_job ' + char(13) +
	'@job_name = ' + char(39) + replace(j.[Name], char(39), char(39)+char(39)) + char(39) + ',' + char(13) +
	'@owner_login_name = ' + char(39) + 'sa' + char(39) + char(13) + char(13)
FROM MSDB.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
WHERE l.[name] <> 'sa'
ORDER BY j.[name]
Random Solutions  
 
programming4us programming4us