Tushar T.
Tushar T.

Reputation: 345

How can i insert data in sql table with select statement and some additional columns

insert into
   [Client].[generalcontact_info]([userid], [first_name], [last_name],
                                  [email], [mobile],
                                  [country], [state], [city], [address],
                                  [Case_id], [contact_type], [query]) 
   select 
       [userid], [first_name], [last_name],
       [email], [mobile],
       [country], [state], [city], [address] 
   from 
       [Admin].[basicinfo] 
   where 
       [userid] = 100003

,'4521r521','Inside','what is my password'

I am taking some value from select statement and rest id from my own code

is it possible to do?

Upvotes: 1

Views: 174

Answers (2)

marc_s
marc_s

Reputation: 755541

Yes of course it's possible - the SELECT used to insert can have both columns as well as "constants" in its list of values:

insert into
   [Client].[generalcontact_info]([userid], [first_name], [last_name],
                                  [email], [mobile],
                                  [country], [state], [city], [address],
                                  [Case_id], [contact_type], [query]) 
   select 
       [userid], [first_name], [last_name],
       [email], [mobile],
       [country], [state], [city], [address],
       '4521r521', 'Inside', 'what is my password'
   from 
       [Admin].[basicinfo] 
   where 
       [userid] = 100003

Upvotes: 1

juergen d
juergen d

Reputation: 204924

You can do that like this

insert into table (col1, col2, col3)
select col1, 'static_value', col2
from table2

Don't put your values not coming from the select after the select. Mix it with the select.

Upvotes: 0

Related Questions