Geeks With Blogs
Guilherme Cardoso, Blog EN .NET geek July 2011 Entries
TSQL - Where clause with nullable variables
This tip is really simple but usefull if we're concatenating strings to write a Where clause. For example: DECLARE NVARCHAR @SQL = 'SELECT ... FROM ...' IF(@IDAluno IS NOT NULL) SET @SQL = @SQL + '' WHERE [IDAluno] = ' + CAST(@IDAluno AS NVARCHAR) A better solution would be something like this: SELECT ... FROM ... WHERE ([IDAluno] = @IDAluno OR @IDAluno IS NULL) A special thanks to Paulo Moreira for the advice ;) ......

Posted On Wednesday, July 13, 2011 12:15 PM

Copyright © Guilherme Cardoso | Powered by: GeeksWithBlogs.net