I've done a lot of functions that return a table but somehow this one keeps on giving me an error at Begin
and I couldn't figure out for the life of me why. My where
statement is stored in a table. This function I want to pass in a value and the where statement.
CREATE FUNCTION dbo.Testtesttest(@employeeID AS INT,
@sqlWhereStatement AS VARCHAR(max))
Returns TABLE
BEGIN
DECLARE @mySQLStatement VARCHAR(max)
SET @mySQLStatement = 'Set Quoted_Identifier OFF Select '
+ CONVERT(VARCHAR, @employeeID) + ',* from (
select m.ManagerID, m.Name,m.Email,e.BU,
e.PSC from m inner join e on m.ManagerID = e.EmployeeID
group by m.ManagerID, m.Name,m.Email,e.BU,e.SC,
e.PSC) x where ' + @sqlWhereStatement
EXEC(@mySQLStatement)
RETURN
END