1

I have a sql stored procedure which consists of two parameters. And I need to set the current value of a Sequence as the default value of a one parameter. So when I added default value for the parameter it gives me following error.

Msg 102, Level 15, State 1, Procedure sp_GET_TransformationSummary, Line 8
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Procedure sp_GET_TransformationSummary, Line 11
Incorrect syntax near the keyword 'AS'.

And here is my code..

ALTER PROCEDURE [dbo].[sp_GET_TransformationSummary]
@AreaCode           AS NVARCHAR(MAX),
@SyncId             AS INT = (SELECT CONVERT(INT, CURRENT_VALUE) FROM SYS.SEQUENCES WHERE name = 'SQ_COMMON')

AS
BEGIN
SET NOCOUNT ON;

SELECT * FROM TABLE_NAME
WHERE Area= @AreaCode AND SyncId = @SyncId;

END

And Is there any way to do this?

I need to have the CURRENT_VALUE of SQ_COMMON sequence as the default value.

2 Answers 2

3

You cannot put a SELECT statement in DEFAULT parameter definition for your stored procedure. The workaround for you is to set NULL as DEFAULT value, and then check if value of @SyncId is null => assign it the current value from your sequence.

ALTER PROCEDURE [dbo].[sp_GET_TransformationSummary]
@AreaCode           AS NVARCHAR(MAX),
@SyncId             AS INT = NULL

AS
BEGIN
SET NOCOUNT ON;

IF @SyncId IS NULL 
    SELECT @SyncId = CONVERT(INT, CURRENT_VALUE) FROM SYS.SEQUENCES WHERE name = 'SQ_COMMON'

SELECT * FROM TABLE_NAME
WHERE Area= @AreaCode AND SyncId = @SyncId;

END
Sign up to request clarification or add additional context in comments.

Comments

1

Just make Syncid input parameter NUll and Inside the Store Procedure Declare another variable and assign the statement and make that Declared variable equal to Input Parameter .

 ALTER PROCEDURE [dbo].[sp_GET_TransformationSummary]
    @AreaCode           NVARCHAR(MAX),
    @SyncId             INT  = NULL
    AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @n_syncID INT
    SET @n_syncID  = 
    SELECT CONVERT(INT, CURRENT_VALUE) FROM SYS.SEQUENCES WHERE name = 'SQ_COMMON'
    SET @n_syncID = @SyncId 

    SELECT * FROM TABLE_NAME
    WHERE Area= @AreaCode AND SyncId = @SyncId;

    END

3 Comments

I tried this way. But I need to have it as a default parameter value as I mentioned in the question
So you are looking to add the value to input parameter.Can you tell me how many values are going to for that select statement Single or Mutliple @tarzanbappa
can you please go through this one stackoverflow.com/questions/14137925/… @tarzanbappa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.