Safe deleting in SQL Server
I am trying to delete table rows using custom table valued function in where clause.
Like this :
DELETE FROM TABLE WHERE ID NOT IN(SELECT ID FROM MYFUNCTION(DATE));
My function selects records from tables with some unions. It intended that this function must return some rows. I had no problems with it. Until one case where all rows have been deleted. After investigation i came to conclusio开发者_JAVA百科n that function should return something. Are there any cases when function could return nothing e.g. When SQL Server is performing some tasks (Backup, Any validation, restoring any other databases)
The last test showed that if pseudo function returns any NULL
or NULL with results e.g. (1,NULL,5)
absolutely nothing gets deleted.
I am using Microsoft SQL Server 2008.
Are there any cases when function could return nothing e.g. When SQL Server is performing some tasks (Backup, Any validation, restoring any other databases)
Although it's possible that any dbms might do something like that, changing behavior because other tasks are running would be a huge, flaming, critical bug. It would be so widely publicized it would be hard to miss the news.
Have you ever heard the saying, "When you hear hoofbeats, think 'horses', not 'zebras'"? It's far more likely that the hoofbeats you hear are a bug in your code than a bug in their code.
My guesses would be a misplaced paren, an isnull() test that returns something you didn't expect, unexpected NULL propagation, or an error in date arithmetic. You seem to have a lot of nullable columns.
This expression looks suspicious.
Dateadd(DAY,-2,Dateadd(MONTH,1,@prad)
精彩评论