= (uguale a) (Transact-SQL)

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

Icona di collegamento a un argomento Convenzioni della sintassi Transact-SQL

Sintassi

expression = expression

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 di risultato

Boolean

Osservazioni

Per il confronto di due espressioni NULL il risultato dipende dall'impostazione ANSI_NULLS:

  • Se ANSI_NULLS è impostata su ON, il risultato è NULL, conformemente alla convenzione ANSI in base alla quale un valore NULL (o sconosciuto) non è uguale a un altro valore NULL o sconosciuto.

  • Se ANSI_NULLS è impostata su OFF, il risultato del confronto di due espressioni NULL è TRUE.

Il confronto di un valore NULL con un valore non NULL restituisce sempre il risultato FALSE. Per ulteriori informazioni, vedere SET ANSI_NULLS (Transact-SQL).

Esempi

A.Utilizzo 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".

USE AdventureWorks2012;
GO
SELECT DepartmentID, Name
FROM HumanResources.Department
WHERE GroupName = 'Manufacturing';

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. Viene inoltre illustrato 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;

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)

Vedere anche

Riferimento

Tipi di dati (Transact-SQL)

Espressioni (Transact-SQL)

Operatori (Transact-SQL)