Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Strange behaviour of getdate() ?

Author  Topic 

Daniel_Buchholz
Starting Member

28 Posts

Posted - 2002-04-19 : 08:28:04
Hi all!

I did some test procedure to find the best solution for a problem i have. (It´s how to get the exact datetime each row is read, no solution so far...)

This is a test that doesn´t quite behave as I expected:

declare c1 cursor for select col1, getdate() from table
open c1

waitfor delay '00:02:00'
fetch next from c1 into
@stest,
@tdate

print @stest
print cast(@tdate as varchar)


Here I expected to have in the @tdate variable the datetime of the procedure start plus two minutes. But in fact is has the datetime of the procedure start itself. The @stest variable has current data. I updated some col1 data during that 2 min break and it is (as I expected) fetched into the variable. So with this type of cursor data is read when 'fetch' occurs not when the cursor is opened. But it seems that the getdate() in the select statement is processed when the cursor is opened.

Strange... Or is it ok?

Daniel

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2002-04-19 : 08:33:16
no....getdate() is evaluated once in an SQL statement....



Try SELECT *, GETDATE() FROM ANYtable in Query Analyser.


you will see that the GETDATE() values are repeated on every row.


A cursor is no different.....it doesn't re-build it's record set as the record set is being traversed...it's like a photograph....it reflects a dataset at a point in time....ie when it is opened.

Go to Top of Page
   

- Advertisement -