tags:

views:

381

answers:

5

I am trying to insert values from one table in to the other, with an additonal parameter that is required. For instance:

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 am not quite sure how use it in the same INSERT statement. (Im using SQL 2005 if it matters)

edit: Ouch...looks like i threw out filet mignon to lions :-)

+12  A: 

Maybe

INSERT INTO table1(someInt, someOtherInt, name, someBit)    
SELECT someInt, someOtherInt, name, 0
FROM table2
Jhonny D. Cano -Leftware-
hehehe :) almost too simple to be true, right? :)
Jaka Jančar
yeah, sometimes we stuck ourselves, it can happen to anyone of us
Jhonny D. Cano -Leftware-
I type too slow to get answers in on these. I started when there was no answer and there were already 3 by the time I hit "Submit" :)
Tom H.
I'm late for this karma feast, so I'll just upvote everyone :)
Quassnoi
Thanks guys. the quick responses were much appreciated.
Mike_G
+4  A: 

Perhaps

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

?

Jaka Jančar
+4  A: 
INSERT INTO table1(someInt, someOtherInt, name, someBit)    
SELECT someInt, someOtherInt, name, 0 FROM table2
dotjoe
+3  A: 

You can just hard-code the value in the SELECT like so:

INSERT INTO
     dbo.table1
(
     someInt,
     someOtherInt,
     name,
     someBit
)
SELECT
     someInt,
     someOtherInt,
     name,
     0
FROM
     dbo.table2
Tom H.
Why not just set the default on the table?
Bob The Janitor
0 may not always be the default
Tom H.
+2  A: 

You could also define a default value for that column on the destination table. This way you wouldn't have to reference it all in your insert statement.

Lets say your SomeBit is the Active flag or status on a customer table. Active 0 = off/false/no Active 1 = on/true/yes.

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