如何使用select语句和一些其他列在sql表中插入数据

时间:2012-05-29 10:25:45

标签: sql-server

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'

我从select语句中获取一些值,并从我自己的代码中获取rest id

有可能吗?

2 个答案:

答案 0 :(得分:1)

是的当然是可能的 - 用于插入的SELECT可以在其值列表中同时包含列和“常量”:

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

答案 1 :(得分:0)

你可以这样做

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

在选择之后,不要让您的值不来自选择。将它与选择混合。