Ver Mensaje Individual
  #2 (permalink)  
Antiguo 30/10/2012, 09:09
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: Como pasar parametros al operador IN (SQL)

A que no puedes pasar parametros en el in de esa manera deberias de hacer una funcion algo como esto:

Código SQL:
Ver original
  1. /****** Object:  UserDefinedFunction [dbo].[SplitApplication]    Script Date: 10/19/2010 15:12:35 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8. CREATE FUNCTION [dbo].[SplitApplication](@applicationid VARCHAR(2000))
  9.  
  10. RETURNS @RtnValue TABLE
  11. (
  12. Id INT IDENTITY(1,1),
  13. DATA nvarchar(100)
  14. )
  15. AS
  16. BEGIN
  17. DECLARE @Cnt INT, @SplitOn nvarchar(5)
  18. SET @SplitOn = ','
  19. SET @Cnt = 1
  20.  
  21. While (Charindex(@SplitOn,@applicationid)>0)
  22. BEGIN
  23. INSERT INTO @RtnValue (DATA)
  24. SELECT
  25. DATA = ltrim(rtrim(SUBSTRING(@applicationid,1,Charindex(@SplitOn,@applicationid)-1)))
  26.  
  27. SET @applicationid = SUBSTRING(@applicationid,Charindex(@SplitOn,@applicationid)+1,len(@applicationid))
  28. SET @Cnt = @Cnt + 1
  29. END
  30.  
  31. INSERT INTO @RtnValue (DATA)
  32. SELECT DATA = ltrim(rtrim(@applicationid))
  33.  
  34. RETURN
  35. END
  36.  
  37. GO
  38.  
  39.  
  40. how TO USE :
  41.  
  42. SELECT * FROM TABLE WHERE DATA IN (
  43.  
  44. SELECT * FROM SplitApplication('1,1,1,1')
  45. )


saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me