Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sindhu.meka
Starting Member
4 Posts |
Posted - 2007-08-01 : 10:33:53
|
| Hi,I'm trying to get the version of SQL Server.I have create and execute jobs based on the version because in SQLExpress SQLJob Agent is supported whose version is 9.0.So If version is 8.0 then only i'm creating the jobs.But I'm getting errors while executing the following query.Msg 102, Level 15, State 1, Line 4Incorrect syntax near 'dt_CreateSQLAgentJob'.Msg 102, Level 15, State 1, Procedure dt_CreateSQLAgentJob, Line 78Incorrect syntax near 'END'IF(SUBSTRING(LTRIM(RIGHT(LEFT(@@VERSION,38),10)),0,4) = '8.0')BEGINif ( exists ( select * from sysobjects where name = 'dt_CreateSQLAgentJob' ) ) DROP PROCEDURE dt_CreateSQLAgentJobGO --9.99CREATE PROCEDURE dt_CreateSQLAgentJob @DatabaseName varchar(255), @JobName varchar(255), @Interval int, @Script varchar(3000)with ENCRYPTIONAS BEGIN TRANSACTION DECLARE @JobID uniqueidentifier DECLARE @ReturnCode INT DECLARE @RollBack BIT SET @JobID = NULL SET @ReturnCode = 0 SET @RollBack = 0 IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'Database Maintenance') < 1 BEGIN EXECUTE msdb.dbo.sp_add_category @name = N'Database Maintenance' END -- Delete the job with the same name, don't care if it exists -- Delete the [local] job EXECUTE msdb.dbo.sp_delete_job @job_name = @JobName -- Add the job --EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT, @job_name = @JobName, @owner_login_name = N'', @description = N'No description available.', @category_name = N'Database Maintenance', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT, @job_name = @JobName,@description = N'No description available.', @category_name = N'Database Maintenance', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN -- Add the job steps -- EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'execute', @command = @Script, @database_name = @DatabaseName, @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'execute', @command = @Script, @database_name = @DatabaseName, @server = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN -- Add the job schedules EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N'execute', @enabled = 1, @freq_type = 4, @active_start_date = 20021018, @active_start_time = 0, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = @Interval, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN -- Add the Target Servers EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)' IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END END END END END IF @RollBack = 1 ROLLBACK TRANSACTION ELSE COMMIT TRANSACTIONENDGO --9.99Can any body please helpme.Thanks and Regards,Meka |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-01 : 10:35:54
|
SQL Server Express does not suppert SQL Agent.You have to pay for a version that does. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
sindhu.meka
Starting Member
4 Posts |
Posted - 2007-08-01 : 10:47:02
|
My application supports both SQL Server8.0 and SQL Express.I want to create and execute this only if it is a SQL Server 8.0.That's why i'm cheking for the version.quote: Originally posted by Peso SQL Server Express does not suppert SQL Agent.You have to pay for a version that does. E 12°55'05.25"N 56°04'39.16"
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-01 : 11:10:56
|
[code]SELECT @@VERSION, SERVERPROPERTY('Edition'), SERVERPROPERTY('EngineEdition'), SERVERPROPERTY('ProductVersion'), SERVERPROPERTY('ProductLevel')[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
sindhu.meka
Starting Member
4 Posts |
Posted - 2007-08-01 : 11:16:57
|
I'm still getting following errorsMsg 102, Level 15, State 1, Line 4Incorrect syntax near 'dt_CreateSQLAgentJob'.Msg 102, Level 15, State 1, Procedure dt_CreateSQLAgentJob, Line 78Incorrect syntax near 'END'quote: Originally posted by Peso
SELECT @@VERSION, SERVERPROPERTY('Edition'), SERVERPROPERTY('EngineEdition'), SERVERPROPERTY('ProductVersion'), SERVERPROPERTY('ProductLevel') E 12°55'05.25"N 56°04'39.16"
|
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-08-01 : 11:21:31
|
| You can't have a GO in the middle of yourIF(SUBSTRING(LTRIM(RIGHT(LEFT(@@VERSION,38),10)),0,4) = '8.0')BEGIN..ENDKristen |
 |
|
|
sindhu.meka
Starting Member
4 Posts |
Posted - 2007-08-01 : 11:27:46
|
How can I execute my query if I don't specify GO.Any way I commented GOThen it is sowing error in the next lineMsg 156, Level 15, State 1, Line 6Incorrect syntax near the keyword 'PROCEDURE'.quote: Originally posted by Kristen You can't have a GO in the middle of yourIF(SUBSTRING(LTRIM(RIGHT(LEFT(@@VERSION,38),10)),0,4) = '8.0')BEGIN..ENDKristen
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-01 : 11:29:26
|
[code]IF CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR) LIKE '8.%'AND EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME = 'DT_CREATESQLAGENTJOB') BEGIN EXEC ('DROP PROCEDURE dt_CreateSQLAgentJob') DECLARE @SQL VARCHAR(8000) SET @SQL = ' CREATE PROCEDURE dt_CreateSQLAgentJob @DatabaseName varchar(255), @JobName varchar(255), @Interval int, @Script varchar(3000) with ENCRYPTION AS BEGIN TRANSACTION DECLARE @JobID uniqueidentifier DECLARE @ReturnCode INT DECLARE @RollBack BIT SET @JobID = NULL SET @ReturnCode = 0 SET @RollBack = 0 IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N''Database Maintenance'') < 1 BEGIN EXECUTE msdb.dbo.sp_add_category @name = N''Database Maintenance'' END -- Delete the job with the same name, don''t care if it exists -- Delete the [local] job EXECUTE msdb.dbo.sp_delete_job @job_name = @JobName -- Add the job --EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT, @job_name = @JobName, @owner_login_name = N'''', @description = N''''No description available.'', @category_name = N''Database Maintenance'', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT, @job_name = @JobName,@description = N''No description available.'', @category_name = N''Database Maintenance'', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN -- Add the job steps -- EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N''execute'', @command = @Script, @database_name = @DatabaseName, @server = N'''', @database_user_name = N'''', @subsystem = N''TSQL'', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N''execute'', @command = @Script, @database_name = @DatabaseName, @server = N'''', @subsystem = N''TSQL'', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN -- Add the job schedules EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N''execute'', @enabled = 1, @freq_type = 4, @active_start_date = 20021018, @active_start_time = 0, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = @Interval, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959 IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END ELSE BEGIN -- Add the Target Servers EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N''(local)'' IF (@@ERROR <> 0) OR (@ReturnCode <> 0) BEGIN SET @RollBack = 1 END END END END END IF @RollBack = 1 ROLLBACK TRANSACTION ELSE COMMIT TRANSACTION END GO --9.99 ' EXEC (@SQL) END[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-01 : 11:37:58
|
There is an END missing in your code.You have so many nested IF's, it is hard to see.Try to rewrite your code to NOT nest IF's. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-01 : 11:41:42
|
Nicer, non-IF-nesting code hereIF CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR) LIKE '8.%'AND EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME = 'DT_CREATESQLAGENTJOB') BEGIN EXEC ('DROP PROCEDURE dt_CreateSQLAgentJob') DECLARE @SQL VARCHAR(8000) SET @SQL = ' CREATE PROCEDURE dt_CreateSQLAgentJob ( @DatabaseName varchar(255), @JobName varchar(255), @Interval int, @Script varchar(3000) ) WITH ENCRYPTION AS SET NOCOUNT ON BEGIN TRAN DECLARE @JobID uniqueidentifier, @ReturnCode INT, @RollBack BIT SELECT @ReturnCode = 0, @RollBack = 0 IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N''Database Maintenance'') < 1 EXEC msdb.dbo.sp_add_category @name = N''Database Maintenance'' -- Delete the job with the same name, don''t care if it exists EXEC msdb.dbo.sp_delete_job @job_name = @JobName -- Add the job EXEC @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT, @job_name = @JobName,@description = N''No description available.'', @category_name = N''Database Maintenance'', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 IF @@ERROR <> 0 OR @ReturnCode <> 0 BEGIN ROLLBACK TRAN RETURN END -- Add the job steps EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N''execute'', @command = @Script, @database_name = @DatabaseName, @server = N'''', @subsystem = N''TSQL'', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 IF @@ERROR <> 0 OR @ReturnCode <> 0 BEGIN ROLLBACK TRAN RETURN END EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1 IF @@ERROR <> 0 OR @ReturnCode <> 0 BEGIN ROLLBACK TRAN RETURN END -- Add the job schedules EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N''execute'', @enabled = 1, @freq_type = 4, @active_start_date = 20021018, @active_start_time = 0, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = @Interval, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959 IF @@ERROR <> 0 OR @ReturnCode <> 0 BEGIN ROLLBACK TRAN RETURN END -- Add the Target Servers EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N''(local)'' IF @@ERROR <> 0 OR @ReturnCode <> 0 ROLLBACK TRAN ELSE COMMIT TRANSACTION' EXEC (@SQL) END E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|