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.
| Author |
Topic |
|
anand_d
Starting Member
9 Posts |
Posted - 2006-07-26 : 11:18:52
|
| Hey guys,I am using the following code to implement cursorless cursor. The idea is to fetch all the dates from a given table and call another stored proc to execute something for each given date. The same is done by avoiding cursors in the following method. Wanted to know if this is an optimal solution:------------------------------------------------------------------DECLARE @CROW INT, @MAXROW INT, @CREATEDATE DATETIMEDECLARE @SQL NVARCHAR(1000)SET @CROW=1SELECT @MAXROW=COUNT(1) FROM MYDB..CREATEDATE_TOREFRESHSELECT @CROW, @MAXROWWHILE @CROW <= @MAXROWBEGIN SET @SQL=N'( SELECT @CREATEDATE=CONVERT(VARCHAR, MAX(CREATEDATE), 101) FROM MYDB..CREATEDATE_TOREFRESH WHERE CREATEDATE IN ( SELECT TOP '+ CONVERT(VARCHAR, @CROW, 101) + ' CREATEDATE FROM MYDB..CREATEDATE_TOREFRESH ) )' EXECUTE MASTER.[DBO].SP_EXECUTESQL @SQL, N'@CREATEDATE DATETIME OUTPUT', @CREATEDATE OUTPUT PRINT CONVERT(VARCHAR, @CREATEDATE, 101) SET @CROW=@CROW + 1END------------------------------------------------------------------ThanksAnand |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-26 : 11:26:28
|
| not sure what you are doing but considerdeclare @CREATEDATE DATETIME, @maxCREATEDATE DATETIMEselect @CREATEDATE = '19000101' , @maxCREATEDATE = max(CREATEDATE) FROM MYDB..CREATEDATE_TOREFRESHwhile @CREATEDATE < @maxCREATEDATEbeginselect @CREATEDATE = min(CREATEDATE) from MYDB..CREATEDATE_TOREFRESH where CREATEDATE > @CREATEDATEPRINT CONVERT(VARCHAR, @CREATEDATE, 101)end==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-07-26 : 11:28:56
|
| It doesn't have any advantage over using a cursor, and will likely be slower than a cursor.CODO ERGO SUM |
 |
|
|
anand_d
Starting Member
9 Posts |
Posted - 2006-07-26 : 12:56:59
|
| NR,Just Superb. This is effective coding. Now, I know that experts like you can easily resolve issues just like that. The alternate code worked perfectly.Yeah, lot of learning can be done with people like you in the forum.Again, thanks a ton.Regards,Anand |
 |
|
|
|
|
|
|
|