在 SQL Server 2008 中使用 select 语句和附加列插入表 [英] Insert into table with select statement plus additional column in SQL Server 2008

查看:35
本文介绍了在 SQL Server 2008 中使用 select 语句和附加列插入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 select 语句中插入到表中,并向行添加其他参数,例如 -

I am trying to insert into a table from a select statement as well as add additional parameters to the rows with something like-

SELECT 
        Appt_ID,
        Pracitioner_ID,
        Appt_Book_ID,
        Start_Time,
        UR_NO,
        [Type],
       [Description]
FROM @TempWaiting,
 @EndTime,
 @PatientID, 
 @Title, 
 @FirstName, 
 @LastName, 
 @Surname, 
 @DOB
INTO PS_WAITING_LIST(Appt_ID,   
        Pracitioner_ID,
        Appt_Book_ID,
        Start_Time,
        UR_NO,
        [Type],
        [Description],
        End_Time,
        Patient_ID, 
        Title,
        First_Name,
        Middle_Name,
        Surname,
        DOB)

这是在一个存储过程中,它说的是 INTO 之后的错误.

This is within a stored procedure and all it says is error after INTO.

我也尝试将其重写为 -

I also tried re-writing it as -

insert into PS_WAITING_LIST (
        Appt_ID,    
        Pracitioner_ID,
        Appt_Book_ID,
        Start_Time,
        UR_NO,
        [Type],
        [Description],
        End_Time,
        Patient_ID, 
        Title,
        First_Name,
        Middle_Name,
        Surname,
        DOB)
SELECT 
        Appt_ID,
        Pracitioner_ID,
        Appt_Book_ID,
        Start_Time,
        UR_NO,
        [Type],
        [Description]
FROM @TempWaiting,
 @EndTime,
 @PatientID, 
 @Title, 
 @FirstName, 
 @LastName, 
 @Surname, 
 @DOB

这会引发错误,指出选择值的数量必须与插入列的数量相匹配.嗯,显然没有.

This throws an error saying the number of select value must match the number of insert column. Well obviously that does not.

我也试过 Insert into table (col1, col2..) Select * from (select (...), col3, col9)

I also tried Insert into table (col1, col2..) Select * from (select (...), col3, col9)

推荐答案

一般是这样的

insert into your_table (col1, col2, col3)
select some_column, @aVariable, 'static_string'
from another_table

这篇关于在 SQL Server 2008 中使用 select 语句和附加列插入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆