简体   繁体   中英

What is wrong with the following T-SQL query

I need to write a query against an EMP table that returns the EMPNO, ENAME and JOB values for Employees with the jobs SALESMAN and MANAGER using a Subquery in the WHERE clause.

I have this, but am getting the error "too many values". I'm not sure where the extra values are. Any help I can get with this, would be appreciated.

SELECT *
FROM SCOTT.emp
WHERE SCOTT.emp.job=(SELECT SCOTT.emp.empno, SCOTT.emp.ename, SCOTT.emp.job
                     FROM SCOTT.emp 
                     WHERE SCOTT.emp.job="SALESMAN" OR SCOTT.emp.job="MANAGER");
SELECT *
FROM SCOTT.emp
WHERE SCOTT.emp.job in(SELECT  SCOTT.emp.job
                 FROM SCOTT.emp 
                 WHERE SCOTT.emp.job="SALESMAN" OR SCOTT.emp.job="MANAGER");

You are using = with a subquery. The subquery can return only one column -- SQL Server doesn't support tuples for such comparisons.

Doesn't this do what you want?

SELECT e.*
FROM SCOTT.emp e
WHERE e.job IN ('SALESMAN', 'MANAGER');

There is no need for a subquery in the WHERE clause. It is simply not necessary.

There are two options for it:

1.you can replace '=' with 'IN' as '='(equal to) only expect one value but in your case there are more than one value.

so your query should be:

SELECT *
FROM SCOTT.emp
WHERE SCOTT.emp.job IN (SELECT SCOTT.emp.empno, SCOTT.emp.ename, SCOTT.emp.job
                     FROM SCOTT.emp 
                     WHERE SCOTT.emp.job="SALESMAN" OR SCOTT.emp.job="MANAGER");

or as mentioned in someone's answer no need to use sub query in your case directly put

SELECT *
FROM SCOTT.emp 
WHERE SCOTT.emp.job in ("SALESMAN","MANAGER");
  1. If you want to use '=' then use it with OR.

    SELECT * FROM SCOTT.emp WHERE SCOTT.emp.job ="SALESMAN" OR SCOTT.emp.job ="MANAGER";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM