title: Syntax error in sp_executesql
body: <p>Hi I am trying to run the following SQL which will pull out table names named SourceDestination by iterating through all databases whose name has 'Pull'.</p>
  
  <p>But I am getting an error at the plus sign near '+@db_name+'.sys.tables.  I tried N'' on both sides but I can't seem to get it to work.</p>
  
  <p>It gives this error 
  Msg 102, Level 15, State 1, Line 20
  Incorrect syntax near '+'.</p>
  
  <p>Need to know where I am wrong. Thanks for the help.</p>
  
  <pre><code>declare db_names cursor for
  select name
  from master.sys.databases
  where name like 'Pull_%'
  
  declare @db_name varchar(50)
  declare @table_name varchar(50)
  declare @sql nvarchar(100)
  DECLARE @ParmDefinition NVARCHAR(500);
  open db_names
  
  fetch next from db_names into @db_name
  
  while @@FETCH_STATUS = 0
  begin
  print @db_name  
  
      --  set @sql = 'select '+@table_name+'=name from '+@db_name+'.sys.tables'
      --  set @sql = N'select @table_name=name from @db_name.sys.tables where name =    ''SourceDestinations'' '
  execute sp_executesql N'select @tbl_name=name from '+@db_name+'.sys.tables where name = ''SourceDestinations'' ', N'@tbl_name varchar(50) OUTPUT', @tbl_name=@table_name OUTPUT
  --exec(@sql)o
  print @table_name
  FETCH NEXT FROM db_names INTO @db_name
      end
  
      close db_names
      deallocate db_names
  </code></pre>