Hello everyone-I'm trying to create a simple function that will do a count on a table. I want to pass the table name in form of a parameter to the variable and this function will return the count as an int. Please see my function below and help me understand why this is not working. CREATE FUNCTION count_rows (@tablename varchar(100)RETURNS int ASBEGINDECLARE @emp_count AS intdeclare @declaration varchar(100)@declaration='SELECT count(*) FROM ' + @tablename@emp_count=cast(@declaration as int)--dbo.TD_EmployeeProfile_FinalV2 RETURN @emp_count ENDGOThe errors I am getting are as follows:Msg 102, Level 15, State 1, Procedure count_rows, Line 3Incorrect syntax near 'RETURNS'.Msg 102, Level 15, State 1, Procedure count_rows, Line 10Incorrect syntax near '@declaration'.Msg 178, Level 15, State 1, Procedure count_rows, Line 14A RETURN statement with a return value cannot be used in this context.
↧