A job step received an error at line 13 in a PowerShell script. The corresponding line is ‘Write-Host “ServerName: ” $comp’. Correct the script and reschedule the job. The error information returned by PowerShell is: ‘Cannot invoke this function because the current host does not implement it. ‘. Process Exit Code -1. The step failed.

Solution: SQL Server Agent allows users to directly run PowerShell scripts in SQL Server Agent. Internally this is implemented by reusing the SQLPS.EXE shell. When SQL Server Agent starts SQLPS.EXE, it does not do this through the console (there is no console attached).

When you run Write-Host, PowerShell asked to write on host console but due to no availability of HOST console, we got error.

Issue: When I am running code from PowerShell console its running fine but when I execute it through SQL agent job step, I am getting below error: –

The job script encountered the following errors. These errors did not stop the script: A job step received an error at line 14 in a PowerShell script. The corresponding line is ‘$files = Get-ChildItem “\\$comp\D$\*.xls”‘. Correct the script and reschedule the job. The error information returned by PowerShell is: ‘An object at the specified path \\ServerName\D$ does not exist. ‘ A job step received an error at line 43 in a PowerShell script. The corresponding line is ‘ $str = Get-Content $i.FullName | Where-Object { ($_ -match ‘Alive’) }’. Correct the script and reschedule the job. The error information returned by PowerShell is: ‘Cannot bind argument to parameter ‘Path’ because it is null. ‘. Process Exit Code -1. The step failed.

Like this:

As per BOL (https://msdn.microsoft.com/en-us/library/ff878058(v=sql.110).aspx) : A sequence is a user-defined schema-bound object that generates a sequence of numeric values according to the specification with which the sequence was created. The sequence of numeric values is generated in an ascending or descending order at a defined interval and may cycle (repeat) as requested. Sequences, unlike identity columns, are not associated with tables. An application refers to a sequence object to receive its next value. The relationship between sequences and tables is controlled by the application. User applications can reference a sequence object and coordinate the values keys across multiple rows and tables.

This feature is really a good to have and this feature is same as Oracle’s sequence objects. A sequence object generates sequence of unique numeric values as per specifications mentioned.

A sequence can be defined as any integer data type. If the data type is not specified, a sequence defaults to bigint. A sequence value is not effected by SQL server restart. Many people think sequence is similar to identity. But big difference is sequence object is independent of a table while identity columns are attached to a table.

Create Sequence

CREATE SEQUENCE TEMPSEQ AS INT

START WITH 1 — START WITH VALUE 1

INCREMENT BY 1– INCREMENT WITH VALUE 1

MINVALUE 0 — MINIMUM VALUE TO START IS 0

MAXVALUE 5 — MAXIMUM IT CAN GO TO 5

NO CYCLE — DO NOT GO ABOVE 5

CACHE 2 — INCREMENT 2 VALUES IN MEMORY RATHER THAN INCREMENTING FROM IO

Generate value from Sequence

SELECT NEXT VALUE FOR TEMPSEQ AS seq_no;

Error when Sequence reach to MAX value

Msg 11728, Level 16, State 1, Line 18

The sequence object ‘TEMPSEQ’ has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.

Alter Sequence to RESTART it from 1 or desired value

ALTER SEQUENCE TEMPSEQ RESTART WITH 1

Create Sequence with CYCLE mode, where value will be reset to MIN value when it reaches to MAX value

CREATE SEQUENCE TEMPSEQ_CYCLE AS INT

START WITH 1 — START WITH VALUE 1

INCREMENT BY 1– INCREMENT WITH VALUE 1

MINVALUE 0 — MINIMUM VALUE TO START IS 0

MAXVALUE 5 — MAXIMUM IT CAN GO TO 5

CYCLE — RESET to 0 once reached to MAX VALUE 5

CACHE 2 — INCREMENT 2 VALUES IN MEMORY RATHER THAN INCREMENTING FROM IO

Output of Sequence with CYCLE

SELECT NEXT VALUE FOR TEMPSEQ_CYCLE AS ID, NAME FROM SYS.OBJECTS

Alter existing Sequence to set it to CYCLE mode

ALTER SEQUENCE TEMPSEQ CYCLE — RESET to 0 once reached to MAX VALUE 5

Use Sequence in Table as Default constraint.

Scenario: We need to generate token for each customer coming to meet him with HR & finance personal.

Create Sequence

CREATE SEQUENCE TOKENNO AS INT

START WITH 1 — START WITH VALUE 1

INCREMENT BY 1– INCREMENT WITH VALUE 1

MINVALUE 0 — MINIMUM VALUE TO START IS 0

MAXVALUE 5000 — MAXIMUM IT CAN GO TO 5000

NO CYCLE — RESET to 0 once reached to MAX VALUE 5000

CACHE 200 — INCREMENT 200 VALUES IN MEMORY RATHER THAN INCREMENTING FROM IO