Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table Announcing...
How do I deal with an erroneously large refund?
What do you call an IPA symbol that lacks a name (e.g. ɲ)?
Will I be more secure with my own router behind my ISP's router?
Why doesn't the university give past final exams' answers?
How would you suggest I follow up with coworkers about our deadline that's today?
Where to find documentation for `whois` command options?
Protagonist's race is hidden - should I reveal it?
Marquee sign letters
All ASCII characters with a given bit count
Suing a Police Officer Instead of the Police Department
What is /etc/mtab in Linux?
What to do with someone that cheated their way though university and a PhD program?
What does the black goddess statue do and what is it?
How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?
What is a 'Key' in computer science?
Is it accepted to use working hours to read general interest books?
What helicopter has the most rotor blades?
Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table
/bin/ls sorts differently than just ls
Israeli soda type drink
What is ls Largest Number Formed by only moving two sticks in 508?
Is there a way to fake a method response using Mock or Stubs?
How to translate "red flag" into Spanish?
"Working on a knee"
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:30 pm 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
13 hours ago
I want to insert the values in 1 column
– Pantea Tourang
11 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
5 hours 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 4 hours ago
Glorfindel
1,0711816
1,0711816
asked 13 hours ago
Pantea TourangPantea Tourang
294
294
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
13 hours ago
I want to insert the values in 1 column
– Pantea Tourang
11 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
5 hours ago
add a comment |
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
13 hours ago
I want to insert the values in 1 column
– Pantea Tourang
11 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
5 hours ago
1
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
13 hours ago
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
13 hours ago
I want to insert the values in 1 column
– Pantea Tourang
11 hours ago
I want to insert the values in 1 column
– Pantea Tourang
11 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
5 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
5 hours 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
-- etc
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 12 hours ago
answered 12 hours ago
Randi VertongenRandi Vertongen
5,2611926
5,2611926
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 13 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 7 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
-- etc
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
-- etc
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
-- 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 1 hour ago
RToyo
1326
1326
answered 13 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
13 hours ago
I want to insert the values in 1 column
– Pantea Tourang
11 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
5 hours ago