I am trying to make this function so that I can use it in another stored procedure, it basically returns one value a number (I'm converting an VBA function from an Access MDB into this function so that I can use it in an Access ADP--where I can't use VBA function is queries now.Server: Msg 156, Level 15, State 1, Procedure FN_QCntr, Line 17Incorrect syntax near the keyword 'If'.Server: Msg 156, Level 15, State 1, Procedure FN_QCntr, Line 18Incorrect syntax near the keyword 'IF'.Server: Msg 170, Level 15, State 1, Procedure FN_QCntr, Line 19Line 19: Incorrect syntax near '@Cntr'.Server: Msg 156, Level 15, State 1, Procedure FN_QCntr, Line 28Incorrect syntax near the keyword 'If'.Server: Msg 156, Level 15, State 1, Procedure FN_QCntr, Line 29Incorrect syntax near the keyword 'IF'.Server: Msg 170, Level 15, State 1, Procedure FN_QCntr, Line 30Line 30: Incorrect syntax near '@Cntr'.Server: Msg 1075, Level 15, State 1, Procedure FN_QCntr, Line 50RETURN statements in scalar valued functions must include an argument.
Here is what I have so far:CREATE FUNCTION "FN_QCntr"/*QCntr(x, Optional Y As Long) As Long: X will be like E152 and Y will either be 1 or 2*/(@X nvarchar(50), @Y int)RETURNS int /* datatype */ASBEGIN /* sql statement ... */DECLARE @Cntr as int, @QCntr as int, @MaxItem as intset @Cntr = 0set @MaxItem = Go SELECT MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItemsIf Y=1 (If @MaxItem IS NOT NULL (IF @Cntr>@MaxItem @Cntr=@Cntr+1 @QCntr=@Cntr Else @Cntr>@MaxItem @Cntr=@Cntr+1 @QCntr=@Cntr End) END)Else IF Y=2 (If @MaxItem IS NOT NULL (IF @Cntr>@MaxItem @Cntr=@Cntr+1 @QCntr=@Cntr Else @Cntr>@MaxItem @Cntr=@Cntr+1 @QCntr=@Cntr End) END) Else @Cntr = @Cntr + 1 @QCntr = @Cntr EndElse @Cntr = @Cntr + 1 @QCntr = @CntrEnd ) RETURN /* value */END
Am I doing the IFs correct? Or what?===================Michael