In SQL Server, how can I insert data into a table that has only one column with an authentication type?

In SQL Server, how can I insert data into a table that has only one column with an authentication type? For example, insert t in the following table . How to write insert statement?

CREATE TABLE t
(
    id INT IDENTITY(1, 1) PRIMARY KEY
)

Many thanks.

+3
source share
3 answers

To insert a single value

INSERT T DEFAULT VALUES 

Or insert multiple rows in SQL Server 2008 +

MERGE INTO t
USING (SELECT TOP 100 *
       FROM master..spt_values) T
ON 1 = 0
WHEN NOT MATCHED THEN
  INSERT
  DEFAULT VALUES; 

To insert multiple lines into a single statement in previous versions, you need to enter values ​​explicitly, for example. as shown below.

SET IDENTITY_INSERT t ON

INSERT INTO t
            (id)
SELECT TOP 100  
               (SELECT ISNULL(MAX(id), 0) FROM   t (TABLOCKX)) + 
                            ROW_NUMBER() OVER (ORDER BY @@SPID)
FROM   master.dbo.spt_values

SET IDENTITY_INSERT t OFF 
+3
source

,

INSERT YourTable DEFAULT VALUES;

,

SET IDENTITY_INSERT YourTable ON
INSERT INTO YourTable (id) VALUES (5);
SET IDENTITY_INSERT YourTable OFF
+2

Try: -

INSERT INTO t Values(NULL)
-1
source

Source: https://habr.com/ru/post/1749166/


All Articles