title: Use null result of exec sp_execute
body: <p>I have some dynamic SQL that I use to see if a certain property for a certain client is in a table. If so, it will return the value, if not, it will return null. However, I would like it to return a default value if the property is not found.</p>
  
  <p>The table:</p>
  
  <pre><code>CREATE TABLE [test].[customers](
      [customer] [nvarchar](256) NULL,
      [key_name] [nvarchar](256) NULL,
      [key_value] [nvarchar](256) NULL
  ) 
  GO
  
  
  INSERT INTO [test].[customers]
             ([customer]
             ,[key_name]
             ,[key_value])
       VALUES
             ('JohnDoe'
             ,'periodlength'
             ,'3')
  GO
  </code></pre>
  
  <p>The Dynamic SQL:</p>
  
  <pre><code>declare @customer as nvarchar(256) = 'JohnDoe'
  declare @table as nvarchar(256) = 'test.customers'
  
  declare @sql as nvarchar(4000)
  set @sql = 'select [key_value] from ' +  @table + ' where [key_name]= ''periodlength'' and customer= ''' + @customer + ''''
  exec sp_executesql @sql
  </code></pre>
  
  <p>So when Qqerying for John Doe you get result 3 which is perfect. However, I would like to reurn 1 for Jane Doe. So I was thinking along the lines 
  <code>IF exec sp_executesql @sql IS NULL 1 ELSE exec sp_executesql @sql</code>
  but that doesn't work.</p>
  
  <p>How to change my dynamic query so that is returns a default value if the property is not found?</p>