2017-02-14 58 views
0

は申し訳ありませんが、私はタイトルにそれを置く方法がわからない、私はSQLのパラメータに基づいてクエリを変更する方法

select utenti.nome as tecnico, Richieste.IDRic as idchia, richieste.descr as rdescr, ISNULL(richieste.assistremota,0) as assremota, Clienti.RagSociale as ragsoc, richieste.descr as descr, richieste.priorita as prior, richieste.tipo as tipo, richieste.rforologio as rforo, ISNULL(statoric.appuntamento,0) as app, ISNULL(statoric.oradalle,0) as dalle, ISNULL(statoric.oraalle,0) as alle, statoric.ID as idstato 
from clienti 
inner join richieste on clienti.idcliente = richieste.rfcliente 
inner join statoric on statoric.rfric = richieste.idric 
inner join stati on stati.idstato = statoric.rfstato 
inner join utenti on utenti.idutente=statoric.rftecnico 
where statoric.attuale = 1 and statoric.rfstato < 14 and statoric.dataass = @data and statoric.rftecnico = 8 order by app desc, oraalle asc, prior desc 

I(のデータとDATA2を言わせて)2 datepickersでaspxページにこのクエリを持っていますこれを行うには、「statoric.dataass = @data」の部分(擬似コード)を変更する必要があります。

if data 2 is null then 
    "statoric.dataass = @data" 
else 
    "statoric.dataass between @data and @data2" 
end if 

は、どのように私はそれを行うことができますか?私はケースを試してみましたが、もし私が何か間違ったことをやっている...おかげ

+0

また、TIME部分を使用すると問題が発生する可能性があります。 – jean

+0

これは機能しません。また、data2より前のレコードが返されます。 –

+0

BETWEENはdata1 <= datass <= data2と等価ですので、data1 <= datass AND(data2はヌルOR datass <= data2) – jean

答えて

0

はここ

... statoric.dataass between @data and coalesce(@data2, @data) ... 
+0

とても簡単です、ありがとう! null値または空値の両方を持つことができるので、@dataとcoalesce(NULLIF(@ data2、 '')、@data)の間のこの固定小数点データのように少し改良しました –

+0

これはASP.NETでこれを使用している問題があります。 SQL Serverとは違った解釈をしているようです。 SQL Server上でデータと結合(NULLIF(data2、 '')、データ)の間でstatoric.dataassを使ってクエリを実行すると、data2 = nullの場合でも正しい結果が得られますが、asp.netでは何も返しません私はdata2を選択すると動作します)。何かヒント? –

+0

http://stackoverflow.com/questions/4555935/assign-null-to-a-sqlparameterたとえば – Serg

0

を合体してみ一つの方法である:

where statoric.attuale = 1 and 
     statoric.rfstato < 14 and 
     ((@data2 is null and statoric.dataass = @data) or 
     statoric.dataass between @data and @data2 
    ) and 
     statoric.rftecnico = 8 

しかし、あなたはこれを検討するかもしれません

where statoric.attuale = 1 and 
     statoric.rfstato < 14 and 
     statoric.dataass >= @data and 
     statoric.dataass <= coalesce(@data2, @data) and 
     statoric.rftecnico = 8 

このアプローチの利点は、(attuale, rftecnico, dataass)のインデックスを利用できることです。

+0

を参照してください。これはASP.NETでこれを使用すると問題が発生しますが、SQL Serverとは異なる解釈をしているようです。 SQL Serverでdata2 = nullのクエリを実行すると、正しい結果が得られます。代わりにasp.netには何も返されません(ただし、data2を選択した場合は動作します)。何かヒント? @ GabrieleCozzolino。 –

+0

。 。なぜそれが問題になるのか理解できません。 '@ data'を' NULL'にできますか?あなたの質問は、そうかもしれないと示唆していません。 –

+0

ページに2つの日付ピッカーがあり、クエリはそれぞれの日付の変更によってトリガーされるため、ユーザーがdata2を最初に選択するとデータはnullになり、逆も同様です –

関連する問題