All syntax scalar_expression {| & lt; & gt; |! | & Gt; |! & Gt; | & lt; |! & Lt;} ALL (subquery) scalar_expression any valid expression. {| & Lt; & gt; |! | & Gt; |! & Gt; | & lt; | all syntax
Scalar_expression {= | <> |! = | >|>=|!> | <= |! <} ALL (subquery)
Scalar_expression
Any valid expression.
{=| <>|! = | >|>=|!> | <= |! <}
Comparison operator.
Subquery
Returns a subquery of a single result set. The data type of the returned column must be the same as that of scalar_expression.
Restricted SELECT statements, where the order by clause, COMPUTE clause, and INTO keywords are not allowed.
Instance
The following example creates a stored procedure to determine whether all components with the specified SalesOrderID in AdventureWorks2008R2 can be created within the specified number of days. This example uses a subquery to create a list of DaysToManufacture values for all components with a specific SalesOrderID, and then confirm that all DaysToManufacture values are within the specified number of days.
Copy
USE AdventureWorks2008R2;
GO
Create procedure DaysToBuild @ OrderID int, @ NumberOfDays int
AS
IF
@ NumberOfDays> = ALL
(
SELECT DaysToManufacture
FROM Sales. SalesOrderDetail
JOIN Production. Product
ON Sales. SalesOrderDetail. ProductID = Production. Product. ProductID
WHERE SalesOrderID = @ OrderID
)
PRINT 'all items for this order can be manufactured in specified number of days or less .'
ELSE
PRINT 'Some items for this order cannot be manufactured in specified number of days or less .';
The following shows a complete instance.
1> create table employee (
2> ID int,
3> name nvarchar (10 ),
4> salary int)
5> GO
1>
2> create table job (
3> ID int,
4> title nvarchar (10 ),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, 'jason, 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, 'Robert ', 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, 'celia ', 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, 'linda ', 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, 'David', 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, 'James ', 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, 'alison, 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, 'chris ', 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, 'Mary ', 2345)
2> GO
(1 rows affected)
1>
2> insert into job (ID, title, averageSalary) values (1, 'developer', 3000)
3> GO
(1 rows affected)
1> insert into job (ID, title, averageSalary) values (2, 'tester', 4000)
2> GO
(1 rows affected)
1> insert into job (ID, title, averageSalary) values (3, 'designer ', 5000)
2> GO
(1 rows affected)
1> insert into job (ID, title, averageSalary) values (4, 'programmer ', 6000)
2> GO
(1 rows affected)
1>
2>
3> * from employee;
4> GO
ID name salary
--------------------------------
1, Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
Five David 7654
6 James 4567
7 Alison 8744
8. Chris 9875
9. Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary
----------------------------------
2 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2>
3> -- If your subquery returns a scalar value, you can use a comparison operator,
4>
5> SELECT e. ID, e. name
6> FROM Employee e
7> WHERE e. salary> ALL (SELECT averageSalary FROM job j)
8> GO
ID name
---------------------
5 David
7 Alison
8. Chris
(3 rows affected)
1>
2>
3> drop table employee;
4> drop table job;
5> GO
1>