I am working on creating a table-valued function but before creating it I would like to check if it exists and if it doesn't exist then create the function using dynamic script and then alter it normally.
IF NOT EXISTS
(SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[region]')
AND OBJECTPROPERTY(id,N'IsTableFunction') = 1)
BEGIN
EXEC dbo.sp_executesql
@statement = N' CREATE FUNCTION [dbo].[region]()
RETURNS TABLE AS BEGIN RETURN ''A'' END'
GO
ALTER FUNCTION dbo.region(@dd datetime)
--
GO
But, above script threw me an error Incorrect syntax near the keyword 'RETURN'.
i think you have to write as follows:
and in case you want to return a variable table just to insert into it before returning it as a result, you can use the following,