Can I create a One-Time-Use Function in a Script or Stored Procedure?

Mark Carpenter picture Mark Carpenter · Jun 11, 2009 · Viewed 112.6k times · Source

In SQL Server 2005, is there a concept of a one-time-use, or local function declared inside of a SQL script or Stored Procedure? I'd like to abstract away some complexity in a script I'm writing, but it would require being able to declare a function.

Just curious.

Answer

Ron Savage picture Ron Savage · Jun 11, 2009

You can create temp stored procedures like:

create procedure #mytemp as
begin
   select getdate() into #mytemptable;
end

in an SQL script, but not functions. You could have the proc store it's result in a temp table though, then use that information later in the script ..