|
Hello, First timer here. I'm having an issue when I use a date variable to set the 'between dates'. The query works when when I replace the variable w/ static dates. I get all rows returned when I use the variable and only three when I use the static dates. Could someone pls explain what I'm doing wrong? thanks!!! Brian
(comments are locked)
|
|
@maxdate is declared as variable to capture the output of the cursor to, so on the invocation of the cursor,
EDIT: added solution following on from OP's comments below Set up some sample data
now for the code
Explanation: No guarantee that this is the most efficient solution, I wanted to lay it out almost step by step to show you the method, rather than give a whizz-bang solution [Note: this is similar in a sense to the DW stumper puzzle that Joe Celko posted at Simple Talk and subsequently discussed over on the SSC main site - I urge you to check out that article and the solutions provided.] Sorry walked in late to the party....that's what you get from starting to write an answer, then being distracted by real work!
Feb 18 '10 at 01:25 PM
Kev Riley ♦♦
thank you, I understand.. but I don't understand how to set the @maxdate in my select to each @maxdate that is returned by the fetch. ...I'll keep working. thanks again.
Feb 18 '10 at 01:26 PM
bvolk
you can't use a variable set by the cursor to define the cursor - what is the real requirement?
Feb 18 '10 at 01:32 PM
Kev Riley ♦♦
Are you getting quirky updates and cursors muddled?
Feb 18 '10 at 02:11 PM
Ian Roke
hi, sorry for the delay (meeting). my requirement is to set each @maxdate to the max(invoicedate) for each item class. The rows that should be returned are the itemclasses that have not be sold in one year. Basically, a rolling 12 months rather than a hard coded date. thank you!
Feb 18 '10 at 03:33 PM
bvolk
(comments are locked)
|
|
Where do you use the date variable? Do you mean @maxdate? I can't see where you initialize @maxdate before using it, but maybe you missed it in your question? Otherwise your subquery will not return any rows since @maxdate is null. Anything compared to NULL is always false. If your subquery doesn't return any rows then the outer query will return every row. @maxdate is set to convert(char(10),max(f.invoicedate),21) by the cursor
Feb 18 '10 at 01:02 PM
Tom Staab
Yes, but the first fetch will return every row, wouldn't it? The variable is used before it is initialized.
Feb 18 '10 at 01:04 PM
Håkan Winther
Ah! Yes. Very good. In fact, I decided to give you +1 for the answer and the additional comment. Great catch.
Feb 18 '10 at 01:07 PM
Tom Staab
Can I get a 1/2 point? ;) I think I was correct about @maxdate being null, but your reason for that seems much more likely than mine. :)
Feb 18 '10 at 01:08 PM
Tom Staab
I'll give you a point! :)
Feb 18 '10 at 01:15 PM
Håkan Winther
(comments are locked)
|
|
If max(f.invoicedate) is null, @maxdate will be null. Therefore a "NOT EXISTS" check on a date comparison of null will always be true, and you will get all rows.
(comments are locked)
|

