Writing a T-SQL stored procedure to receive 4 numbers and insert them into a table Announcing...
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Deciphering death certificate writing
Eigenvalues of the Laplacian of the directed De Bruijn graph
What is the definining line between a helicopter and a drone a person can ride in?
Show two Lagrangians are equivalent
Is there a verb for listening stealthily?
Translate text contents of an existing file from lower to upper case and copy to a new file
What is the ongoing value of the Kanban board to the developers as opposed to management
Simulate round-robin tournament draw
Is a self contained air-bullet cartridge feasible?
Processing ADC conversion result: DMA vs Processor Registers
My admission is revoked after accepting the admission offer
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY
Where to find documentation for `whois` command options?
/bin/ls sorts differently than just ls
How did Elite on the NES work?
What's parked in Mil Moscow helicopter plant?
What helicopter has the most rotor blades?
Why isn't everyone flabbergasted about Bran's "gift"?
How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?
When I export an AI 300x60 art board it saves with bigger dimensions
What do you call an IPA symbol that lacks a name (e.g. ɲ)?
All ASCII characters with a given bit count
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
9 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 45 mins ago
Glorfindel
1,0711816
1,0711816
asked 9 hours ago
Pantea TourangPantea Tourang
294
294
1
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
9 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
9 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
9 hours ago
you want to insert these values in one column or 4 columns?
– Learning_DBAdmin
9 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 8 hours ago
answered 8 hours ago
Randi VertongenRandi Vertongen
5,2511925
5,2511925
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 9 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 3 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 9 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
9 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