Fórum Erro em sql dinâmica. #182828
18/09/2003
0
var
sql: string;
i: integer;
begin
sql:=´Select * From Cliente where´;
If Length(Tipo.Text) > 0
then Sql := Sql + ´ TIPO = ´´+Tipo.Text+´´ AND´;
If (Length(Data5.Text)) and (Length(Data6.Text)) > 0
then Sql := Sql + ´ Data between ´´+data5.Text+´´ AND ´´+data6.Text+´´´;
If Copy(Sql,Length(Sql)-2,3) = ´AND´
Then Delete(Sql,Length(Sql)-2,3);
If Copy(Sql,Length(Sql)-4,5) = ´WHERE´
Then Delete(Sql,Length(Sql)-4,5);
Cliente.SQL.Text:=sql;
Cliente.Active:=True;
sem a linha do between, ela funciona bem.
Paulo
Curtir tópico
+ 0Posts
18/09/2003
Motta
No Oracle seria algo assim
data between to_date(´01-sep-03´,´dd-mmm-yy´) and
to_date(´18-sep-03´,´dd-mmm-yy´)
ou seja vc terá de formatar a data
outra solucao
var
sql: string;
i: integer;
begin
sql:=´Select * From Cliente where´;
If Length(Tipo.Text) > 0
then Sql := Sql + ´ TIPO = ´´+Tipo.Text+´´ AND´;
If (Length(Data5.Text)) and (Length(Data6.Text)) > 0
then Sql := Sql + ´ Data between ´´+´:d1´+´´ AND ´´+´:d2´+´´´;
If Copy(Sql,Length(Sql)-2,3) = ´AND´
Then Delete(Sql,Length(Sql)-2,3);
If Copy(Sql,Length(Sql)-4,5) = ´WHERE´
Then Delete(Sql,Length(Sql)-4,5);
Cliente.SQL.Text:=sql;
cliente.params[0].AsDateTime := strtodate(edit1.text);
cliente.params[1].AsDateTime := strtodate(edit2.text);
Cliente.Active:=True;
desde que o formato da data nos edits seja válido
Gostei + 0
18/09/2003
Ljr
var
sql: string;
i: integer;
Par: Boolean
begin
sql:=´Select * From Cliente where´;
If Length(Tipo.Text) > 0 then
Sql := Sql + ´ TIPO = ´´+Tipo.Text+´´ AND´;
{ If (Length(Data5.Text)) and (Length(Data6.Text)) > 0 then
Sql := Sql + ´ Data between ´´+data5.Text+´´ AND ´´+data6.Text+´´´;}
If (Length(Data5.Text)) and (Length(Data6.Text)) > 0 then
begin
Sql := Sql + ´ Data between :Dini AND :Dfim´;
Par:= True;
end;
If Copy(Sql,Length(Sql)-2,3) = ´AND´ Then
Delete(Sql,Length(Sql)-2,3);
If Copy(Sql,Length(Sql)-4,5) = ´WHERE´ Then
Delete(Sql,Length(Sql)-4,5);
Cliente.SQL.Text:=sql;
if Par then
begin
cliente.ParamByName(´DINI´).Value:= StrToDate(data5.Text);
cliente.ParamByName(´DFIM´).Value:= StrToDate(data6.Text);
end;
Cliente.Open;
end;
So nao testei, para ver o resultado.
Gostei + 0
18/09/2003
Paulo
Eu tenho dois campos:
1º - Uma combobox, onde eu entro com o tipo de imóvel a ser consultado
2º - Há dois maskedit, para formatar a data. A consulta deve-se fazer da seguinte forma:
Ou eu entro com todos os dados, ou eu entro só com o tipo de imóvel ou o período, e a consulta deve prosseguir. Eu tenho outra consulta semelhante a esta, só que com muito mais campos, como, Tipo, Localização, Bairro, Valor e etc.. e a consulta funciona, tanto eu preencher todos os campos ou somente um ou alguns deles(Query Dinâmica). É isto o que eu quero, só que com períodos de datas(BETWEEN).
Gostei + 0
18/09/2003
Motta
tente apenas formatar a data .
no meu exemplo tinha um furo , pois se o controle que tem a data está vazio iria passar nulo para o parametro.
costumo fazer isto formatando a data no sql p/ o Oracle
Gostei + 0
18/09/2003
Paulo
Gostei + 0
18/09/2003
Motta
SELECT *
FROM Orders
WHERE (SaleDate <= ´1/23/1998´)
tente formatar a data assim entre aspas duplas e deve funcionar
Gostei + 0
18/09/2003
Paulo
Gostei + 0
18/09/2003
Ljr
Gostei + 0
18/09/2003
Motta
query1.sql.add(´select *´);
query1.sql.add(´from tabela´);
// condicao obrig
query1.sql.add(´where campo1=12345´);
// condicao opcional
if edit1.text <> ´´ then
query1.sql.add(´ and campo2=´+edit1.text);
// outra opcional
if edit2.text <> ´´ then
query1.sql.add(´ and campo3<>´edit2.text);
só tome cuidado para que a sintaxe fique legal
Gostei + 0
19/09/2003
Adilsond
begin with Cliente do begin if Active then Close; SQL.Clear; SQL.Add(´select *´); SQL.Add(´from Cliente´); if Trim(Tipo.Text) <> ´´ then SQL.Add(´where tipo = ´ + QuotedStr(Tipo.Text)); if (Trim(Data5.Text) <> ´/ /´) and (Trim(Data6.Text) <> ´/ /´) then begin if SQL.Count = 2 then SQL.Add(´where data between :dtini and :dtfin´) else SQL.Add(´ and data between :dtini and :dtfin´); ParamByName(´dtini´).AsDate := StrToDate(Data5.Text); ParamByName(´dtfin´).AsDate := StrToDate(Data6.Text); end else if (Trim(Data5.Text) <> ´/ /´) or (Trim(Data6.Text) <> ´/ /´) then begin if Trim(Data5.Text) = ´/ /´ then begin if SQL.Count = 2 then begin SQL.Add(´where (data is null´); SQL.Add(´ or data <= :dtfin)´); end else begin SQL.Add(´ and (data is null´); SQL.Add(´ or data <= :dtfin)´); end; ParamByName(´dtfin´).AsDate := StrToDate(Data6.Text); end else begin if SQL.Count = 2 then begin SQL.Add(´where (data is null´); SQL.Add(´ or data >= :dtini)´); end else begin SQL.Add(´ and (data is null´); SQL.Add(´ or data >= :dtini)´); end; ParamByName(´dtini´).AsDate := StrToDate(Data5.Text); end; end; Open; end; end;
Gostei + 0
20/09/2003
Paulo
1ª - Localização
2ª - Bairro
3ª - Condomínio
4ª - Valor
Então o cliente ele quer, usar as quatros, a consulta vem pelas quatro. Mas um outro cliente, por exemplo, só quer o valor e o bairro, e assim por diante, por isso o uso de query dinâmica. Comigo é semelhante, só que ao invés de um campo fixo, direto, eu uso um período de datas e o tipo de imóvel. Se os campos de Data(Data_Ini e Data_Fim) estiverem vazios, a pesquisa vem só pelo tipo, ou seja, a data é ignorada, o contrário é válido também.
Gostei + 0
21/09/2003
Adilsond
begin with Cliente do begin if Active then Close; SQL.Clear; SQL.Add(´select *´); SQL.Add(´from Cliente´); if Trim(Tipo.Text) <> ´´ then SQL.Add(´where tipo = ´ + QuotedStr(Tipo.Text)); if (Trim(Data5.Text) <> ´/ /´) and (Trim(Data6.Text) <> ´/ /´) then begin if SQL.Count = 2 then SQL.Add(´where data between :dtini and :dtfin´) else SQL.Add(´ and data between :dtini and :dtfin´); ParamByName(´dtini´).AsDate := StrToDate(Data5.Text); ParamByName(´dtfin´).AsDate := StrToDate(Data6.Text); end; Open; end; end;
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)