i’m trying to wrap a simple function within an a check for existence why am i getting incorrect syntax?
updated:
GO
IF EXISTS (SELECT TOP 1 * FROM Customers)
BEGIN
USE [rstestDB]
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE FUNCTION [dbo].[udf_GetName]
(
@p1 nvarchar(25)
)
RETURNS varchar
AS
BEGIN
DECLARE @Result varchar(25)
SELECT @Result = 'John Doe'
RETURN @Result
END
END
GO
Getting following error:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword ‘FUNCTION’.
Msg 178, Level 15, State 1, Line 19
A RETURN statement with a return value cannot be used in this context.
GOis a batch separator.GO is a command recognized by the sqlcmd and osql utilities that send commands to the SQL engine, it’s not valid SQL.
Take out all the
GOs except for at the very end.A simpler example that will also fail:
All your settings should really be at the very beginning of the script since they will persist for the length of the session.