Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)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
How to translate "red flag" into Spanish?
What's the difference between using dependency injection with a container and using a service locator?
What does the black goddess statue do and what is it?
Is it appropriate to mention a relatable company blog post when you're asked about the company?
Raising a bilingual kid. When should we introduce the majority language?
Could a cockatrice have parasitic embryos?
Why aren't road bicycle wheels tiny?
Where to find documentation for `whois` command options?
Is Bran literally the world's memory?
Why did Europeans not widely domesticate foxes?
Could typically electronic sounds be generated mechanically?
When I export an AI 300x60 art board it saves with bigger dimensions
What is the purpose of the side handle on a hand ("eggbeater") drill?
What was Apollo 13's "Little Jolt" after MECO?
What helicopter has the most rotor blades?
Co-worker works way more than he should
Israeli soda type drink
When does Bran Stark remember Jamie pushing him?
Are there existing rules/lore for MTG planeswalkers?
My admission is revoked after accepting the admission offer
Why do owned slices take 16 bytes in rust? (on x64 machine)
How was Lagrange appointed professor of mathematics so early?
Determinant of a matrix with 2 equal rows
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
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)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
add a comment |
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
8 hours ago
I want to insert the values in 1 column
– Pantea Tourang
7 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
1 hour ago
add a comment |
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 30 mins ago
Glorfindel
1,0711816
1,0711816
asked 9 hours ago
Pantea TourangPantea Tourang
244
244
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
8 hours ago
I want to insert the values in 1 column
– Pantea Tourang
7 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
1 hour ago
add a comment |
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
8 hours ago
I want to insert the values in 1 column
– Pantea Tourang
7 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
1 hour ago
1
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
8 hours ago
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
8 hours ago
I want to insert the values in 1 column
– Pantea Tourang
7 hours ago
I want to insert the values in 1 column
– Pantea Tourang
7 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
1 hour 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
1 hour ago
add a comment |
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
add a comment |
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
add a comment |
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);
add a comment |
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
ect
End
SET @I +=1
end
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f235465%2fwriting-a-t-sql-stored-procedure-to-receive-4-numbers-and-insert-them-into-a-tab%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
add a comment |
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
add a comment |
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 7 hours ago
answered 7 hours ago
Randi VertongenRandi Vertongen
5,2411925
5,2411925
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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 8 hours ago
Denis RubashkinDenis Rubashkin
66318
66318
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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 2 hours ago
Queue MannQueue Mann
48237
48237
add a comment |
add a comment |
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
ect
End
SET @I +=1
end
add a comment |
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
ect
End
SET @I +=1
end
add a comment |
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
ect
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
ect
End
SET @I +=1
end
answered 8 hours ago
saihtam8saihtam8
665
665
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f235465%2fwriting-a-t-sql-stored-procedure-to-receive-4-numbers-and-insert-them-into-a-tab%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
8 hours ago
I want to insert the values in 1 column
– Pantea Tourang
7 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
1 hour ago