Writing a T-SQL stored procedure to receive 4 numbers and insert them into a tableOracle GoldenGate add trandata errorsMultiple SELECT subqueries in an INSERT statement in a stored procedureMost efficient way to insert rows into a temp table in a stored procedureHow to create Dynamic table in stored procedure?SQL 2005 Unused proceduresSQL server Stored Procedure temp variable value mismatching sometimeUsing T-SQL, is it possible to a split a result set or table and then insert into two different temp tables?Insert results of spBlitzIndex stored procedure into tableSQL Server: Performance Insert Into vs Select IntoSQL Insert Into New Table Or Else Insert Overwrite Into Existing Table

Processing ADC conversion result: DMA vs Processor Registers

Is Bran literally the world's memory?

What is ls Largest Number Formed by only moving two sticks in 508?

When speaking, how do you change your mind mid-sentence?

How was Lagrange appointed professor of mathematics so early?

What was Apollo 13's "Little Jolt" after MECO?

Could a cockatrice have parasitic embryos?

Determinant of a matrix with 2 equal rows

Specify the range of GridLines

How do I deal with an erroneously large refund?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

What helicopter has the most rotor blades?

How did Elite on the NES work?

What is a 'Key' in computer science?

How to begin with a paragraph in latex

Why did Israel vote against lifting the American embargo on Cuba?

What were wait-states, and why was it only an issue for PCs?

Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table

What's parked in Mil Moscow helicopter plant?

What is the ongoing value of the Kanban board to the developers as opposed to management

Why is water being consumed when my shutoff valve is closed?

What does the black goddess statue do and what is it?

What to do with someone that cheated their way though university and a PhD program?

Like totally amazing interchangeable sister outfit accessory swapping or whatever



Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table


Oracle GoldenGate add trandata errorsMultiple SELECT subqueries in an INSERT statement in a stored procedureMost efficient way to insert rows into a temp table in a stored procedureHow to create Dynamic table in stored procedure?SQL 2005 Unused proceduresSQL server Stored Procedure temp variable value mismatching sometimeUsing T-SQL, is it possible to a split a result set or table and then insert into two different temp tables?Insert results of spBlitzIndex stored procedure into tableSQL Server: Performance Insert Into vs Select IntoSQL Insert Into New Table Or Else Insert Overwrite Into Existing Table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I need to write a stored procedure to receive 4 numbers and insert them into a table. This is what I've developed so far:



Declare 
@1 Int = 10,
@2 Int = 20,
@3 Int = 30,
@4 Int = 40


Create table #Temp(Num int)
Declare @I char(1) = 1


While (@I <= 4)
Begin

Insert Into #Temp
Select @I

SET @I +=1
end


Select * from #Temp
Drop table #Temp


I know that I can directly and statically insert the inputs into the table but I just want to know is there any better way to do that? I wanted to use a while statement but the problem is the numbers for (I) variable are being inserted into the table!! I mean the output is 1,2,3,4; what I want is 10,20,30,40.









share



















  • 1





    you want to insert these values in one column or 4 columns?

    – Learning_DBAdmin
    12 hours ago











  • I want to insert the values in 1 column

    – Pantea Tourang
    10 hours ago











  • Just so you know, in general, looping isn't very "sql"-esque. That doesn't mean it's necessarily bad, but it's a strong yellow flag that you may be using SQL more like a traditional programming language rather than as a query language.

    – David Rice
    4 hours ago

















3















I need to write a stored procedure to receive 4 numbers and insert them into a table. This is what I've developed so far:



Declare 
@1 Int = 10,
@2 Int = 20,
@3 Int = 30,
@4 Int = 40


Create table #Temp(Num int)
Declare @I char(1) = 1


While (@I <= 4)
Begin

Insert Into #Temp
Select @I

SET @I +=1
end


Select * from #Temp
Drop table #Temp


I know that I can directly and statically insert the inputs into the table but I just want to know is there any better way to do that? I wanted to use a while statement but the problem is the numbers for (I) variable are being inserted into the table!! I mean the output is 1,2,3,4; what I want is 10,20,30,40.









share



















  • 1





    you want to insert these values in one column or 4 columns?

    – Learning_DBAdmin
    12 hours ago











  • I want to insert the values in 1 column

    – Pantea Tourang
    10 hours ago











  • Just so you know, in general, looping isn't very "sql"-esque. That doesn't mean it's necessarily bad, but it's a strong yellow flag that you may be using SQL more like a traditional programming language rather than as a query language.

    – David Rice
    4 hours ago













3












3








3


1






I need to write a stored procedure to receive 4 numbers and insert them into a table. This is what I've developed so far:



Declare 
@1 Int = 10,
@2 Int = 20,
@3 Int = 30,
@4 Int = 40


Create table #Temp(Num int)
Declare @I char(1) = 1


While (@I <= 4)
Begin

Insert Into #Temp
Select @I

SET @I +=1
end


Select * from #Temp
Drop table #Temp


I know that I can directly and statically insert the inputs into the table but I just want to know is there any better way to do that? I wanted to use a while statement but the problem is the numbers for (I) variable are being inserted into the table!! I mean the output is 1,2,3,4; what I want is 10,20,30,40.









share
















I need to write a stored procedure to receive 4 numbers and insert them into a table. This is what I've developed so far:



Declare 
@1 Int = 10,
@2 Int = 20,
@3 Int = 30,
@4 Int = 40


Create table #Temp(Num int)
Declare @I char(1) = 1


While (@I <= 4)
Begin

Insert Into #Temp
Select @I

SET @I +=1
end


Select * from #Temp
Drop table #Temp


I know that I can directly and statically insert the inputs into the table but I just want to know is there any better way to do that? I wanted to use a while statement but the problem is the numbers for (I) variable are being inserted into the table!! I mean the output is 1,2,3,4; what I want is 10,20,30,40.







sql-server t-sql stored-procedures





share














share












share



share








edited 4 hours ago









Glorfindel

1,0711816




1,0711816










asked 12 hours ago









Pantea TourangPantea Tourang

294




294







  • 1





    you want to insert these values in one column or 4 columns?

    – Learning_DBAdmin
    12 hours ago











  • I want to insert the values in 1 column

    – Pantea Tourang
    10 hours ago











  • Just so you know, in general, looping isn't very "sql"-esque. That doesn't mean it's necessarily bad, but it's a strong yellow flag that you may be using SQL more like a traditional programming language rather than as a query language.

    – David Rice
    4 hours ago












  • 1





    you want to insert these values in one column or 4 columns?

    – Learning_DBAdmin
    12 hours ago











  • I want to insert the values in 1 column

    – Pantea Tourang
    10 hours ago











  • Just so you know, in general, looping isn't very "sql"-esque. That doesn't mean it's necessarily bad, but it's a strong yellow flag that you may be using SQL more like a traditional programming language rather than as a query language.

    – David Rice
    4 hours ago







1




1





you want to insert these values in one column or 4 columns?

– Learning_DBAdmin
12 hours ago





you want to insert these values in one column or 4 columns?

– Learning_DBAdmin
12 hours ago













I want to insert the values in 1 column

– Pantea Tourang
10 hours ago





I want to insert the values in 1 column

– Pantea Tourang
10 hours ago













Just so you know, in general, looping isn't very "sql"-esque. That doesn't mean it's necessarily bad, but it's a strong yellow flag that you may be using SQL more like a traditional programming language rather than as a query language.

– David Rice
4 hours ago





Just so you know, in general, looping isn't very "sql"-esque. That doesn't mean it's necessarily bad, but it's a strong yellow flag that you may be using SQL more like a traditional programming language rather than as a query language.

– David Rice
4 hours ago










4 Answers
4






active

oldest

votes


















10














You could also use a table valued parameter type in the stored procedure and pass numbers through this tvp.



Create the type



CREATE TYPE GetNumbers AS TABLE 
( Numbers INT );
GO


Create the procedure



CREATE PROCEDURE dbo.InsertNumbers 
@GetNumbers GetNumbers READONLY
AS
SET NOCOUNT ON;

CREATE TABLE #Temp(Num int);
INSERT INTO #Temp(Num)
SELECT Numbers
FROM @GetNumbers;
SELECT * FROM #Temp;
DROP TABLE #Temp;
GO


Inserting into temp table is not really needed here, only done to keep it the same as the question.



Fill up a variable with data and call the procedure



/* Declare a variable that references the type. */ 
DECLARE @GetNumbers AS GetNumbers;

/* Add data to the table variable. */
INSERT INTO @GetNumbers (Numbers)
VALUES(10),(20),(30),(40);

/* Pass the table variable data to a stored procedure. */
EXEC InsertNumbers @GetNumbers;


The example used and more on tvp's here





share
































    6














    Declare 
    @1 Int = 10,
    @2 Int = 20,
    @3 Int = 30,
    @4 Int = 40

    Create table #Temp(Num int)

    --1st way
    INSERT #Temp(Num)
    SELECT @1
    UNION ALL
    SELECT @2
    UNION ALL
    SELECT @3
    UNION ALL
    SELECT @4


    SELECT * FROM #Temp

    TRUNCATE TABLE #Temp

    --2nd way
    INSERT #Temp(Num)
    VALUES
    (@1),
    (@2),
    (@3),
    (@4)

    SELECT * FROM #Temp

    DROP TABLE #Temp




    share






























      2














      CREATE PROCEDURE dbo.InsertFourValues (@I1 int, @I2 int, @I3 int, @I4 int)
      AS

      BEGIN
      --the table below must already exist
      INSERT INTO dbo.MyTable (MyIntColumn)
      VALUES (@I1), (@I2), (@I3), (@I4);

      END


      Now you just call it using your values:



      EXEC dbo.InsertFourValues (10, 20, 30, 40);




      share






























        1














        The best bet is to do it with a static Insert Statement
        if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that



        I.E.



        While (@I <= 4)
        Begin
        Insert Into #Temp
        Select case @I when 1 then @1
        when 2 then @2
        -- etc
        End
        SET @I +=1
        end




        share
































          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          10














          You could also use a table valued parameter type in the stored procedure and pass numbers through this tvp.



          Create the type



          CREATE TYPE GetNumbers AS TABLE 
          ( Numbers INT );
          GO


          Create the procedure



          CREATE PROCEDURE dbo.InsertNumbers 
          @GetNumbers GetNumbers READONLY
          AS
          SET NOCOUNT ON;

          CREATE TABLE #Temp(Num int);
          INSERT INTO #Temp(Num)
          SELECT Numbers
          FROM @GetNumbers;
          SELECT * FROM #Temp;
          DROP TABLE #Temp;
          GO


          Inserting into temp table is not really needed here, only done to keep it the same as the question.



          Fill up a variable with data and call the procedure



          /* Declare a variable that references the type. */ 
          DECLARE @GetNumbers AS GetNumbers;

          /* Add data to the table variable. */
          INSERT INTO @GetNumbers (Numbers)
          VALUES(10),(20),(30),(40);

          /* Pass the table variable data to a stored procedure. */
          EXEC InsertNumbers @GetNumbers;


          The example used and more on tvp's here





          share





























            10














            You could also use a table valued parameter type in the stored procedure and pass numbers through this tvp.



            Create the type



            CREATE TYPE GetNumbers AS TABLE 
            ( Numbers INT );
            GO


            Create the procedure



            CREATE PROCEDURE dbo.InsertNumbers 
            @GetNumbers GetNumbers READONLY
            AS
            SET NOCOUNT ON;

            CREATE TABLE #Temp(Num int);
            INSERT INTO #Temp(Num)
            SELECT Numbers
            FROM @GetNumbers;
            SELECT * FROM #Temp;
            DROP TABLE #Temp;
            GO


            Inserting into temp table is not really needed here, only done to keep it the same as the question.



            Fill up a variable with data and call the procedure



            /* Declare a variable that references the type. */ 
            DECLARE @GetNumbers AS GetNumbers;

            /* Add data to the table variable. */
            INSERT INTO @GetNumbers (Numbers)
            VALUES(10),(20),(30),(40);

            /* Pass the table variable data to a stored procedure. */
            EXEC InsertNumbers @GetNumbers;


            The example used and more on tvp's here





            share



























              10












              10








              10







              You could also use a table valued parameter type in the stored procedure and pass numbers through this tvp.



              Create the type



              CREATE TYPE GetNumbers AS TABLE 
              ( Numbers INT );
              GO


              Create the procedure



              CREATE PROCEDURE dbo.InsertNumbers 
              @GetNumbers GetNumbers READONLY
              AS
              SET NOCOUNT ON;

              CREATE TABLE #Temp(Num int);
              INSERT INTO #Temp(Num)
              SELECT Numbers
              FROM @GetNumbers;
              SELECT * FROM #Temp;
              DROP TABLE #Temp;
              GO


              Inserting into temp table is not really needed here, only done to keep it the same as the question.



              Fill up a variable with data and call the procedure



              /* Declare a variable that references the type. */ 
              DECLARE @GetNumbers AS GetNumbers;

              /* Add data to the table variable. */
              INSERT INTO @GetNumbers (Numbers)
              VALUES(10),(20),(30),(40);

              /* Pass the table variable data to a stored procedure. */
              EXEC InsertNumbers @GetNumbers;


              The example used and more on tvp's here





              share















              You could also use a table valued parameter type in the stored procedure and pass numbers through this tvp.



              Create the type



              CREATE TYPE GetNumbers AS TABLE 
              ( Numbers INT );
              GO


              Create the procedure



              CREATE PROCEDURE dbo.InsertNumbers 
              @GetNumbers GetNumbers READONLY
              AS
              SET NOCOUNT ON;

              CREATE TABLE #Temp(Num int);
              INSERT INTO #Temp(Num)
              SELECT Numbers
              FROM @GetNumbers;
              SELECT * FROM #Temp;
              DROP TABLE #Temp;
              GO


              Inserting into temp table is not really needed here, only done to keep it the same as the question.



              Fill up a variable with data and call the procedure



              /* Declare a variable that references the type. */ 
              DECLARE @GetNumbers AS GetNumbers;

              /* Add data to the table variable. */
              INSERT INTO @GetNumbers (Numbers)
              VALUES(10),(20),(30),(40);

              /* Pass the table variable data to a stored procedure. */
              EXEC InsertNumbers @GetNumbers;


              The example used and more on tvp's here






              share













              share


              share








              edited 11 hours ago

























              answered 11 hours ago









              Randi VertongenRandi Vertongen

              5,2611926




              5,2611926























                  6














                  Declare 
                  @1 Int = 10,
                  @2 Int = 20,
                  @3 Int = 30,
                  @4 Int = 40

                  Create table #Temp(Num int)

                  --1st way
                  INSERT #Temp(Num)
                  SELECT @1
                  UNION ALL
                  SELECT @2
                  UNION ALL
                  SELECT @3
                  UNION ALL
                  SELECT @4


                  SELECT * FROM #Temp

                  TRUNCATE TABLE #Temp

                  --2nd way
                  INSERT #Temp(Num)
                  VALUES
                  (@1),
                  (@2),
                  (@3),
                  (@4)

                  SELECT * FROM #Temp

                  DROP TABLE #Temp




                  share



























                    6














                    Declare 
                    @1 Int = 10,
                    @2 Int = 20,
                    @3 Int = 30,
                    @4 Int = 40

                    Create table #Temp(Num int)

                    --1st way
                    INSERT #Temp(Num)
                    SELECT @1
                    UNION ALL
                    SELECT @2
                    UNION ALL
                    SELECT @3
                    UNION ALL
                    SELECT @4


                    SELECT * FROM #Temp

                    TRUNCATE TABLE #Temp

                    --2nd way
                    INSERT #Temp(Num)
                    VALUES
                    (@1),
                    (@2),
                    (@3),
                    (@4)

                    SELECT * FROM #Temp

                    DROP TABLE #Temp




                    share

























                      6












                      6








                      6







                      Declare 
                      @1 Int = 10,
                      @2 Int = 20,
                      @3 Int = 30,
                      @4 Int = 40

                      Create table #Temp(Num int)

                      --1st way
                      INSERT #Temp(Num)
                      SELECT @1
                      UNION ALL
                      SELECT @2
                      UNION ALL
                      SELECT @3
                      UNION ALL
                      SELECT @4


                      SELECT * FROM #Temp

                      TRUNCATE TABLE #Temp

                      --2nd way
                      INSERT #Temp(Num)
                      VALUES
                      (@1),
                      (@2),
                      (@3),
                      (@4)

                      SELECT * FROM #Temp

                      DROP TABLE #Temp




                      share













                      Declare 
                      @1 Int = 10,
                      @2 Int = 20,
                      @3 Int = 30,
                      @4 Int = 40

                      Create table #Temp(Num int)

                      --1st way
                      INSERT #Temp(Num)
                      SELECT @1
                      UNION ALL
                      SELECT @2
                      UNION ALL
                      SELECT @3
                      UNION ALL
                      SELECT @4


                      SELECT * FROM #Temp

                      TRUNCATE TABLE #Temp

                      --2nd way
                      INSERT #Temp(Num)
                      VALUES
                      (@1),
                      (@2),
                      (@3),
                      (@4)

                      SELECT * FROM #Temp

                      DROP TABLE #Temp





                      share











                      share


                      share










                      answered 12 hours ago









                      Denis RubashkinDenis Rubashkin

                      66318




                      66318





















                          2














                          CREATE PROCEDURE dbo.InsertFourValues (@I1 int, @I2 int, @I3 int, @I4 int)
                          AS

                          BEGIN
                          --the table below must already exist
                          INSERT INTO dbo.MyTable (MyIntColumn)
                          VALUES (@I1), (@I2), (@I3), (@I4);

                          END


                          Now you just call it using your values:



                          EXEC dbo.InsertFourValues (10, 20, 30, 40);




                          share



























                            2














                            CREATE PROCEDURE dbo.InsertFourValues (@I1 int, @I2 int, @I3 int, @I4 int)
                            AS

                            BEGIN
                            --the table below must already exist
                            INSERT INTO dbo.MyTable (MyIntColumn)
                            VALUES (@I1), (@I2), (@I3), (@I4);

                            END


                            Now you just call it using your values:



                            EXEC dbo.InsertFourValues (10, 20, 30, 40);




                            share

























                              2












                              2








                              2







                              CREATE PROCEDURE dbo.InsertFourValues (@I1 int, @I2 int, @I3 int, @I4 int)
                              AS

                              BEGIN
                              --the table below must already exist
                              INSERT INTO dbo.MyTable (MyIntColumn)
                              VALUES (@I1), (@I2), (@I3), (@I4);

                              END


                              Now you just call it using your values:



                              EXEC dbo.InsertFourValues (10, 20, 30, 40);




                              share













                              CREATE PROCEDURE dbo.InsertFourValues (@I1 int, @I2 int, @I3 int, @I4 int)
                              AS

                              BEGIN
                              --the table below must already exist
                              INSERT INTO dbo.MyTable (MyIntColumn)
                              VALUES (@I1), (@I2), (@I3), (@I4);

                              END


                              Now you just call it using your values:



                              EXEC dbo.InsertFourValues (10, 20, 30, 40);





                              share











                              share


                              share










                              answered 6 hours ago









                              Queue MannQueue Mann

                              48237




                              48237





















                                  1














                                  The best bet is to do it with a static Insert Statement
                                  if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that



                                  I.E.



                                  While (@I <= 4)
                                  Begin
                                  Insert Into #Temp
                                  Select case @I when 1 then @1
                                  when 2 then @2
                                  -- etc
                                  End
                                  SET @I +=1
                                  end




                                  share





























                                    1














                                    The best bet is to do it with a static Insert Statement
                                    if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that



                                    I.E.



                                    While (@I <= 4)
                                    Begin
                                    Insert Into #Temp
                                    Select case @I when 1 then @1
                                    when 2 then @2
                                    -- etc
                                    End
                                    SET @I +=1
                                    end




                                    share



























                                      1












                                      1








                                      1







                                      The best bet is to do it with a static Insert Statement
                                      if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that



                                      I.E.



                                      While (@I <= 4)
                                      Begin
                                      Insert Into #Temp
                                      Select case @I when 1 then @1
                                      when 2 then @2
                                      -- etc
                                      End
                                      SET @I +=1
                                      end




                                      share















                                      The best bet is to do it with a static Insert Statement
                                      if you really want to do it via a loop you could use the value of @I to determine which value to pass into the insert statement using a case statement would be the best route for that



                                      I.E.



                                      While (@I <= 4)
                                      Begin
                                      Insert Into #Temp
                                      Select case @I when 1 then @1
                                      when 2 then @2
                                      -- etc
                                      End
                                      SET @I +=1
                                      end





                                      share













                                      share


                                      share








                                      edited 58 mins ago









                                      RToyo

                                      1326




                                      1326










                                      answered 12 hours ago









                                      saihtam8saihtam8

                                      665




                                      665













                                          Popular posts from this blog

                                          How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

                                          Българска екзархия Съдържание История | Български екзарси | Вижте също | Външни препратки | Литература | Бележки | НавигацияУстав за управлението на българската екзархия. Цариград, 1870Слово на Ловешкия митрополит Иларион при откриването на Българския народен събор в Цариград на 23. II. 1870 г.Българската правда и гръцката кривда. От С. М. (= Софийски Мелетий). Цариград, 1872Предстоятели на Българската екзархияПодмененият ВеликденИнформационна агенция „Фокус“Димитър Ризов. Българите в техните исторически, етнографически и политически граници (Атлас съдържащ 40 карти). Berlin, Königliche Hoflithographie, Hof-Buch- und -Steindruckerei Wilhelm Greve, 1917Report of the International Commission to Inquire into the Causes and Conduct of the Balkan Wars

                                          Category:Tremithousa Media in category "Tremithousa"Navigation menuUpload media34° 49′ 02.7″ N, 32° 26′ 37.32″ EOpenStreetMapGoogle EarthProximityramaReasonatorScholiaStatisticsWikiShootMe