What determines success or failure of SQL Server Job? - sql

I have a query that I want to run in a SQL Server job. I would like to have the query report success if the query returns 1 or more rows, and to return failure if the query does not reutrn and rows. How would I go about doing this?

I think you will have to do this within the query:
IF ##ROWCOUNT < 1
BEGIN
RAISERROR('No records returned',16,1)
RETURN
END
If no rows are returned, an error will occur and the job will have a failure status.

Have a step that checks the count and calls RAISERROR or THROWs if there are no rows.

Related

DB2 SQL considers IF ELSE condition a DDL statement?

I am trying to use a simple IF ELSE query to test a feature with DB2 SQL. However when I attempt to execute it, I run into an error stating that I am not allowed to execute DDL statements.
What is throwing me off is that as far as I know, only database structure altering statements are considered DDL statements.
What gives?
Code:
IF 'True' = 'True' THEN
SELECT * FROM RM_TRANSACTION
FETCH FIRST 2 ROWS ONLY
FOR READ ONLY WITH UR
ELSE
SELECT * FROM RM_TRANSACTION
FETCH FIRST 4 ROWS ONLY
FOR READ ONLY WITH UR
END IF
https://imgur.com/a/58RYjpu
The problem is that you can’t ‘select to nowhere’ in a compound statement in DB2. Db2 CLP can return you the result set of a single sql statement, but it doesn’t try to do the same for select statements in a compound statement.
If you want to print the result set from a select statement in a compound statement, you can, for example, declare a cursor, fetch it in a loop, and use dbms_output.put_line calls to print the values of variables.

Determining MSSQL job status and returning 0 or 1 result

I am trying to write an MSSQL query and determine if a certain job is running or failed/unknown.
If it is running I want it to return = 0 I it is in any other status 1 or up.
Can anyone help?
I am trying to select from last_run_outcome
You can try running the following stored procedure and store the result in some temporary table, and then evaluate the result:
exec msdb.dbo.sp_help_jobactivity #job_id

getting number of records updated or inserted in sql server stored procedure

I have an SP that inserts some records and updates others and deletes some. What I want is to return the count values of what was inserted and what was updated and what was deleted. I thought I could use ##ROWCOUNT but that is always giving me a 1.
After my INSERT I run:
PRINT ##ROWCOUNT
But my message console shows what really happened and this number:
(36 row(s) affected)
1
So I can see that 36 records were actually updated but ##ROWCOUNT returned a 1.
I am trying to do the same thing after the UPDATE and DELETE parts of the SP runs with the same result.
##ROWCOUNT will show the number of rows affected by the most recent statement - if you have any statements between the INSERT and the PRINT then it will give you the wrong number.
Can you show us a little more code so we can see the order of execution?
Depending on how #ninesided's answer works for you, you could also use the output clause on each update/insert/delete and get the counts from there.
Example:
declare #count table
(
id int
)
update mytable
set oldVal = newVal
output inserted.field1 into #count
select count(*) from #count
You could reuse the count table throughout, and set variables as needed to hold the values.

SQL Server 2000 - Update rows and return updated rows

I am wondering how to rewrite the following SQL Server 2005/2008 script for SQL Server 2000 which didn't have OUTPUT yet.
Basically, I would like to update rows and return the updated rows without creating deadlocks.
Thanks in advance!
UPDATE TABLE
SET Locked = 1
OUTPUT INSERTED.*
WHERE Locked = 0
You can't in SQL Server 2000 cleanly
What you can do is use a transaction and some lock hints to prevent a race condition. Your main problem is 2 processes accessing the same row(s), not a deadlock. See SQL Server Process Queue Race Condition for more, please.
BEGIN TRANSACTION
SELECT * FROM TABLE WITH (ROWLOCK, READPAST, UPDLOCK) WHERE Locked = 0
UPDATE TABLE
SET Locked = 1
WHERE Locked = 0
COMMIT TRANSACTION
I haven't tried this, but you could also try a SELECT in an UPDATE trigger from INSERTED.

SQL - Raise error in Sproc when the select result set is empty

I am trying to execute a stored procedure within a SQL JOB step (SQL Server 2005).
I want to Raise error and fail the job step when the result set of the stored procedure I am executing is not empty.
what my stored procedure does is --I have a select statement where the rows are displayed if the current date is equal to the date in one of the columns of a table.
SELECT
Holiday_date
from tblHolidays
where
CONVERT(VARCHAR(10),GETDATE(),101) = CONVERT(VARCHAR(10),Holiday_date,101)
If the result set is empty, I want to succeed the job step and proceed with the next job step.
Any thoughts on how to get this working.
Thanks
You can try RAISERROR althought I can't remember if this will cause the whole job to fail, if it does try one of the warning severity levels.
IF ##ROWCOUNT > 0
RAISERROR ('found data', 16, 1)