HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMajor

how do I insert a default row?

Submitted by: @import:stackexchange-dba··
0
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

INSERT INTO RR DEFAULT VALUES;


It is possible to insert multiple rows of default values by (ab)using MERGE

MERGE 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.