Insert the NULL and DEFAULT values ​​using the INSERT / SELECT statement

The MICROSOFT SQL SERVER has the following table:

CREATE TABLE [T1]
(
    [ID] int IDENTITY (1, 1) NOT NULL,
    [col1] int NOT NULL,
    [col2] int NOT NULL,
    [col3] int NULL,
    [col4] datetime NOT NULL DEFAULT (getdate()),
    [col5] datetime NOT NULL DEFAULT (getdate())
)

I want to write an insert statement that selects 2 columns from another table and inserts the entire other column as NULL or default. This is what I have tried so far (but this does not work):

    INSERT INTO [T1] ([col1],[col2], [COL3])
SELECT [1column],[2column],NULL
FROM [T2]

When I right-click on table T1 and select an open table, the table has only 2 columns, even if the column “folder” in the object explorer has all the columns

What I want to achieve is to have in T1: in Col1 and COL2 the result is SELECT and COL3 NULL, and in COL4 and COL5 - the default value!

+3
source share
3 answers

, , , . , , , .

, .

+1
INSERT INTO [T1] ([col1], [col2], [COL3])
SELECT [1column],[2column],NULL
FROM [T2]

, :

INSERT
INTO    [T1] ([col1], [col2], [col3])
SELECT  1, 2, NULL

SELECT  *
FROM    [T1]

id    col1  col2  col3  col4                     col5
---   ---   ---   ---   ---                      ---
1     1     2     NULL  2009-04-22 15:46:47.090  2009-04-22 15:46:47.090

?

+2

, .

  • SSMS "". 5 ? SELECT * FROM T1 , .

  • .. , [SQL] , SELECT * FROM T1?

  • SELECT , INSERT ( ), MetaData SELECT *.

+1
source

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


All Articles