XML DML (modify) in SQL Server Agent Job - sql-server-2008

I'm trying to modify an XML value using XML.modify in an SQL Server Agent job. I'm using SQL Server 2008. Here is my code...
DECLARE #temp XML;
DECLARE #newname VARCHAR(50);
SELECT #temp = CAST(ExtensionSettings AS XML) FROM [ReportServer].[dbo].[Subscriptions] WHERE SubscriptionID = 'a2e1dd4e-5f65-4f0e-bc5a-8e58d21d7292';
SET #newname = 'Monthly_Data_' + CONVERT(VARCHAR(7), DATEADD(day, -1, GETDATE()), 120);
SET #temp.modify('replace value of (/ParameterValues/ParameterValue/Value[../Name/text()="FILENAME"]/text())[1] with sql:variable("#newname")');
UPDATE [ReportServer].[dbo].[Subscriptions] SET ExtensionSettings = CAST(#temp AS varchar(2000)) WHERE SubscriptionID = 'a2e1dd4e-5f65-4f0e-bc5a-8e58d21d7292';
This code runs fine if I just run it in a query window, but when I run it as a step in my job, the job fails. The problem seems to be with the line...
SET #temp.modify('replace value of (/ParameterValues/ParameterValue/Value[../Name/text()="FILENAME"]/text())[1] with sql:variable("#newname")');
Because if I comment it out and run the job, it completes fine.
Can XML.modify not be used in a job? What am I missing?!
Thanks

I worked on this the better part of today, finally post my question, and promptly found the answer!
I finally discovered how to view the job run history (right click on job, View History). There it mentioned QUOTED_IDENTIFIER being set wrong. A Google search later tells me that Agent jobs by default turn QUOTED_IDENTIFIER OFF (https://dba.stackexchange.com/questions/52802/sql-server-agent-quoted-identifier).
I added the line...
SET QUOTED_IDENTIFIER ON
to my job's step and it started working!

Related

Creating a cron job in SQL server that executes a procedure every day

I am working on SQL server and have a procedure that takes data from one table and put into other table. I want to execute this procedure every midnight. I have searched on this
I didn't find a proper way to achieve this.
My procedure is "archive_table_sp" I want to run this every day at 12 midnight
This sounds like setting up a SQL Server Agent Job.
You can do something like this, and it is fairly simple. As long as you have full access to the SQL Server Management Studio (SSMS), you should be able to set this up.
Below is the command (T-SQL) way of doing it. I would use GUI method as it is probably easier (please see the link down below for detail).
USE msdb ;
GO
EXEC dbo.sp_add_job
#job_name = N'Weekly Sales Data Backup' ;
GO
EXEC sp_add_jobstep
#job_name = N'Weekly Sales Data Backup',
#step_name = N'Set database to read only',
#subsystem = N'TSQL',
#command = N'ALTER DATABASE SALES SET READ_ONLY',
#retry_attempts = 5,
#retry_interval = 5 ;
GO
EXEC dbo.sp_add_schedule
#schedule_name = N'RunOnce',
#freq_type = 1,
#active_start_time = 233000 ;
USE msdb ;
GO
EXEC sp_attach_schedule
#job_name = N'Weekly Sales Data Backup',
#schedule_name = N'RunOnce';
GO
EXEC dbo.sp_add_jobserver
#job_name = N'Weekly Sales Data Backup';
GO
For more information, please go to the following:
https://learn.microsoft.com/en-us/sql/ssms/agent/create-a-job?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/ssms/agent/schedule-a-job?view=sql-server-ver15

How to run a query in every 5 minute in SQL Server

I want to run a query in every 5 minutes. I am wondering if there is something like a timer exists in SQL Server.
It would be really helpful for me. I googled a lot but i didn't get anything helpful.
Your suggestions would be much appreciated.
Pooja
You can create a SQL Agent Job via the gui or using these stored procedures: https://technet.microsoft.com/en-us/library/ms181153(v=sql.105).aspx
Also check this example (borrowed from https://www.mssqltips.com/sqlservertip/3052/simple-way-to-create-a-sql-server-job-using-tsql/ ) :
USE msdb
go
CREATE procedure [dbo].[sp_add_job_quick]
#job nvarchar(128),
#mycommand nvarchar(max),
#servername nvarchar(28),
#startdate nvarchar(8),
#starttime nvarchar(8)
as
--Add a job
EXEC dbo.sp_add_job
#job_name = #job ;
--Add a job step named process step. This step runs the stored procedure
EXEC sp_add_jobstep
#job_name = #job,
#step_name = N'process step',
#subsystem = N'TSQL',
#command = #mycommand
--Schedule the job at a specified date and time
exec sp_add_jobschedule #job_name = #job,
#name = 'MySchedule',
#freq_type=1,
#active_start_date = #startdate,
#active_start_time = #starttime
-- Add the job to the SQL Server Server
EXEC dbo.sp_add_jobserver
#job_name = #job,
#server_name = #servername
exec dbo.sp_add_job_quick
#job = 'myjob', -- The job name
#mycommand = 'sp_who', -- The T-SQL command to run in the step
#servername = 'serverName', -- SQL Server name. If running localy, you can use #servername=##Servername
#startdate = '20130829', -- The date August 29th, 2013
#starttime = '160000' -- The time, 16:00:00
Create a batch file and use windows scheduler to trigger it every five minutes.
Or write a service in .Net
Working with SQL-Server Agent job
Maintenance plan
Windows job scheduler
Trick for run on SSMS query windows (Not recommend for production or main plan)
If you need run a simple query on SSMS you can use WAITFOR DELAY statement
for example:
Declare #a int = 0
WHILE 1=1
BEGIN
-- Example code
EXEC sp_WhoIsActive -- You can write you query or code
WAITFOR DELAY '00:05:00'
IF #a = 10
Break;
SET #a = #a + 1
END
Parameter #a is important for count of running. limit to 10 running in this query., or this query run untiled, your session is open. when you close session or stop running this query is stop to work.
This is not a best practice, and use for typical trick on work. for testing, check results... this is not idea for production long time

Variables not recognized in an Execute SQL task

I have this sql statements running in my ssis package. I have hard coded the email address. How can i pass it as variable and externalize the variable and can pass the value from config file?
The #PackageStartTime is the system start time. I have declared that variable and set the expression in property window with #[System::StartTime] and evaluated that expression. But when i run this package and when it hits this particular task it get stuck at there saying PackageStartTime parameter is not recognized and result property is not set correctly.
Here is my code:
DECLARE #PackageStartTime Varchar(250)
SET #PackageStartTime =?
IF (SELECT COUNT(*) FROM [dbo].[Table1] WHERE RowCDate >= #PackageStartTime) > 0
BEGIN
DECLARE #SUB Varchar(250)
SET #SUB = 'File Failed' + ##SERVERNAME
DECLARE #BODY Varchar(250)
SET #BODY = 'File Failed' + ##SERVERNAME
EXEC msdb.dbo.sp_send_dbmail
#profile_name='default',
#recipients='dev#null.com',
#subject=#SUB,
#body=#BODY,
#query= 'SELECT DISTINCT FileLoadName FROM [dbo].[Table1] WHERE RowCDate >= #PackageStartTime',
#attach_query_result_as_file=1
Any idea what to solve these two things?
Thanks in advance.
Maina, I just responded to your another post on this subject. You have three posts so far that I have encountered. The questions you have asked in this one are very general in nature and the answer can be found in any good book (such as Professional Microsoft SQL Server 2008 Integration Services). You can also find complete step-by-step answer on many websites (but do not use them as a replacement of a book; otherwise chances are your knowledge of SSIS will remain patchy.
With that said, here are two links that answer your questions:
1. Passing a variable: https://www.simple-talk.com/sql/ssis/passing-variables-to-and-from-an-ssis-task/ (Actually, it appears I had sent this link to you earlier as well.)
Assigning variable values from config - http://www.bidn.com/blogs/DevinKnight/ssis/1655/passing-values-into-an-ssis-package-at-runtime-from-outside-the-package
Focus on item 1 first. After you have practiced it, create a new Execute sql task and put just his much code inside it:
DECLARE #PackageStartTime Varchar(40)=?;
SELECT COUNT(*) FROM [dbo].[Table1] WHERE RowCDate >= #PackageStartTime;
and store this row count in a package variable iCount. Returning the value is done through ResultSet property. If you are able to make this task work, you have enhanced your proficiency in Execute SQL Task.

Variable persistence inside SQL server transactions

I've got a large T-SQL script that opens and closes a few transactions. I have read that committing a batch with a GO statement effectively clears all variables out from it's scope.
Given the script below, will #MyImportantVariable be defined after the transaction is committed?
Is this an issue, if so, how do I overcome it?
DECLARE #MyImportantVariable INT;
SET #MyImportantVariable = 42;
DECLARE #Counter INT;
SET #Counter = 0
DECLARE UpdateGarmentCursor CURSOR FAST_FORWARD
FOR
SELECT
MyColumn
FROM
MyWonderfulTable
BEGIN TRANSACTION
WHILE ##TRAN_STATUS = 0
BEGIN
-- Do interesting work in here
SET #Counter = #Counter +1
IF(#Counter>10)
BEGIN
COMMIT TRANSACTION
-- What happens here to #MyImportantVariable?
SET #Counter = 0
BEGIN TRANSACTION
END
END
-- Close the transaction one last time
COMMIT TRANSACTION
The variable will still exist.
Your example contains no GO commands, only transactions.
GO signals the end of a batch... batches and transactions are two different things entirely.
From MSDN:
GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor.
SQL Server utilities interpret GO as a signal that they should send
the current batch of Transact-SQL statements to an instance of SQL
Server. The current batch of statements is composed of all statements
entered since the last GO, or since the start of the ad hoc session or
script if this is the first GO.

Assigning function result to SQL variable and displaying

Migrating asp.net code (VB.net) to use functions and subroutines as parameters.
Using MS Server Management Studio to create said functions and subs.
Would like to test the functions from within MS SMS before testing them via the web page.
Here's an example. Say I have a function called "dbo.getNumber"
I'm trying to test it using the following:
USE [someDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
declare #value int;
select #value = dbo.getNumber;
print #value;
go
When I type F5 (to run the "query") it gives the following msg:
"The name "dbo.getNumber" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted."
The function dbo.getNumber was accepted just fine, btw. (It's counting records of a database that meet certain criteria.)
Hopefully you can infer from the non-working code what I am trying to do.
How can I print the value of a function (for testing purposes) from within SMS?
Correct solution as per James Johnson, below:
USE [someDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
declare #value int;
select #value = dbo.getNumber();
print #value;
go
Note the parens for the function invocation.
Note also: intellisense in SMS underlines dbo.getNumber() as if it were an error, but running the query with F5 works and outputs the right result.
You need to call it like this:
select #value = dbo.getNumber()