Guilherme Cardoso, Blog EN

.NET geek

  Home  |   Contact  |   Syndication    |   Login
  21 Posts | 0 Stories | 21 Comments | 0 Trackbacks

News

Name: Guilherme Cardoso
Age: 19
Country: Portugal


pontonetpt

NetPonto

xaml pt

Twitter





Archives

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