Can anyone help with the following? I'm trying to use a Cursor within a select statement, can it be done?My statementDeclare @i1 intDeclare @i2 intDeclare @d1 DATETIMEDeclare @t1 VARCHAR(10)SELECT a.persno,a.date1,a.code,daycount =(DECLARE Dcursor CURSOR FORSELECT DISTINCT b.persno,b.date1,b.code from table1 bWHERE b.persno = a.persnoAND b.date1 = a.date1AND b.code = a.codeORDER BY b.persnoOPEN DcursorFETCH NEXT FROM Dcursor INTO @i1,@d1,@t1WHILE @@FETCH_STATUS = 0BEGINIF @t1 = 'Y' @i2 = DATEDIFF(d,@d1,GETDATE())FETCH NEXT FROM Dcursor INTO @i1,@d1,@t1 END CLOSE Dcursor DEALLOCATE Dcursor RETURN @i2)into table2FROM table1
I'm getting an error :-Incorrect syntax near the keyword 'DECLARE'Any ideas as to how to get this working?