%3CLINGO-SUB%20id%3D%22lingo-sub-1199555%22%20slang%3D%22en-US%22%3ERE%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1199555%22%20slang%3D%22en-US%22%3Etest%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1199556%22%20slang%3D%22en-US%22%3ERE%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1199556%22%20slang%3D%22en-US%22%3Ereply%20written%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1199557%22%20slang%3D%22en-US%22%3ERE%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1199557%22%20slang%3D%22en-US%22%3Ewriting%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1199605%22%20slang%3D%22en-US%22%3ERE%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1199605%22%20slang%3D%22en-US%22%3EAndroid%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1199608%22%20slang%3D%22en-US%22%3ERE%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1199608%22%20slang%3D%22en-US%22%3Etttest%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1202972%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1202972%22%20slang%3D%22en-US%22%3E%3CP%3EExcellent%20and%20much%20needed%20feature%20and%20guide%2C%20thank%20you!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%20you%20have%20a%20small%20mistake%20in%20this%20part%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EEXEC%20jobs.sp_add_target_group_member%0A'ServerGroup1'%2C%3C%2FPRE%3E%3CP%3EI'm%20guessing%20%22ServerGroup1%22%20is%20supposed%20to%20be%20%22DemoGroup%22%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1204680%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1204680%22%20slang%3D%22en-US%22%3E%3CP%3EWhere%20do%20you%20define%20the%20database%20that%20the%20script%20should%20run%3F%20I%20saw%20the%20servername%20being%20set%20in%26nbsp%3B%3CSPAN%3Ejobs.sp_add_target_group_member%20but%20not%20the%20database%20where%20the%20update%20stats%20will%20run.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1180096%22%20slang%3D%22en-US%22%3ECreating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1180096%22%20slang%3D%22en-US%22%3E%3CTABLE%20style%3D%22background-color%3A%20transparent%3B%20border-collapse%3A%20collapse%3B%20border-spacing%3A%200px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20width%3A%20100%25%3B%20word-spacing%3A%200px%3B%20border-style%3A%20double%3B%22%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%20style%3D%22box-sizing%3A%20border-box%3B%22%3E%0A%3CTR%20style%3D%22box-sizing%3A%20border-box%3B%22%3E%0A%3CTD%20width%3D%22100%25%22%20style%3D%22border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20box-sizing%3A%20border-box%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-weight%3A%20300%3B%20line-height%3A%201.7142%3B%20min-width%3A%2040px%3B%20word-break%3A%20normal%3B%20padding%3A%201px%205px%201px%205px%3B%20border%3A%201px%20solid%20%23c4c4c4%3B%22%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23ff0000%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23ff0000%22%3EElastic%20Jobs%20are%20currently%20in%20public%20preview.%26nbsp%3B%20The%20feature%20is%20ready%20and%20we%20are%20preparing%20to%20make%20it%20officially%20generally%20available.%26nbsp%3B%20No%20extra%20steps%20are%20required%20to%20get%20started.%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20part%204%20of%20my%20blog%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Felastic-jobs-in-azure-sql-database-what-and-why%2Fba-p%2F1177902%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eseries%3C%2FA%3E%20on%20Elastic%20Jobs%20for%20Azure%20SQL%20Database%2C%20I%20will%20show%20how%20to%20create%20the%20Elastic%20Job%20Agent%20and%20associated%20credentials%20and%20jobs%20using%20T-SQL.%26nbsp%3B%20For%20more%20extensive%20documentation%20on%20this%20subject%2C%20see%20here.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23333333%22%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E%3CFONT%20size%3D%225%22%20style%3D%22box-sizing%3A%20border-box%3B%22%3ECreating%20the%20Job%20Agent%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThere%20is%20no%20way%20to%20create%20the%20Elastic%20Job%20Agent%20in%20T-SQL.%20I%20have%20already%20shown%20how%20to%20do%20this%20in%20PowerShell.%20To%20do%20this%20in%20the%20Azure%20Portal%2C%20go%20to%20Home%2C%20click%20the%20box%20that%20says%20%E2%80%9C%2B%20Create%20a%20Resource%E2%80%9D%2C%20then%20search%20in%20the%20box%20for%20Elastic%20Job%20Agent.%20Select%20that%2C%20and%20then%20follow%20the%20steps%20in%20the%20portal%20to%20create%20the%20agent.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Kate_Smith_0-1582046909140.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F171784i6306E2E27EC301B2%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Kate_Smith_0-1582046909140.png%22%20alt%3D%22Kate_Smith_0-1582046909140.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Kate_Smith_1-1582046909144.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F171785i1AE350AF81983ECB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Kate_Smith_1-1582046909144.png%22%20alt%3D%22Kate_Smith_1-1582046909144.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23333333%22%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E%3CFONT%20size%3D%225%22%20style%3D%22box-sizing%3A%20border-box%3B%22%3ECreating%20the%20Credentials%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIndependent%20of%20creating%20the%20Elastic%20Job%20Agent%20(meaning%2C%20this%20step%20and%20the%20prior%20one%20can%20be%20done%20in%20any%20order)%2C%20you%20need%20to%20connect%20to%20the%20database%20you%20have%20hosting%20the%20Elastic%20Job%20Agent%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%20and%20create%20the%20refresh%20and%20job%20credentials%20there.%20Again%2C%20I%20assume%20that%20these%20credentials%20have%20already%20been%20created%20on%20the%20target%20servers%20and%20databases%20and%20show%20only%20the%20portions%20specific%20to%20the%20elastic%20job%20here.%20For%20a%20refresher%20on%20credentials%2C%20refer%20to%20this%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Ffundamental-concepts-for-elastic-jobs-in-azure-sql-database%2Fba-p%2F1177939%22%20target%3D%22_self%22%3Eprevious%20post%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EExecuting%20in%20the%20database%20hosting%20the%20Elastic%20Job%20Agent%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECREATE%20MASTER%20KEY%20ENCRYPTION%20BY%20PASSWORD%20%3D%20'password'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECREATE%20DATABASE%20SCOPED%20CREDENTIAL%20refreshcredential%3C%2FP%3E%0A%3CP%3EWITH%20IDENTITY%20%3D%20'refreshcredential'%2C%3C%2FP%3E%0A%3CP%3ESECRET%20%3D%20'password1'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECREATE%20DATABASE%20SCOPED%20CREDENTIAL%20jobcredential%3C%2FP%3E%0A%3CP%3EWITH%20IDENTITY%20%3D%20'jobcredential'%2C%3C%2FP%3E%0A%3CP%3ESECRET%20%3D%20'password2'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20query%20the%20credentials%20to%20make%20sure%20they%20are%20created%20via%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESELECT%20*%20FROM%20sys.database_scoped_credentials%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23333333%22%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E%3CFONT%20size%3D%225%22%20style%3D%22box-sizing%3A%20border-box%3B%22%3ECreating%20the%20Target%20Group%26nbsp%3B%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThe%20final%20step%20before%20creating%20the%20job%20is%20to%20specify%20the%20group%20that%20is%20the%20target%20of%20the%20job.%20Remeber%2C%20if%20I%20specify%20a%20target%20server%2C%20the%20job%20will%20target%20every%20database%20on%20that%20server%20automatically.%26nbsp%3B%20If%20you%20wish%20to%20exclude%20a%20database%2C%20please%20see%20the%20documentation%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Felastic-jobs-tsql%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere.%3C%2FA%3E%26nbsp%3B%20Again%2C%20I%20follow%20through%20creating%20the%20demo%20group%20just%20like%20I%20did%20with%20PowerShell%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEXEC%20jobs.sp_add_target_group%20'DemoGroup'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E--%20Add%20a%20server%20target%20member%3C%2FP%3E%0A%3CP%3EEXEC%20jobs.sp_add_target_group_member%3C%2FP%3E%0A%3CP%3E'DemoGroup'%2C%3C%2FP%3E%0A%3CP%3E%40%20target_type%20%3D%20'SqlServer'%2C%3C%2FP%3E%0A%3CP%3E%40refresh_credential_name%3D'refreshcredential'%2C%20--credential%20required%20to%20refresh%20the%20databases%20in%20server%3C%2FP%3E%0A%3CP%3E%40server_name%3D'elasticjobstargetserver.database.windows.net'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E--View%20the%20recently%20created%20target%20group%20and%20target%20group%20members%3C%2FP%3E%0A%3CP%3ESELECT%20*%20FROM%20jobs.target_groups%20WHERE%20target_group_name%3D'DemoGroup'%3B%3C%2FP%3E%0A%3CP%3ESELECT%20*%20FROM%20jobs.target_group_members%20WHERE%20target_group_name%3D'DemoGroup'%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20color%3D%22%23333333%22%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E%3CFONT%20size%3D%225%22%20style%3D%22box-sizing%3A%20border-box%3B%22%3ECreating%20and%20Defining%20the%20Job%26nbsp%3B%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ENow%20fully%20equipped%20with%20an%20Elastic%20Job%20Agent%2C%20the%20appropriate%20credentials%2C%20and%20a%20defined%20target%20group%2C%20we%20can%20now%20create%20and%20define%20the%20Elastic%20Job%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E--Add%20job%20for%20create%20table%3C%2FP%3E%0A%3CP%3EEXEC%20jobs.sp_add_job%20%40job_name%3D'demo123'%2C%20%40description%3D'Demo%20job'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E--%20Add%20job%20step%20for%20update%20statistics%3C%2FP%3E%0A%3CP%3EEXEC%20jobs.sp_add_jobstep%20%40job_name%3D'demo123'%2C%3C%2FP%3E%0A%3CP%3E%40command%3D%20'IF%3C%2FP%3E%0A%3CP%3E(CASE%3C%2FP%3E%0A%3CP%3EWHEN%20(SELECT%20STATS_DATE(%5Bobject_id%5D%2C%20%5Bstats_id%5D)%20FROM%20sys.stats%20WHERE%20name%20%3D%20'tStats')%20%26lt%3B%20DATEADD(day%2C-1%2C%20sysdatetime())%3C%2FP%3E%0A%3CP%3ETHEN%201%3C%2FP%3E%0A%3CP%3EELSE%200%3C%2FP%3E%0A%3CP%3EEND)%20%3D%201%3C%2FP%3E%0A%3CP%3EUPDATE%20STATISTICS%20T(tStats)%3B'%2C%3C%2FP%3E%0A%3CP%3E%40credential_name%3D'jobcredential'%2C%3C%2FP%3E%0A%3CP%3E%40%20target_group_name%3D'DemoGroup'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20this%20point%2C%20the%20job%20is%20created%20and%20ready%20to%20run!%20You%20can%20verify%20this%20by%20querying%20the%20database%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESELECT%20*%20FROM%20jobs.jobs%20WHERE%20job_name%20%3D%20%E2%80%98demo123%E2%80%99%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnother%20way%20to%20verify%20the%20job%20is%20to%20examine%20it%20through%20the%20Azure%20Portal.%20To%20do%20this%2C%20find%20all%20of%20your%20Elastic%20Job%20Agents%20(you%20can%20search%20for%20the%20Elastic%20Job%20Agents%20service%20in%20the%20search%20bar%20at%20the%20top%20of%20the%20screen)%2C%20and%20then%20select%20your%20agent.%20From%20there%2C%20you%20can%20drill%20down%20into%20the%20jobs%2C%20target%20groups%2C%20and%20even%20browse%20the%20credentials.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22MonitoringElasticJobAgentInPortal.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F171798i69CE3958C9EC8D53%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22MonitoringElasticJobAgentInPortal.png%22%20alt%3D%22MonitoringElasticJobAgentInPortal.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%2C%20I%20will%20show%20the%20basics%20of%20running%2C%20scheduling%2C%20and%20verifying%20the%20schedule%20of%20an%20Elastic%20Job%20using%20both%20PowerShell%20and%20T-SQL.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20blog%20is%20part%20of%20a%20series%20about%20Elastic%20Jobs%20on%20Azure%20SQL%20Database.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Felastic-jobs-in-azure-sql-database-what-and-why%2Fba-p%2F1177902%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EElastic%20Jobs%20in%20Azure%20SQL%20Database%20%E2%80%93%20What%20and%20Why%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Ffundamental-concepts-for-elastic-jobs-in-azure-sql-database%2Fba-p%2F1177939%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EFundamental%20Concepts%20for%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Fcreating-an-elastic-jobs-agent-credentials-and-jobs-for-azure%2Fba-p%2F1179929%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3ECreating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20in%20PowerShell%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql-database%2Fcreating-an-elastic-jobs-agent-credentials-and-jobs-for-azure%2Fba-p%2F1180096%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3ECreating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3ERunning%2C%20Scheduling%20and%20Monitoring%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FLI%3E%0A%3CLI%3ET%3CSPAN%3Eroubleshooting%20Common%20issues%20with%20Elastic%20Jobs%20in%20Azure%20SQL%20Database%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1180096%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20part%204%20of%20my%20blog%20series%20on%20Elastic%20Jobs%20for%20Azure%20SQL%20Database%2C%20I%20will%20show%20how%20to%20create%20the%20Elastic%20Job%20Agent%20and%20associated%20credentials%20and%20jobs%20using%20T-SQL.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1204992%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20an%20Elastic%20Jobs%20Agent%2C%20Credentials%2C%20and%20Jobs%20for%20Azure%20SQL%20Database%20using%20T-SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1204992%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F572404%22%20target%3D%22_blank%22%3E%40leopasta%3C%2FA%3E%20The%20elastic%20jobs%20will%20target%20every%20database%20on%20the%20server.%26nbsp%3B%20You%20can%20exclude%20databases%20if%20needed.%20%26nbsp%3B%20Complete%20documentation%20here%3A%20%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Felastic-jobs-tsql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Felastic-jobs-tsql%3C%2FA%3E%3C%2FFONT%3E%20%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft
Elastic Jobs are currently in public preview.  The feature is ready and we are preparing to make it officially generally available.  No extra steps are required to get started.

 

In part 4 of my blog series on Elastic Jobs for Azure SQL Database, I will show how to create the Elastic Job Agent and associated credentials and jobs using T-SQL.  For more extensive documentation on this subject, see here.

 

Creating the Job Agent

There is no way to create the Elastic Job Agent in T-SQL. I have already shown how to do this in PowerShell. To do this in the Azure Portal, go to Home, click the box that says “+ Create a Resource”, then search in the box for Elastic Job Agent. Select that, and then follow the steps in the portal to create the agent.

 

Kate_Smith_0-1582046909140.png

 

 

Kate_Smith_1-1582046909144.png

 

 

Creating the Credentials

Independent of creating the Elastic Job Agent (meaning, this step and the prior one can be done in any order), you need to connect to the database you have hosting the Elastic Job Agent  and create the refresh and job credentials there. Again, I assume that these credentials have already been created on the target servers and databases and show only the portions specific to the elastic job here. For a refresher on credentials, refer to this previous post.

 

Executing in the database hosting the Elastic Job Agent:

 

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'password'

CREATE DATABASE SCOPED CREDENTIAL refreshcredential
WITH IDENTITY = 'refreshcredential',
SECRET = 'password1'

CREATE DATABASE SCOPED CREDENTIAL jobcredential
WITH IDENTITY = 'jobcredential',
SECRET = 'password2'

 

You can query the credentials to make sure they are created via the following:

 

SELECT * FROM sys.database_scoped_credentials

 

Creating the Target Group 

The final step before creating the job is to specify the group that is the target of the job. Remeber, if I specify a target server, the job will target every database on that server automatically.  If you wish to exclude a database, please see the documentation here.  Again, I follow through creating the demo group just like I did with PowerShell:

 

EXEC jobs.sp_add_target_group 'DemoGroup'

-- Add a server target member
EXEC jobs.sp_add_target_group_member
'DemoGroup',
@target_type = 'SqlServer',
@refresh_credential_name='refreshcredential', --credential required to refresh the databases in server
@server_name='elasticjobstargetserver.database.windows.net'

--View the recently created target group and target group members
SELECT * FROM jobs.target_groups WHERE target_group_name='DemoGroup';
SELECT * FROM jobs.target_group_members WHERE target_group_name='DemoGroup';

 

Creating and Defining the Job 

Now fully equipped with an Elastic Job Agent, the appropriate credentials, and a defined target group, we can now create and define the Elastic Job:

 

--Add job for create table
EXEC jobs.sp_add_job @job_name='demo123', @description='Demo job'

-- Add job step for update statistics
EXEC jobs.sp_add_jobstep @job_name='demo123',
@command= 'I
(CASE
WHEN (SELECT STATS_DATE([object_id], [stats_id]) FROM sys.stats WHERE name = 'tStats') < DATEADD(day,-1, sysdatetime())
THEN 1
ELSE 0
END) = 1
UPDATE STATISTICS T(tStats);',
@credential_name='jobcredential'
@target_group_name='DemoGroup

 

At this point, the job is created and ready to run! You can verify this by querying the database:

 

SELECT * FROM jobs.jobs WHERE job_name = ‘demo123’

 

Another way to verify the job is to examine it through the Azure Portal. To do this, find all of your Elastic Job Agents (you can search for the Elastic Job Agents service in the search bar at the top of the screen), and then select your agent. From there, you can drill down into the jobs, target groups, and even browse the credentials.

 

MonitoringElasticJobAgentInPortal.png

 

Next, I will show the basics of running, scheduling, and verifying the schedule of an Elastic Job using both PowerShell and T-SQL.

 

This blog is part of a series about Elastic Jobs on Azure SQL Database.

Code samples are also available on GitHub

  1. Elastic Jobs in Azure SQL Database – What and Why
  2. Fundamental Concepts for Elastic Jobs in Azure SQL Database
  3. Creating an Elastic Jobs Agent, Credentials, and Jobs for Azure SQL Database in PowerShell
  4. Creating an Elastic Jobs Agent, Credentials, and Jobs for Azure SQL Database using T-SQL
  5. Running, Scheduling and Monitoring Elastic Jobs in Azure SQL Database
  6. Troubleshooting Common issues with Elastic Jobs in Azure SQL Database
4 Comments
Senior Member

Excellent and much needed feature and guide, thank you!

 

I think you have a small mistake in this part:

 

EXEC jobs.sp_add_target_group_member
'ServerGroup1',

I'm guessing "ServerGroup1" is supposed to be "DemoGroup"?

Occasional Visitor

Where do you define the database that the script should run? I saw the servername being set in jobs.sp_add_target_group_member but not the database where the update stats will run.

Microsoft

@leopasta The elastic jobs will target every database on the server.  You can exclude databases if needed.   Complete documentation here: https://docs.microsoft.com/en-us/azure/sql-database/elastic-jobs-tsql  

Occasional Visitor

Hi,

 

I noticed small typo in 

 

@target_type = 'SqlServe

is it supposed to be

@target_type = 'SqlServer',