Usuário com melhor resposta
Query com variaveis

Pergunta
-
Bom dia, pessoal
To precisando fazer uma query que tenha na condicao uma variavel tipo assim...
Select * from produtos
where codprod in (@variavel)
Sendo que na @variavel teria os valores: 1, 5, 6, 8, 10 etc
Tem jeito de fazer isso?
Valeu,
terça-feira, 3 de julho de 2007 14:02
Respostas
-
sim isso mesmo mais fica melhor se vc. usar um join
select * from produtos inner join dbo.Uf_splitString(@Variavel) As Tbl on Tbl.item = produtos.codprod
Abs;
terça-feira, 3 de julho de 2007 16:08
Todas as Respostas
-
o in nao funciona assim entao vc. pode ou concatenatr tudo e daar u exe na variavel ou usar uma funcao que retorne uma table veja este exemplo:
Create Function Uf_SplitString (@String Varchar(1000))
Returns @RetTableString Table (String Int)
As
Begin
Declare @TableString Table (String Int)
Declare @Start Int
Declare @End Int
Declare @Insert Varchar(30)
Select @Start = 1, @End = Len(@String+','), @Insert = '', @String = @String + ','
While @Start <= @End
Begin
If Substring(@String,@Start,1) = ','
Begin
Insert Into @TableString (String) Values (@Insert)
Set @Insert = ''
End
Else
Begin
Set @Insert = @Insert + Substring(@String,@Start,1)
End
Set @Start = @Start + 1
End
Insert Into @RetTableString (String) Select String From @TableString
Return
End
Create Table #Exemplo (Campo1 int)
Insert into #Exemplo (Campo1) Values (15)
Insert into #Exemplo (Campo1) Values (10)
Insert into #Exemplo (Campo1) Values (1)
Declare @StringSequencia Varchar(1000)
Set @StringSequencia = '10,20,30' -- pode ser um retorno de procedure ou passado via codigo mesmo.
Select * From #Exemplo
Inner Join (Select String From dbo.Uf_SplitString(@StringSequencia)) Tbl On Tbl.String = #Exemplo.Campo1terça-feira, 3 de julho de 2007 14:09 -
Ta dando alguns erros:
Server: Msg 156, Level 15, State 1, Procedure Uf_SplitString, Line 26
Incorrect syntax near the keyword 'Create'.
Server: Msg 2772, Level 16, State 1, Procedure Uf_SplitString, Line 26
Cannot access temporary tables from within a function.
Server: Msg 2772, Level 16, State 1, Procedure Uf_SplitString, Line 27
Cannot access temporary tables from within a function.
Server: Msg 2772, Level 16, State 1, Procedure Uf_SplitString, Line 28
Cannot access temporary tables from within a function.
Server: Msg 2772, Level 16, State 1, Procedure Uf_SplitString, Line 29
Cannot access temporary tables from within a function.
Server: Msg 2772, Level 16, State 1, Procedure Uf_SplitString, Line 35
Cannot access temporary tables from within a function.terça-feira, 3 de julho de 2007 14:16 -
antes de cada create coloque um go ou rode passo a passo cada parte do exemplo.
Abs;
terça-feira, 3 de julho de 2007 14:30 -
So p/ entender melhor... usando essa funcao consigo usa-la no condicao da query?
Algo tipo assim:
Select * from produtos
where codprod in (dbo.Uf_SplitString(@variavel))
terça-feira, 3 de julho de 2007 15:34 -
sim isso mesmo mais fica melhor se vc. usar um join
select * from produtos inner join dbo.Uf_splitString(@Variavel) As Tbl on Tbl.item = produtos.codprod
Abs;
terça-feira, 3 de julho de 2007 16:08