You must pass in the parameters to sp_executesql. See MSDN for details.
...
WHERE
CreatedBy = @p
...
EXECUTE sp_executesql @sql, N'@p UNIQUEIDENTIFIER', @p = @p_CreatedBY
More Related Contents:
- Getting result of dynamic SQL into a variable for sql-server
- Dynamic SQL to generate column names?
- Select distinct record from table and perform Sum of column (Pallats, Gross) of a duplicate row. And show that duplicate rows once [closed]
- How do I split a string so I can access item x?
- When should I use CROSS APPLY over INNER JOIN?
- get a comma delimited string from rows [duplicate]
- Find all tables containing column with specified name – MS SQL Server
- SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, =
- How important is the order of columns in indexes?
- How to split a single column values to multiple column values?
- Why no windowed functions in where clauses?
- Using the result of an expression (e.g. Function call) in a stored procedure parameter list?
- T-SQL Cast versus Convert
- When no ‘Order by’ is specified, what order does a query choose for your record set?
- What is a stored procedure?
- How do I create a step in my SQL Server Agent Job which will run my SSIS package?
- T-SQL datetime rounded to nearest minute and nearest hours with using functions
- Partition Function COUNT() OVER possible using DISTINCT
- How do I create a foreign key in SQL Server?
- How do I group on continuous ranges
- join comma delimited data column
- Need to list all triggers in SQL Server database with table name and table’s schema
- Is it possible to perform multiple updates with a single UPDATE SQL statement?
- Incorrect syntax near the keyword ‘with’…previous statement must be terminated with a semicolon
- Alter user defined type in SQL Server
- Best equivalent for IsInteger in SQL Server
- Drop all tables whose names begin with a certain string
- T-SQL Skip Take Stored Procedure
- Why is RAND() not producing random numbers?
- Should every SQL Server foreign key have a matching index? [closed]