= (Igual a) (Transact-SQL)= (Equals) (Transact-SQL)
Se aplica a:Applies to: SQL ServerSQL Server (todas las versiones admitidas)
SQL ServerSQL Server (all supported versions)
Azure SQL DatabaseAzure SQL Database
Azure SQL DatabaseAzure SQL Database
Instancia administrada de Azure SQLAzure SQL Managed Instance
Instancia administrada de Azure SQLAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Almacenamiento de datos paralelosParallel Data Warehouse
Almacenamiento de datos paralelosParallel Data Warehouse
SQL ServerSQL Server (todas las versiones admitidas)
SQL ServerSQL Server (all supported versions)
Azure SQL DatabaseAzure SQL Database
Azure SQL DatabaseAzure SQL Database
Instancia administrada de Azure SQLAzure SQL Managed Instance
Instancia administrada de Azure SQLAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Almacenamiento de datos paralelosParallel Data Warehouse
Almacenamiento de datos paralelosParallel Data Warehouse
Compara la igualdad de dos expresiones (un operador de comparación) en SQL ServerSQL Server.Compares the equality of two expressions (a comparison operator) in SQL ServerSQL Server.
Convenciones de sintaxis de Transact-SQL
Transact-SQL Syntax Conventions
SintaxisSyntax
expression = expression
Nota
Para ver la sintaxis de Transact-SQL para SQL Server 2014 y versiones anteriores, consulte Versiones anteriores de la documentación.To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
ArgumentosArguments
expressionexpression
Es cualquier expresión válida.Is any valid expression. Si las expresiones no tienen el mismo tipo de datos, el tipo de datos para una expresión debe ser convertible de forma implícita al tipo de datos de la otra expresión.If the expressions are not of the same data type, the data type for one expression must be implicitly convertible to the data type of the other. La conversión se basa en las reglas de prioridad de tipo de datos.The conversion is based on the rules of data type precedence.
Tipos de resultadoResult Types
BooleanBoolean
ComentariosRemarks
Al comparar mediante una expresión NULL, el resultado depende de la configuración de ANSI_NULLS
:When you compare using a NULL expression, the result depends on the ANSI_NULLS
setting:
Si
ANSI_NULLS
se establece en ON, el resultado de las comparaciones con NULL es UNKNOWN, según la convención ANSI de que NULL es un valor desconocido y no se puede comparar con ningún otro, incluidos otros valores NULL.IfANSI_NULLS
is set to ON, the result of any comparison with NULL is UNKNOWN, following the ANSI convention that NULL is an unknown value and cannot be compared with any other value, including other NULLs.Si
ANSI_NULLS
se establece en OFF, el resultado de la comparación de NULL con NULL es TRUE, y el resultado de la comparación de NULL con cualquier otro valor es FALSE.IfANSI_NULLS
is set to OFF, the result of comparing NULL to NULL is TRUE, and the result of comparing NULL to any other value is FALSE.
Para obtener más información, vea SET ANSI_NULLS (Transact-SQL).For more information, see SET ANSI_NULLS (Transact-SQL).
Una expresión booleana que da como resultado UNKNOWN se comporta de forma similar en FALSE en la mayoría de los casos, aunque no en todos.A boolean expression resulting in UNKNOWN behaves similarly to FALSE in most, but not all cases. Vea NULL and UNKNOWN (Transact-SQL) y NOT (Transact-SQL) para obtener más información.See NULL and UNKNOWN (Transact-SQL) and NOT (Transact-SQL) for more information.
EjemplosExamples
A.A. Usar = en una consulta simpleUsing = in a simple query
En el ejemplo siguiente se usa el operador Es igual a para devolver todas las filas de la tabla HumanResources.Department
en las que el valor de la columna GroupName
es igual a la palabra 'Manufacturing'.The following example uses the Equals operator to return all rows in the HumanResources.Department
table in which the value in the GroupName
column is equal to the word 'Manufacturing'.
-- Uses AdventureWorks
SELECT DepartmentID, Name
FROM HumanResources.Department
WHERE GroupName = 'Manufacturing';
El conjunto de resultados es el siguiente:Here is the result set.
DepartmentID Name
------------ --------------------------------------------------
7 Production
8 Production Control
(2 row(s) affected)
B.B. Comparar valores NULL y distintos de NULLComparing NULL and non-NULL values
En el ejemplo siguiente se utilizan los operadores de comparación Es igual a (=
) y No es igual a (<>
) para realizar comparaciones con valores NULL
y distintos de NULL en una tabla.The following example uses the Equals (=
) and Not Equal To (<>
) comparison operators to make comparisons with NULL
and nonnull values in a table. En este ejemplo también se muestra que IS NULL
no se ve afectado por el valor SET ANSI_NULLS
.The example also shows that IS NULL
is not affected by the SET ANSI_NULLS
setting.
-- Create table t1 and insert 3 rows.
CREATE TABLE dbo.t1 (a INT NULL);
INSERT INTO dbo.t1 VALUES (NULL),(0),(1);
GO
-- Print message and perform SELECT statements.
PRINT 'Testing default setting';
DECLARE @varname int;
SET @varname = NULL;
SELECT a
FROM t1
WHERE a = @varname;
SELECT a
FROM t1
WHERE a <> @varname;
SELECT a
FROM t1
WHERE a IS NULL;
GO
-- SET ANSI_NULLS to ON and test.
PRINT 'Testing ANSI_NULLS ON';
SET ANSI_NULLS ON;
GO
DECLARE @varname int;
SET @varname = NULL
SELECT a
FROM t1
WHERE a = @varname;
SELECT a
FROM t1
WHERE a <> @varname;
SELECT a
FROM t1
WHERE a IS NULL;
GO
-- SET ANSI_NULLS to OFF and test.
PRINT 'Testing SET ANSI_NULLS OFF';
SET ANSI_NULLS OFF;
GO
DECLARE @varname int;
SET @varname = NULL;
SELECT a
FROM t1
WHERE a = @varname;
SELECT a
FROM t1
WHERE a <> @varname;
SELECT a
FROM t1
WHERE a IS NULL;
GO
-- Drop table t1.
DROP TABLE dbo.t1;
El conjunto de resultados es el siguiente:Here is the result set.
Testing default setting
a
-----------
NULL
(1 row(s) affected)
a
-----------
0
1
(2 row(s) affected)
a
-----------
NULL
(1 row(s) affected)
Testing ANSI_NULLS ON
a
-----------
(0 row(s) affected)
a
-----------
(0 row(s) affected)
a
-----------
NULL
(1 row(s) affected)
Testing SET ANSI_NULLS OFF
a
-----------
NULL
(1 row(s) affected)
a
-----------
0
1
(2 row(s) affected)
a
-----------
NULL
(1 row(s) affected)
Consulte tambiénSee Also
Tipos de datos (Transact-SQL) Data Types (Transact-SQL)
Expresiones (Transact-SQL) Expressions (Transact-SQL)
Operadores (Transact-SQL)Operators (Transact-SQL)