I have this simple code to define and call a UDF, which works when run in VS Code:
IF OBJECT_ID(N'fn_get_date', N'FN') IS NOT NULL
DROP FUNCTION fn_get_date
;
GO
CREATE FUNCTION fn_get_date ()
RETURNS DATE
BEGIN
RETURN CAST(GETDATE() AS DATE);
END;
GO
SELECT dbo.fn_get_date() 'Test';
But putting it into an Excel file throws this error:
(Transcription: Unable to connect We encountered an error while trying to connect. Details: "Microsoft SQL: Incorrect syntax near 'GO'. 'CREATE FUNCTION' must be the first statement in a query batch. A RETURN statement with a return value cannot be used in this context.")
What might be causing this, assuming it's even possible to do this somehow?