snippetsqlMajor
how do I insert a default row?
Viewed 0 times
rowdefaultinserthow
Problem
If I create a table with an identity column as primary key, and all the other columns have default values, for example
create table rr (id int identity(1,1) primary key, dt datetime default getdate())Solution
To insert a single row
It is possible to insert multiple rows of default values by (ab)using
INSERT INTO RR DEFAULT VALUES;It is possible to insert multiple rows of default values by (ab)using
MERGEMERGE INTO RR
USING (SELECT TOP 1000 *
FROM master..spt_values) T
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT
DEFAULT VALUES;Code Snippets
INSERT INTO RR DEFAULT VALUES;MERGE INTO RR
USING (SELECT TOP 1000 *
FROM master..spt_values) T
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT
DEFAULT VALUES;Context
StackExchange Database Administrators Q#36553, answer score: 22
Revisions (0)
No revisions yet.