2
CREATE PROCEDURE Usp_insertgender
    (@gendervalues VARCHAR(MAX))
AS
BEGIN
    INSERT INTO sparsha.dbo.Genders ([GenderName])
    VALUES @gendervalues;
END

I tried above procedure but I am getting an error

Msg 102, Level 15, State 1, Procedure Usp_insertgender, Line 10
Incorrect syntax near '@gendervalues'.

I am passing @gendervalues values as ('1'),('2')

2 Answers 2

4

Probably the easiest way is to define TVP:

CREATE TYPE myTVPType AS TABLE( v VARCHAR(50));  
GO  

CREATE PROCEDURE Usp_insertgender (@gendervalues myTVPType READONLY) 
AS 
BEGIN
  INSERT INTO sparsha.dbo.Genders ([GenderName]) 
  SELECT v
  FROM @gendervalues; 
END;

How to call it:

DECLARE @tvp AS myTVPType;  

INSERT INTO @TVP (v)  
VALUES ('1'),('2');

EXEC Usp_insertgender @TVP;  
GO  
Sign up to request clarification or add additional context in comments.

3 Comments

When am executing procedure getting error execute Usp_insertgender (1),(2) Incorrect syntax near '1'.
hi lad2025 thanks for giving solution to how to call Procedure. plz give a solution to how to call it on stored procedure by giving parameter as ('1'),('2') and insert multiple values. here the problem is above inserting depends on another insert query.
by giving parameter as ('1'),('2') The only way I see is dynamic SQL but it will be huge antipattern to present it here.
1

Try this: (with dynamic SQL)

CREATE PROCEDURE Usp_insertgender
    (@gendervalues VARCHAR(MAX))
AS
BEGIN
    DECLARE @sql nvarchar(2000)

    SET @sql = N'INSERT INTO sparsha.dbo.Genders ([GenderName])
    VALUES ' + @gendervalues

    EXEC sp_executesql @sql
END

1 Comment

@guna: Hi, you've received two good answers. Please accept one of them

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.