it looks like there is an extra minus sign in your query at whereisnull(h.ldatuitdienst, getdate()+1) > pd.eddatum - @DaysMinus -
Other than that with out some sample data it will be hard to dianose what the issue is.
The other issue is that i may look at hard coding the @DaysMinus or concantenate that value in to the dynamic sql query instead of declaring a variable in the dynamic SQL
For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]
Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
Jeff Moden's Cross tab and Pivots Part 1[/url]
Jeff Moden's Cross tab and Pivots Part 2[/url]