我有以下动态查询,没有WHERE子句正在工作,这是期待着UNIQUEIDENTIFIER.
当我通过它,我没有得到结果.我尝试了CAST和CONVERT,但没有结果.我可能做错了,有人可以帮忙吗?
CREATE PROCEDURE [dbo].[sp_Test1] /* 'b0da56dc-fc73-4c0e-85f7-541e3e8f249d' */ ( @p_CreatedBy UNIQUEIDENTIFIER ) AS DECLARE @sql NVARCHAR(4000) SET @sql =' DECLARE @p_CreatedBY UNIQUEIDENTIFIER SELECT DateTime,Subject,CreatedBy FROM ( SELECT DateTime,CreatedBy,ROW_NUMBER() OVER(ORDER BY DateTime ) AS Indexing FROM ComposeMail WHERE CreatedBy = @p_CreatedBy /* <--- the problem is in this condition */ ) AS NewDataTable ' EXEC sp_executesql @sql
解决方法
您必须将参数传递给sp_executesql.
See MSDN for details.
... WHERE CreatedBy = @p ... EXECUTE sp_executesql @sql,N'@p UNIQUEIDENTIFIER',@p = @p_CreatedBY