= (uguale a) (Transact-SQL)

Si applica a:SQL Server database SQL di Azure Istanza gestita di SQL di Azure Azure Synapse Analytics AnalyticsPlatform System (PDW)SQL analytics endpoint in Microsoft FabricWarehouse in Microsoft Fabric

Esegue un confronto per determinare se due espressioni sono uguali (operatore di confronto) in SQL Server.

Convenzioni di sintassi Transact-SQL

Sintassi

expression = expression  

Nota

Per visualizzare la sintassi Transact-SQL per SQL Server 2014 (12.x) e versioni precedenti, vedere la documentazione delle versioni precedenti.

Argomenti

expression
Qualsiasi espressione valida. Se il tipo di dati delle espressioni non è uguale, è necessario che il tipo di dati di un'espressione possa essere convertito in modo implicito nel tipo di dati dell'altra. La conversione dipende dalle regole di precedenza dei tipi di dati.

Tipi restituiti

Boolean

Osservazioni:

Se si esegue il confronto usando un'espressione NULL, il risultato dipende dall'impostazione ANSI_NULLS:

  • Se ANSI_NULLS è impostata su ON, il risultato di qualsiasi confronto con NULL è UNKNOWN, in base alla convenzione ANSI secondo la quale NULL è un valore sconosciuto che non può essere confrontato con nessun altro valore, neanche con altri valori NULL.

  • Se ANSI_NULLS è impostata su OFF, il risultato del confronto tra NULL a NULL è TRUE e il risultato del confronto tra NULL e qualsiasi altro valore è FALSE.

Per altre informazioni, vedere SET ANSI_NULLS (Transact-SQL).

Un'espressione booleana con risultato UNKNOWN si comporta in modo simile a FALSE nella maggior parte dei casi, ma non in tutti. Per altre informazioni, vedere NULL e UNKNOWN (Transact-SQL) e NOT (Transact-SQL).

Esempi

R. Uso di = in una query semplice

Nell'esempio seguente l'operatore Uguale a viene utilizzato per restituire tutte le righe della tabella HumanResources.Department in cui il valore della colonna GroupName è uguale alla parola "Manufacturing".

-- Uses AdventureWorks  
  
SELECT DepartmentID, Name  
FROM HumanResources.Department  
WHERE GroupName = 'Manufacturing';  

Questo è il set di risultati.

  
DepartmentID Name  
------------ --------------------------------------------------  
7            Production  
8            Production Control  
  
(2 row(s) affected)  
  

B. Confronto tra valori NULL e non NULL

Nell'esempio seguente vengono utilizzati gli operatori di confronto Uguale a (=) e Diverso da (<>) per confrontare i valori NULL e non Null di una tabella. L'esempio evidenzia anche il fatto che l'impostazione dell'opzione SET ANSI_NULLS non influisce su IS NULL.

-- 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;  

Questo è il set di risultati.

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)  
  

Vedi anche

Tipi di dati (Transact-SQL)
Espressioni (Transact-SQL)
Operatori (Transact-SQL)