Hi there.I've two Statements. The first example runs fast, the second runs slow. Is anyone of you able to explain why?SELECT convert(varchar(20),[TAT Werktage]) as [TAT Werktage], SUM(Menge) AS AnzahlFROM v_bla_bla_blaWHERE (datum >= CONVERT(DATETIME, '2007-01-01', 102) AND datum < CONVERT(DATETIME, '2007-01-13', 102)) AND ([Versand Nummer] IS NOT NULL) and ([TAT Werktage] <= 9)GROUP BY [TAT Werktage]
--------------------------------------------------------------------declare @start varchar(20), @ende varchar(20)set @start = '2007-01-01'set @ende = '2007-01-13'SELECT convert(varchar(20),[TAT Werktage]) as [TAT Werktage], SUM(Menge) AS AnzahlFROM v_bla_bla_blaWHERE (datum >= CONVERT(DATETIME, @start, 102) AND datum < CONVERT(DATETIME, @ende, 102)) AND ([Versand Nummer] IS NOT NULL) and ([TAT Werktage] <= 9)GROUP BY [TAT Werktage]
In the first example the date is queried via static text.In the second example the date is queried by two variables. A solution would be to create the sql text dynamically and execute the whole string -> exec (@sqlstring). You have to fight with quotes (') then. But my question is, why the first version (static text) is that much more fast.The field 'datum' is indexed of course. Thanks for your time + patience.Kind regards