I am having problems running sql server stored procedure. I using a dynamic sql. I get the error "Must declare the scalar variable "@EmployeeId".
SQL QUERY
ALTER PROCEDURE [dbo].[GetLeaveDays] -- Add the parameters for the stored
procedure here
@LeaveType varchar(5000),
@AdminId int,
@EmployeeId int
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@queryString nvarchar(MAX);
SET @queryString = 'SELECT ' + @LeaveType + ' FROM CompulsoryLeave WHERE
AdminId = @AdminId AND Id=(SELECT LeaveStructureId FROM Employee WHERE
Id=@EmployeeId)';
EXECUTE sp_executesql @queryString,
N'@AdminId int',
N'@EmployeeId int',
@AdminId = @AdminId,
@EmployeeId=@EmployeeId
END
@LeaveType. You might want to validate it against the columns in the table unless you need something more interesting, e.g. expressions. If it is a list of columns then you can split it and validate them individually.