title: use table name as parameter in sql function
body: <p>I want create function, which use table name as parameter. As I search I need use dynamic sql. I try such code:</p>
  
  <pre><code>CREATE FUNCTION get_column_id
      (
      @TableName VARCHAR(30),
      @ColumnName VARCHAR(30),
      )
  RETURNS int 
  AS
  BEGIN
  IF EXISTS 
      (
      DECLARE @SQL VARCHAR(50)
      SET @sql = 'SELECT' + @ColumnName + 'FROM' + @TableName + 'WHERE @ColumnName =    @ColumnNameValue';
      EXEC(@sql)
      )
  BEGIN
  </code></pre>
  
  <p>But get errors. Is where any way to procceed this?</p>
  
  <p>I try use dynamic sql in such way </p>
  
  <pre><code>DECLARE @SQL VARCHAR(50)
  SET @SQL = 'SELECT' + @ColumnName + 'FROM' + @Table + 'WHERE @ColumnName = @ColumnNameValue'
  EXEC(@SQL)
  DECLARE @TableName table (Name VARCHAR(30))
  INSERT INTO @TableName VALUES (@SQL)
  IF EXISTS 
      (SELECT Name FROM @TableName WHERE Name = @ColumnNameValue)
  </code></pre>
  
  <p>But get <code>Invalid use of a side-effecting operator 'EXECUTE STRING' within a function.</code>
  Does anyone knows how bypass this constraint?</p>