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 |
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2011-12-02 : 21:50:53
|
| I have a sql agent job that is supposed to do the following:1. Create a new db (sizing_db)2. create the tables in that db (4 tables in total)3. Create a sproc in the master db4. Exec a system stored procedure When i fire off any of the create scripts independently, they all work flawlessly. When i combine them as seperate steps in the agent job, the job consistenly fails. Any idea why? The create script for the agent job is pasted below. Actually, the create scripts are combined as one step and the exec of the system store procedure is set as step 2. The failure happens even if all of the create scripts are their own step:USE [msdb]GO/****** Object: Job [1] Script Date: 12/02/2011 20:52:49 ******/BEGIN TRANSACTIONDECLARE @ReturnCode INTSELECT @ReturnCode = 0/****** Object: JobCategory [[Uncategorized (Local)]]] Script Date: 12/02/2011 20:52:49 ******/IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)BEGINEXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollbackENDDECLARE @jobId BINARY(16)EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'1', @enabled=1, @notify_level_eventlog=0, @notify_level_email=0, @notify_level_netsend=0, @notify_level_page=0, @delete_level=0, @description=N'No description available.', @category_name=N'[Uncategorized (Local)]', @owner_login_name=N'SMARTCOPINC\jhamel', @job_id = @jobId OUTPUTIF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback/****** Object: Step [1] Script Date: 12/02/2011 20:52:49 ******/EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'1', @step_id=1, @cmdexec_success_code=0, @on_success_action=3, @on_success_step_id=0, @on_fail_action=2, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'TSQL', @command=N'USE [master]GO/****** Object: Database [SIZING_DB] Script Date: 12/02/2011 14:32:17 ******/CREATE DATABASE [SIZING_DB] ON PRIMARY( NAME = N''SIZING_DB'', FILENAME = N''C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\SIZING_DB.mdf'', SIZE = 3072KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = N''SIZING_DB_log'', FILENAME = N''C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\SIZING_DB_log.ldf'', SIZE = 1024KB, MAXSIZE = 2048GB, FILEGROWTH = 10%)GOALTER DATABASE [SIZING_DB] SET COMPATIBILITY_LEVEL = 100GOIF (1 = FULLTEXTSERVICEPROPERTY(''IsFullTextInstalled''))BEGIN EXEC [SIZING_DB].[dbo].[sp_fulltext_database] @action = ''enable''ENDGOALTER DATABASE [SIZING_DB] SET ANSI_NULL_DEFAULT OFF GOALTER DATABASE [SIZING_DB] SET ANSI_NULLS OFF GOALTER DATABASE [SIZING_DB] SET ANSI_PADDING OFF GOALTER DATABASE [SIZING_DB] SET ANSI_WARNINGS OFF GOALTER DATABASE [SIZING_DB] SET ARITHABORT OFF GOALTER DATABASE [SIZING_DB] SET AUTO_CLOSE OFF GOALTER DATABASE [SIZING_DB] SET AUTO_CREATE_STATISTICS ON GOALTER DATABASE [SIZING_DB] SET AUTO_SHRINK OFF GOALTER DATABASE [SIZING_DB] SET AUTO_UPDATE_STATISTICS ON GOALTER DATABASE [SIZING_DB] SET CURSOR_CLOSE_ON_COMMIT OFF GOALTER DATABASE [SIZING_DB] SET CURSOR_DEFAULT GLOBAL GOALTER DATABASE [SIZING_DB] SET CONCAT_NULL_YIELDS_NULL OFF GOALTER DATABASE [SIZING_DB] SET NUMERIC_ROUNDABORT OFF GOALTER DATABASE [SIZING_DB] SET QUOTED_IDENTIFIER OFF GOALTER DATABASE [SIZING_DB] SET RECURSIVE_TRIGGERS OFF GOALTER DATABASE [SIZING_DB] SET DISABLE_BROKER GOALTER DATABASE [SIZING_DB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF GOALTER DATABASE [SIZING_DB] SET DATE_CORRELATION_OPTIMIZATION OFF GOALTER DATABASE [SIZING_DB] SET TRUSTWORTHY OFF GOALTER DATABASE [SIZING_DB] SET ALLOW_SNAPSHOT_ISOLATION OFF GOALTER DATABASE [SIZING_DB] SET PARAMETERIZATION SIMPLE GOALTER DATABASE [SIZING_DB] SET READ_COMMITTED_SNAPSHOT OFF GOALTER DATABASE [SIZING_DB] SET HONOR_BROKER_PRIORITY OFF GOALTER DATABASE [SIZING_DB] SET READ_WRITE GOALTER DATABASE [SIZING_DB] SET RECOVERY FULL GOALTER DATABASE [SIZING_DB] SET MULTI_USER GOALTER DATABASE [SIZING_DB] SET PAGE_VERIFY CHECKSUM GOALTER DATABASE [SIZING_DB] SET DB_CHAINING OFF GO', @database_name=N'master', @flags=0IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback/****** Object: Step [2] Script Date: 12/02/2011 20:52:49 ******/EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'2', @step_id=2, @cmdexec_success_code=0, @on_success_action=3, @on_success_step_id=0, @on_fail_action=2, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'TSQL', @command=N'USE [master]GO/****** Object: StoredProcedure [dbo].[sp_GetSTATS] Script Date: 12/02/2011 14:29:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[sp_GetSTATS]WITH EXECUTE AS CALLERASCREATE TABLE #t( [name] NVARCHAR(128), [rows] CHAR(11), reserved VARCHAR(18), DATA VARCHAR(18), index_size VARCHAR(18), unused VARCHAR(18)) INSERT #tEXEC sp_msForEachTable ''EXEC sp_spaceused ''''?'''''' INSERT INTO SIZING_DB.dbo.DBTableStatsSELECT NAME, ROWS, reserved, DATA, index_size, unused, GETDATE() AS datecreatedFROM #tINSERT INTO SIZING_DB.dbo.rowcountsSELECT DB_NAME() AS DBName, GETDATE() AS DateCreated, SUM(CAST([rows] AS INT)) AS [rows]FROM #tDROP TABLE #tINSERT INTO sizing_db.dbo.dbstatsSELECT GETDATE() AS date_and_time, DB_NAME() AS database_name, SUM(total_pages) AS reservedKB, SUM(used_pages) AS usedKB, SUM(total_pages -used_pages) AS unallocatedKBFROM sys.allocation_unitsINSERT INTO sizing_db.dbo.dbstats2SELECT GETDATE() AS date_and_time, DF.name, SIZE, FILEPROPERTY(DF.name, ''spaceused'') AS pages_used, FILEPROPERTY(DF.name, ''spaceused'') * 1.0 / SIZE * 100 AS pct_usedFROM sys.database_files AS DFSELECT dbstats.uniquekey, dbstats.date_and_time, dbstats.database_name, dbstats.reservedKB, dbstats.usedKB, dbstats.unallocatedKB, dbstats2.size, dbstats2.pages_used, dbstats2.pct_usedFROM sizing_db.dbo.dbstats dbstats INNER JOIN sizing_db.dbo.dbstats2 dbstats2 ON (dbstats.uniquekey = dbstats2.uniquekey)SELECT *FROM sizing_db.dbo.DBTableStats dsGO', @database_name=N'master', @flags=0IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback/****** Object: Step [3] Script Date: 12/02/2011 20:52:49 ******/EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'3', @step_id=3, @cmdexec_success_code=0, @on_success_action=1, @on_success_step_id=0, @on_fail_action=2, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'TSQL', @command=N'EXEC sp_msForEachdb sp_getstats', @database_name=N'master', @flags=0IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollbackEXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollbackEXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollbackCOMMIT TRANSACTIONGOTO EndSaveQuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTIONEndSave:GOBangers and Mash are neither "Bangers" nor "Mash". |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-03 : 00:14:12
|
| can you tell us what error you're getting when they're run as steps?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2011-12-03 : 08:37:51
|
| I was just getting a syntax error very early in the script. I've since moved past that by adding each step incrementally and adding IF EXISTS statements. Now i'm at the next hurdle. See my more recent post. I'm in the last two steps of this, then i'll generate the create script for the agent job and hope that it's syntax holds together.Bangers and Mash are neither "Bangers" nor "Mash". |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2011-12-05 : 09:15:07
|
| Just for the record, i discovered that the problem lies within SQLAssistant, which i typically use to format my code (to give it a cleaner visual flow) at the end of a project. When i combine the steps in this agent job and leave them just as they were upon cutting and pasting the code, all is well. If it then apply SQLAssistant's "Format Code" command, it wreaks havoc with the syntax. I'll choose working code over pretty code any day of the week.Bangers and Mash are neither "Bangers" nor "Mash". |
 |
|
|
|
|
|
|
|