title: How to fix query error with ambiguous column name in my example?
body: <p>Show error when use <code>@columnsname = ID</code> and <code>@columnsname = [address]</code> show return value 0 don't show data I want fix error help query.</p>
  
  <p>Query: </p>
  
  <pre><code>create procedure ProcedureName1 
      (@columnsname sysname, --varchar(50)
       @columnsvalue varchar(50)
      ) 
  as
  begin
      declare @sql nvarchar(max), @column_name sysname;
  
      /* make sure the @columnsname is a valid column name */
     set @column_name = (select c.name
                         from sys.columns c
                         where c.object_id = object_id(N'Tabl1') 
                           and c.name = @columnsname);
  
     set @sql = 'with cte as (
      select 
          ID
        , names
        , null as address
        , work
        , note
      from Tabl1
      where '+@column_name+' like @columnsvalue
      union all
      select 
          t2.ID
        , t2.name
        , t2.address
        , null
        , null as tt
      from Tabl2 as t2
      left join Tabl1 as t1 on t2.ID = t1.ID
      where '+@column_name+' like @columnsvalue
      )
    select *
    from cet
    order by 
        id
      , note desc
      , address;'
  exec sp_executesql @sql, N'@columnsvalue varchar(50)', @columnsvalue
  end;
  go
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>Msg 209, Level 16, State 1, Line 21<br>
    Ambiguous column name 'ID'</p>
  </blockquote>