I have the following dynamic query which is working fine without the WHERE
clause, which is expecting UNIQUEIDENTIFIER
.
When I pass it in,
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
DECLARE @ParmDefinition NVARCHAR(500)
SET @ParmDefinition = '@p_CreatedBy UNIQUEIDENTIFIER'
EXEC sp_executesql @sql, @ParmDefinition, @p_CreatedBy = @p_CreatedBy
I'm not sure if your variable is getting populated in string format or binary, but you may need to quote the uniqueidentifier in your where clause. If you just select the uniqueidentifier field, does it come back as string or binary?
Multiple parameter syntax. Maybe this will save someone an extra Google Search:
exec sp_executesql
@qry,
N'@value1 int, @value2 int, @currentValue int',
@value1 = @value1, @value2 = @value2, @currentValue = @currentValue