2011-02-11 49 views
1

我在SQL Server中有一个非常简单的查询。但它给错误。SQL Server内部查询问题

select * from(
select emp_name + ' ' + emp.surname as employee from ca_contact 
) 

此查询不起作用。 但是,当我写这样的下面,它是工作:

select emp_name + ' ' + emp.surname as employee from ca_contact 

回答

2

你会需要一个别名。在这种情况下foobar

select * from 
    (select emp_name + ' ' + emp.surname as employee from ca_contact) foobar 
1

我认为你需要指定表的别名 -

select * from(
select emp_name + ' ' + emp.surname as employee from ca_contact 
) t1 
+0

谢谢。我通常在Oracle上编写代码,因此我无法考虑放置表别名。 – user125687 2011-02-11 14:28:08

0

在SQL Server中,所有派生表必须有一个别名[例外是,如果没有选择从他们什么,例如在IN/EXISTS条款]中。对于SQL Server 2005以后的工作,另一种方法是使用公用表表达式,这在偶然的情况下也可用于最新版本的Oracle。

一个简单意义的别名

select * from 
    (select emp_name + ' ' + surname as employee from ca_contact) [ ] 

一个公共表表达式,也以在Sametime

;WITH [ ](employee) AS (
    select emp_name + ' ' + surname 
    from ca_contact) 
select * from [ ] 

FWIW命名列,可以省略CTE列名和查询

得到他们
WITH [ ] AS (
    select emp_name + ' ' + surname as employee 
    from ca_contact) 
select * from [ ] 

注意:不知道你怎么能有emp.surname,因为在您的查询中没有定义表/别名emp

0

请尝试以下查询

select employee from (select emp_name + ' ' + emp.surname as employee from ca_contact) as test