0

Here is my procedure

CREATEPROCEDURE mysp
    @id int
    @param1 int
    @param2 int
    @param3 datetime
    @param4 bit
AS
    DECLARE @tv TABLE
    ( 
         param1 INT
    )
    INSERT INTO @tv ( param1 )
    SELECT param1 FROM tbl1 WHERE id = @id and IsActive = 1


    INSERT INTO tbl2
    (   
     id
    ,param1 
    ,param2 
    ,param3 
    ,param4 
    )
    VALUES
    (
     // all recors from @tv
    ,@param1 
    ,@param2 
    ,@param3 
    ,@param4 
   )

@tv will have multiple records i want to insert each value of param1 in @tv insert into another table along with other parameter param1, param2, param3, param4. Something like this

Is there a way of doing this. i dont want to use any cursor for this .

Thanks. help will be really appreciated.

1
  • I'm not quite understanding - you're not using your @paramX parameters in your SP, only the [@id]. Commented Dec 16, 2014 at 17:12

2 Answers 2

1

It looks to me like you can simplify the whole thing to:

CREATE PROCEDURE mysp
    @id INT ,
    @param1 INT ,
    @param2 INT ,
    @param3 DATETIME ,
    @param4 BIT
AS
    INSERT  INTO tbl2
            ( id ,
              param1 ,
              param2 ,
              param3 ,
              param4
            )
            SELECT  param1 ,
                    @param1 ,
                    @param2 ,
                    @param3 ,
                    @param4
            FROM    tbl1
            WHERE   id = @id
                    AND isActive = 1

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

1 Comment

That is a great idea. That prevents me from using a table variable thanks man. really appreciate it.
0

change your second insert like this.

INSERT INTO tbl2
            (id,param1,param2,param3,param4)
SELECT param1,@param1,@param2,@param3,@param4
FROM   @tv 

or You can directly do a insert from tbl1 no need to declare a table variable (@tv)

INSERT INTO tbl2
            (id,param1,param2,param3,param4)
SELECT param1,@param1,@param2,@param3,@param4
FROM   tbl1 WHERE id = @id and IsActive = 1

Comments

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.