How to insert N rows of default values into a table
I have a table containing an identity column as well as a column representing the creation date:
CREATE TABLE dbo.OrderStatus
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL default GETDATE(),
CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId)
)
Since the identity column generates a value by itself and the CreationDate is always going to be the current date (GETDATE()
), I can add a row thanks to DEFAULT VALUES
:
INSERT INTO dbo.OrderStatus DEFAULT VALUES;
But what can I do if I want to add, let's say, three records?
Current solution (edited some input since it didn't make any sense)
For now, in order to do what I want, I add several rows with VALUES
:
INSERT INTO dbo.OrderStatus (CreationDate)
VALUES (GETDATE()),
(GETDATE()),
(GETDATE())
Although, I'd prefer to know the equivalent of INSERT INTO .. DEFAULT VALUES
for multiple rows, in case that I add another column with a default value later on.
Is there a way to insert N rows into a table with DEFAULT VALUES
or in a similar way?
sql sql-server tsql insert
|
show 1 more comment
I have a table containing an identity column as well as a column representing the creation date:
CREATE TABLE dbo.OrderStatus
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL default GETDATE(),
CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId)
)
Since the identity column generates a value by itself and the CreationDate is always going to be the current date (GETDATE()
), I can add a row thanks to DEFAULT VALUES
:
INSERT INTO dbo.OrderStatus DEFAULT VALUES;
But what can I do if I want to add, let's say, three records?
Current solution (edited some input since it didn't make any sense)
For now, in order to do what I want, I add several rows with VALUES
:
INSERT INTO dbo.OrderStatus (CreationDate)
VALUES (GETDATE()),
(GETDATE()),
(GETDATE())
Although, I'd prefer to know the equivalent of INSERT INTO .. DEFAULT VALUES
for multiple rows, in case that I add another column with a default value later on.
Is there a way to insert N rows into a table with DEFAULT VALUES
or in a similar way?
sql sql-server tsql insert
what are you achieving by removing default GETDATE()
– Sagar
Jun 9 '15 at 20:00
What's the purpose here - why do you need 3 "default" records?
– D Stanley
Jun 9 '15 at 20:16
@Sagar Actually, my mind bugged on that one. I'm not achieving anything. Although, I'm still interested in theINSERT INTO DEFAULT VALUES
equivalent for multiple rows.
– TheWanderingMind
Jun 9 '15 at 23:47
@DStanley I know that seems strange, but I want to create a table for the order statuses and then create a junction table between the orderStatus and language tables, which will contain the translations for each status. So, I already know that I need 5 status records for now and then the admins will be able to add additional ones from the app.
– TheWanderingMind
Jun 9 '15 at 23:50
@BishopBarber Yes that does seem a bit strange, but it may make your job easier - could you not just add a record relating to each language rather than "default" records?
– D Stanley
Jun 10 '15 at 13:15
|
show 1 more comment
I have a table containing an identity column as well as a column representing the creation date:
CREATE TABLE dbo.OrderStatus
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL default GETDATE(),
CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId)
)
Since the identity column generates a value by itself and the CreationDate is always going to be the current date (GETDATE()
), I can add a row thanks to DEFAULT VALUES
:
INSERT INTO dbo.OrderStatus DEFAULT VALUES;
But what can I do if I want to add, let's say, three records?
Current solution (edited some input since it didn't make any sense)
For now, in order to do what I want, I add several rows with VALUES
:
INSERT INTO dbo.OrderStatus (CreationDate)
VALUES (GETDATE()),
(GETDATE()),
(GETDATE())
Although, I'd prefer to know the equivalent of INSERT INTO .. DEFAULT VALUES
for multiple rows, in case that I add another column with a default value later on.
Is there a way to insert N rows into a table with DEFAULT VALUES
or in a similar way?
sql sql-server tsql insert
I have a table containing an identity column as well as a column representing the creation date:
CREATE TABLE dbo.OrderStatus
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL default GETDATE(),
CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId)
)
Since the identity column generates a value by itself and the CreationDate is always going to be the current date (GETDATE()
), I can add a row thanks to DEFAULT VALUES
:
INSERT INTO dbo.OrderStatus DEFAULT VALUES;
But what can I do if I want to add, let's say, three records?
Current solution (edited some input since it didn't make any sense)
For now, in order to do what I want, I add several rows with VALUES
:
INSERT INTO dbo.OrderStatus (CreationDate)
VALUES (GETDATE()),
(GETDATE()),
(GETDATE())
Although, I'd prefer to know the equivalent of INSERT INTO .. DEFAULT VALUES
for multiple rows, in case that I add another column with a default value later on.
Is there a way to insert N rows into a table with DEFAULT VALUES
or in a similar way?
sql sql-server tsql insert
sql sql-server tsql insert
edited Jun 10 '15 at 0:03
asked Jun 9 '15 at 19:55
TheWanderingMind
1,27131734
1,27131734
what are you achieving by removing default GETDATE()
– Sagar
Jun 9 '15 at 20:00
What's the purpose here - why do you need 3 "default" records?
– D Stanley
Jun 9 '15 at 20:16
@Sagar Actually, my mind bugged on that one. I'm not achieving anything. Although, I'm still interested in theINSERT INTO DEFAULT VALUES
equivalent for multiple rows.
– TheWanderingMind
Jun 9 '15 at 23:47
@DStanley I know that seems strange, but I want to create a table for the order statuses and then create a junction table between the orderStatus and language tables, which will contain the translations for each status. So, I already know that I need 5 status records for now and then the admins will be able to add additional ones from the app.
– TheWanderingMind
Jun 9 '15 at 23:50
@BishopBarber Yes that does seem a bit strange, but it may make your job easier - could you not just add a record relating to each language rather than "default" records?
– D Stanley
Jun 10 '15 at 13:15
|
show 1 more comment
what are you achieving by removing default GETDATE()
– Sagar
Jun 9 '15 at 20:00
What's the purpose here - why do you need 3 "default" records?
– D Stanley
Jun 9 '15 at 20:16
@Sagar Actually, my mind bugged on that one. I'm not achieving anything. Although, I'm still interested in theINSERT INTO DEFAULT VALUES
equivalent for multiple rows.
– TheWanderingMind
Jun 9 '15 at 23:47
@DStanley I know that seems strange, but I want to create a table for the order statuses and then create a junction table between the orderStatus and language tables, which will contain the translations for each status. So, I already know that I need 5 status records for now and then the admins will be able to add additional ones from the app.
– TheWanderingMind
Jun 9 '15 at 23:50
@BishopBarber Yes that does seem a bit strange, but it may make your job easier - could you not just add a record relating to each language rather than "default" records?
– D Stanley
Jun 10 '15 at 13:15
what are you achieving by removing default GETDATE()
– Sagar
Jun 9 '15 at 20:00
what are you achieving by removing default GETDATE()
– Sagar
Jun 9 '15 at 20:00
What's the purpose here - why do you need 3 "default" records?
– D Stanley
Jun 9 '15 at 20:16
What's the purpose here - why do you need 3 "default" records?
– D Stanley
Jun 9 '15 at 20:16
@Sagar Actually, my mind bugged on that one. I'm not achieving anything. Although, I'm still interested in the
INSERT INTO DEFAULT VALUES
equivalent for multiple rows.– TheWanderingMind
Jun 9 '15 at 23:47
@Sagar Actually, my mind bugged on that one. I'm not achieving anything. Although, I'm still interested in the
INSERT INTO DEFAULT VALUES
equivalent for multiple rows.– TheWanderingMind
Jun 9 '15 at 23:47
@DStanley I know that seems strange, but I want to create a table for the order statuses and then create a junction table between the orderStatus and language tables, which will contain the translations for each status. So, I already know that I need 5 status records for now and then the admins will be able to add additional ones from the app.
– TheWanderingMind
Jun 9 '15 at 23:50
@DStanley I know that seems strange, but I want to create a table for the order statuses and then create a junction table between the orderStatus and language tables, which will contain the translations for each status. So, I already know that I need 5 status records for now and then the admins will be able to add additional ones from the app.
– TheWanderingMind
Jun 9 '15 at 23:50
@BishopBarber Yes that does seem a bit strange, but it may make your job easier - could you not just add a record relating to each language rather than "default" records?
– D Stanley
Jun 10 '15 at 13:15
@BishopBarber Yes that does seem a bit strange, but it may make your job easier - could you not just add a record relating to each language rather than "default" records?
– D Stanley
Jun 10 '15 at 13:15
|
show 1 more comment
4 Answers
4
active
oldest
votes
You can use your original definition and just use a while loop, for example
DECLARE @OrderStatus TABLE
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL DEFAULT GETDATE()
--CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId) -- this can be uncommented if creating a real table.
)
DECLARE @i int = 0;
WHILE @i < 100 -- insert 100 rows. change this value to whatever you want.
BEGIN
INSERT @OrderStatus DEFAULT VALUES
SET @i = @i + 1;
END
SELECT * FROM @OrderStatus
Here's how to do it using a recursive CTE:
;with cteNums(n) AS
(
SELECT 1
UNION ALL
SELECT n + 1
FROM cteNums WHERE n < 100 -- how many times to iterate
)
INSERT @OrderStatus
SELECT * FROM cteNums
Just note that for the CTE you'd have to specify OPTION(MAXRECURSION ...)
if it's greater than 100. Also note that even though you're selecting a list of numbers from the CTE, they don't actually get inserted into the table.
add a comment |
An easier way is:
insert dbo.OrderStatus default values
go 500
this will insert 500 rows of default values.
I'm trying to make the inserts inside of aBEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?
– TheWanderingMind
Jun 10 '15 at 13:07
1
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
2
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
add a comment |
The Tally Table method can insert large sets of multiple rows, providing the tally table is big enough. This Tally table will handle up to 1000 entries.
WITH Tally (n) AS
(
-- 1000 rows
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
)
--SELECT * FROM Tally;
Create Table #temp (id int, d datetime, GUID uniqueidentifier, str1 nvarchar(1), number int)
insert into #temp
select n, getdate(), newid(), 'a', 101 from tally
where N<=100 -- THIS IS WHERE YOU INDICATE HOW MANY ROWS
select * from #temp
add a comment |
Set up a trigger when a new row is CREATEd:
https://msdn.microsoft.com/en-us/library/ms189799.aspx
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f30741629%2fhow-to-insert-n-rows-of-default-values-into-a-table%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 can use your original definition and just use a while loop, for example
DECLARE @OrderStatus TABLE
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL DEFAULT GETDATE()
--CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId) -- this can be uncommented if creating a real table.
)
DECLARE @i int = 0;
WHILE @i < 100 -- insert 100 rows. change this value to whatever you want.
BEGIN
INSERT @OrderStatus DEFAULT VALUES
SET @i = @i + 1;
END
SELECT * FROM @OrderStatus
Here's how to do it using a recursive CTE:
;with cteNums(n) AS
(
SELECT 1
UNION ALL
SELECT n + 1
FROM cteNums WHERE n < 100 -- how many times to iterate
)
INSERT @OrderStatus
SELECT * FROM cteNums
Just note that for the CTE you'd have to specify OPTION(MAXRECURSION ...)
if it's greater than 100. Also note that even though you're selecting a list of numbers from the CTE, they don't actually get inserted into the table.
add a comment |
You can use your original definition and just use a while loop, for example
DECLARE @OrderStatus TABLE
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL DEFAULT GETDATE()
--CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId) -- this can be uncommented if creating a real table.
)
DECLARE @i int = 0;
WHILE @i < 100 -- insert 100 rows. change this value to whatever you want.
BEGIN
INSERT @OrderStatus DEFAULT VALUES
SET @i = @i + 1;
END
SELECT * FROM @OrderStatus
Here's how to do it using a recursive CTE:
;with cteNums(n) AS
(
SELECT 1
UNION ALL
SELECT n + 1
FROM cteNums WHERE n < 100 -- how many times to iterate
)
INSERT @OrderStatus
SELECT * FROM cteNums
Just note that for the CTE you'd have to specify OPTION(MAXRECURSION ...)
if it's greater than 100. Also note that even though you're selecting a list of numbers from the CTE, they don't actually get inserted into the table.
add a comment |
You can use your original definition and just use a while loop, for example
DECLARE @OrderStatus TABLE
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL DEFAULT GETDATE()
--CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId) -- this can be uncommented if creating a real table.
)
DECLARE @i int = 0;
WHILE @i < 100 -- insert 100 rows. change this value to whatever you want.
BEGIN
INSERT @OrderStatus DEFAULT VALUES
SET @i = @i + 1;
END
SELECT * FROM @OrderStatus
Here's how to do it using a recursive CTE:
;with cteNums(n) AS
(
SELECT 1
UNION ALL
SELECT n + 1
FROM cteNums WHERE n < 100 -- how many times to iterate
)
INSERT @OrderStatus
SELECT * FROM cteNums
Just note that for the CTE you'd have to specify OPTION(MAXRECURSION ...)
if it's greater than 100. Also note that even though you're selecting a list of numbers from the CTE, they don't actually get inserted into the table.
You can use your original definition and just use a while loop, for example
DECLARE @OrderStatus TABLE
(
OrderStatusId int IDENTITY(1, 1) NOT NULL,
CreationDate datetime NOT NULL DEFAULT GETDATE()
--CONSTRAINT PK_OrderStatus PRIMARY KEY(OrderStatusId) -- this can be uncommented if creating a real table.
)
DECLARE @i int = 0;
WHILE @i < 100 -- insert 100 rows. change this value to whatever you want.
BEGIN
INSERT @OrderStatus DEFAULT VALUES
SET @i = @i + 1;
END
SELECT * FROM @OrderStatus
Here's how to do it using a recursive CTE:
;with cteNums(n) AS
(
SELECT 1
UNION ALL
SELECT n + 1
FROM cteNums WHERE n < 100 -- how many times to iterate
)
INSERT @OrderStatus
SELECT * FROM cteNums
Just note that for the CTE you'd have to specify OPTION(MAXRECURSION ...)
if it's greater than 100. Also note that even though you're selecting a list of numbers from the CTE, they don't actually get inserted into the table.
edited Jun 9 '15 at 20:27
answered Jun 9 '15 at 20:01
Dan Field
13.4k22746
13.4k22746
add a comment |
add a comment |
An easier way is:
insert dbo.OrderStatus default values
go 500
this will insert 500 rows of default values.
I'm trying to make the inserts inside of aBEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?
– TheWanderingMind
Jun 10 '15 at 13:07
1
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
2
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
add a comment |
An easier way is:
insert dbo.OrderStatus default values
go 500
this will insert 500 rows of default values.
I'm trying to make the inserts inside of aBEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?
– TheWanderingMind
Jun 10 '15 at 13:07
1
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
2
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
add a comment |
An easier way is:
insert dbo.OrderStatus default values
go 500
this will insert 500 rows of default values.
An easier way is:
insert dbo.OrderStatus default values
go 500
this will insert 500 rows of default values.
answered Jun 10 '15 at 0:34
benjamin moskovits
3,6161516
3,6161516
I'm trying to make the inserts inside of aBEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?
– TheWanderingMind
Jun 10 '15 at 13:07
1
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
2
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
add a comment |
I'm trying to make the inserts inside of aBEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?
– TheWanderingMind
Jun 10 '15 at 13:07
1
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
2
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
I'm trying to make the inserts inside of a
BEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?– TheWanderingMind
Jun 10 '15 at 13:07
I'm trying to make the inserts inside of a
BEGIN .. END
block. Is there a way to use your solution in that case or (should I / must I) stick with the while loop?– TheWanderingMind
Jun 10 '15 at 13:07
1
1
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
'Go' is a SSMS command and not part of SQL or TSQL.This means that you cannot use it as part of a proc or script.
– benjamin moskovits
Jun 10 '15 at 14:07
2
2
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
This is easy if you're just doing it in SSMS or SqlCmd. You can't use it in a SP or function.
– Dan Field
Jun 10 '15 at 14:07
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
@benjaminmoskovits Could you specify it in your answer? It might be clearer for newcomers, since my question doesn't include the SSMS tag.
– TheWanderingMind
Jun 10 '15 at 14:13
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
When you are working in SQL Server Management Studio in a query window you are in 'SSMS' mode and Go is available to you. It is interpeted by SSMS. If you write a stored procedure it is often referred to as a 'proc' and when the SQL Server engine encounters GO it will probably throw an error.
– benjamin moskovits
Jun 10 '15 at 16:51
add a comment |
The Tally Table method can insert large sets of multiple rows, providing the tally table is big enough. This Tally table will handle up to 1000 entries.
WITH Tally (n) AS
(
-- 1000 rows
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
)
--SELECT * FROM Tally;
Create Table #temp (id int, d datetime, GUID uniqueidentifier, str1 nvarchar(1), number int)
insert into #temp
select n, getdate(), newid(), 'a', 101 from tally
where N<=100 -- THIS IS WHERE YOU INDICATE HOW MANY ROWS
select * from #temp
add a comment |
The Tally Table method can insert large sets of multiple rows, providing the tally table is big enough. This Tally table will handle up to 1000 entries.
WITH Tally (n) AS
(
-- 1000 rows
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
)
--SELECT * FROM Tally;
Create Table #temp (id int, d datetime, GUID uniqueidentifier, str1 nvarchar(1), number int)
insert into #temp
select n, getdate(), newid(), 'a', 101 from tally
where N<=100 -- THIS IS WHERE YOU INDICATE HOW MANY ROWS
select * from #temp
add a comment |
The Tally Table method can insert large sets of multiple rows, providing the tally table is big enough. This Tally table will handle up to 1000 entries.
WITH Tally (n) AS
(
-- 1000 rows
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
)
--SELECT * FROM Tally;
Create Table #temp (id int, d datetime, GUID uniqueidentifier, str1 nvarchar(1), number int)
insert into #temp
select n, getdate(), newid(), 'a', 101 from tally
where N<=100 -- THIS IS WHERE YOU INDICATE HOW MANY ROWS
select * from #temp
The Tally Table method can insert large sets of multiple rows, providing the tally table is big enough. This Tally table will handle up to 1000 entries.
WITH Tally (n) AS
(
-- 1000 rows
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
)
--SELECT * FROM Tally;
Create Table #temp (id int, d datetime, GUID uniqueidentifier, str1 nvarchar(1), number int)
insert into #temp
select n, getdate(), newid(), 'a', 101 from tally
where N<=100 -- THIS IS WHERE YOU INDICATE HOW MANY ROWS
select * from #temp
answered Nov 16 '18 at 19:13
Jens Frandsen
446611
446611
add a comment |
add a comment |
Set up a trigger when a new row is CREATEd:
https://msdn.microsoft.com/en-us/library/ms189799.aspx
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
add a comment |
Set up a trigger when a new row is CREATEd:
https://msdn.microsoft.com/en-us/library/ms189799.aspx
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
add a comment |
Set up a trigger when a new row is CREATEd:
https://msdn.microsoft.com/en-us/library/ms189799.aspx
Set up a trigger when a new row is CREATEd:
https://msdn.microsoft.com/en-us/library/ms189799.aspx
answered Jun 9 '15 at 20:02
Randall
122
122
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
add a comment |
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
This method is not efficient (recursion), and somewhat constrained to always adding the same number of rows, and would require a batch column key for termination of the trigger loop. However, +10 for creativity!
– Jens Frandsen
Nov 14 '18 at 18:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f30741629%2fhow-to-insert-n-rows-of-default-values-into-a-table%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
what are you achieving by removing default GETDATE()
– Sagar
Jun 9 '15 at 20:00
What's the purpose here - why do you need 3 "default" records?
– D Stanley
Jun 9 '15 at 20:16
@Sagar Actually, my mind bugged on that one. I'm not achieving anything. Although, I'm still interested in the
INSERT INTO DEFAULT VALUES
equivalent for multiple rows.– TheWanderingMind
Jun 9 '15 at 23:47
@DStanley I know that seems strange, but I want to create a table for the order statuses and then create a junction table between the orderStatus and language tables, which will contain the translations for each status. So, I already know that I need 5 status records for now and then the admins will be able to add additional ones from the app.
– TheWanderingMind
Jun 9 '15 at 23:50
@BishopBarber Yes that does seem a bit strange, but it may make your job easier - could you not just add a record relating to each language rather than "default" records?
– D Stanley
Jun 10 '15 at 13:15