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;
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
|
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
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
|
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
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
sql-server t-sql stored-procedures
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
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
|
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
|
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);
|
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
|
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
|
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
|
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
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
edited 11 hours ago
answered 11 hours ago
Randi VertongenRandi Vertongen
5,2611926
5,2611926
|
|
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
|
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
|
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
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
answered 12 hours ago
Denis RubashkinDenis Rubashkin
66318
66318
|
|
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);
|
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);
|
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);
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);
answered 6 hours ago
Queue MannQueue Mann
48237
48237
|
|
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
|
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
|
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
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
edited 58 mins ago
RToyo
1326
1326
answered 12 hours ago
saihtam8saihtam8
665
665
|
|
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