Insert a value into an SQL table and use the SELECT statement

I am trying to insert values ​​from one table into another, with an additional parameter that is required. For example:

INSERT INTO table1(someInt, someOtherInt, name, someBit)    
SELECT someInt, someOtherInt, name FROM table2

someBit is not allowed to be null and I need to set it to False, but I'm not quite sure how to use it in the same INSERT statement. (I am using SQL 2005 if that matters)

edit: Oh ... it looks like I threw the filet of the minion to the lions :-)

+3
source share
5 answers

Can

INSERT INTO table1(someInt, someOtherInt, name, someBit)    
SELECT someInt, someOtherInt, name, 0
FROM table2
+14
source

Can

INSERT INTO table1(someInt, someOtherInt, name, someBit)
SELECT someInt, someOtherInt, name, 0 FROM table2

?

+4
source
INSERT INTO table1(someInt, someOtherInt, name, someBit)    
SELECT someInt, someOtherInt, name, 0 FROM table2
+4

SELECT :

INSERT INTO
     dbo.table1
(
     someInt,
     someOtherInt,
     name,
     someBit
)
SELECT
     someInt,
     someOtherInt,
     name,
     0
FROM
     dbo.table2
+3

. , insert.

Suppose your SomeBit is an Active flag or status in a client table. Active 0 = off / False / no Active 1 = on / true / yes.

ALTER TABLE [dbo].[Customer] ADD  CONSTRAINT [DF_Customer_Active]  DEFAULT ((1)) FOR [Active]
+2
source

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


All Articles