title: Microsoft t sql how to declare temporary variable within user defined function?
body: <p>So how can I declare a temporary local variable with declare like:<code>declare @tempVar varchar(max)</code> within a user declared function?
  The actual problem I'm trying to solve here is declaring and using variable within user defined stored function. I declare my function like this:</p>
  
  <pre><code>create function someFunction(@someParam varchar(100))
  returns table
  as 
  return (
      declare @tempvar varchar(100)
      set @tempvar = ''--Set the var to something useful
      select * from sometable where somecolumn = (
          select top 1 someColumn
          from sometable
          where somecolumn = @tempvar
      )
  )
  </code></pre>
  
  <p>Server complain about the variable being declared in wrong place. Where should I declare my variables in UDF like this?</p>