I want to create a function, but only if it doesn't already exist. Reduced to its simplest form, that doesn't seem very hard at all; after all, I know how to check if it exists:1> IF (NOT EXISTS (SELECT id FROM sysobjects WHERE name = 'funct'))2> print 'Function does not exist.'3> goFunction does not exist.
...and I know how to create the function:1> create function funct ()2> returns real3> begin4> return 1.15> end6> go
...which works fine on its own. But combining the two working bits together generates a couple of seemingly irrelevant errors:1> IF (NOT EXISTS (SELECT id FROM sysobjects WHERE name = 'funct'))2> create function funct ()3> returns real4> begin5> return 1.16> end7> goMsg 156, Level 15, State 1, Server <server>, Line 2Incorrect syntax near the keyword 'function'.Msg 178, Level 15, State 1, Server <server>, Line 6A RETURN statement with a return value cannot be used in this context.
I'm confused; why can't I create my function inside an IF block?