Límite en el LUGAR DONDE col EN ( ... ) condición


Estoy usando el siguiente código:

SELECT * FROM table
WHERE Col IN (123,123,222,....)

Sin embargo, si pongo más de ~3000 números en la cláusula IN, SQL arroja un error.

¿Alguien sabe si hay un límite de tamaño o algo similar?!!

Author: Jesse, 2009-07-01

6 answers

Dependiendo del motor de base de datos que esté utilizando, puede haber límites en la longitud de una instrucción.

SQL Server tiene un límite muy grande:

Http://msdn.microsoft.com/en-us/library/ms143432.aspx

ORACLE tiene un límite muy fácil de alcanzar en el otro lado.

Por lo tanto, para cláusulas IN grandes, es mejor crear una tabla temporal, insertar los valores y hacer una COMBINACIÓN. Funciona más rápido también.

 57
Author: tekBlues,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-28 13:45:36

Hay un límite, pero puedes dividir tus valores en bloques separados de in()

Select * 
From table 
Where Col IN (123,123,222,....)
or Col IN (456,878,888,....)
 30
Author: Iain Hoult,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-01 14:29:01

Lo estás haciendo mal.

Parametriza tu consulta y pasa los id usando un Parámetro de Valor de Tabla .

Por ejemplo, podría definir el siguiente tipo

CREATE TYPE IdTable AS TABLE (Id INT NOT NULL PRIMARY KEY)

Y tener el siguiente procedimiento almacenado

CREATE PROCEDURE sp__Procedure_Name
    @OrderIDs IdTable READONLY,
AS

    SELECT *
    FROM table
    WHERE Col IN (SELECT Id FROM @OrderIDs)
 8
Author: Greg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-16 15:03:53

Dependiendo de su versión, use un parámetro de valor de tabla en 2008, o algún enfoque descrito aquí:

Matrices y Listas en SQL Server 2005

 4
Author: A-K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-01 14:31:00

¿Por qué no hacer un dónde EN una sub-selección...

Pre-consulta en una tabla temporal o algo así...

CREATE TABLE SomeTempTable AS
    SELECT YourColumn
    FROM SomeTable
    WHERE UserPickedMultipleRecordsFromSomeListOrSomething

Entonces...

SELECT * FROM OtherTable
WHERE YourColumn IN ( SELECT YourColumn FROM SomeTempTable )
 3
Author: DRapp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-21 03:47:56

Puedes usar tuplas como esta: SELECCIONAR * DE LA tabla DONDE (Col, 1) EN ((123,1),(123,1),(222,1),....)

No hay restricciones en el número de estos. Compara pares.

 -2
Author: Lukasz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-10 07:35:48