Here's one example:
declare @LockCount int, @WhereClause varchar(1000)
set @WhereClause = ' Projects.OwnerCode = 25 '
execute(' select @LockCount = count(*) from DBO.ProjectChangeID join Projects on Projects.ProjectID = ProjectChangeID.ProjectID join OwnerInformation on Projects.OwnerCode = OwnerInformation.OwnerCode where ProjectChangeID.LockFlag > 0 and ' + @WhereClause)
Resulting Error: Must declare the scalar variable "@LockCount".
I have tried variations on this theme, to no avail, (mostly syntax errors.) There's gotta be a way to do this. Suggestions? Thanks in advance, Joel