Sql server T-SQL-创建临时表,其中包含来自原始表中同一列的2个单元格以及更多列
我有表名employeeLog 我的专栏是: 雇员ID int 参数化int 文本nvarchar50 ParameterID包含组织中发生的事情的索引,Text包含描述 我需要创建一个临时表,其中包含两种不同的参数化情况。以及employee表中的firstname和lastname 我试着这样做:Sql server T-SQL-创建临时表,其中包含来自原始表中同一列的2个单元格以及更多列,sql-server,tsql,insert,subquery,Sql Server,Tsql,Insert,Subquery,我有表名employeeLog 我的专栏是: 雇员ID int 参数化int 文本nvarchar50 ParameterID包含组织中发生的事情的索引,Text包含描述 我需要创建一个临时表,其中包含两种不同的参数化情况。以及employee表中的firstname和lastname 我试着这样做: insert into @tmpReportTable (col1, col2, col3, col4) (select top 1 Text from employeeLog where E
insert into @tmpReportTable (col1, col2, col3, col4)
(select top 1 Text
from employeeLog
where EmployeeID = @employee_id
and ParameterID = @event_ID) ,
(select top 1 Text
from employeeLog
where EmployeeID = @employee_id and ParameterID = @Location_ID),
(select top 1 firstname
from employees
where EmployeeID = @employee_id),
(select top 1 LastName
from employees
where EmployeeID = @employee_id),
而且它起作用了
有没有更好的方法,或者我可以在一个选择中插入名字和姓氏
感谢您的帮助:这回答了问题的原始版本 我想你只是想要这个:
insert into @tmpReportTable (col1, col2)
select (select top 1 Text
from employeeLog
Where EmployeeID = @employee_id And ParameterID = @event_ID
) as event,
(select top 1 Text
from employeeLog
where EmployeeID = @employee_id And ParameterID = @Location_ID
) as Location;
这些是标量子查询,它们可以直接进入SELECT,而不需要将它们放在FROM子句中。您缺少and
insert into @tmpReportTable (col1, col2)
select
T.Event, T.Location
from
((select top 1 Text
from employeeLog
where EmployeeID = @employee_id
and ParameterID = @event_ID) as 'Event',
(select top 1 Text
from employeeLog
where EmployeeID = @employee_id
and ParameterID = @Location_ID) as'Location'
) as T
但正如戈登所说,我认为你不需要from子句谢谢戈登的回答,你说得对,我不需要from子句。我想知道是否有比我写的更好的方法,所以我稍微改变了我的问题。