title: How to use conditional statement "if not exists" in dynamic sql query within stored procedure?
body: <p>I have something like this.</p>
  
  <pre><code>declare @insertQuery nvarchar(max)
  
  declare @ifQuery nvarchar(max)
  
  SET @insertQuery='...'
  
  SET @ifQuery='if not exists(select Id from '+@DbName+'.[dbo].[tbl_TestHere] where Id='+@id+')'
  
    BEGIN
      Exec sp_executesql @insertQuery
  END
  </code></pre>
  
  <p>I need to execute dynamic query @insertQuery if "if not exists(...)" returns true..But i couldn't find right solution for this.
  Thanks in advance</p>