20

我有以下动态查询,它在没有WHERE预期的子句的情况下工作正常UNIQUEIDENTIFIER

当我传递它时,我没有得到结果。我试过CASTand 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, Subject, 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
4

4 回答 4

27

您必须将参数传递给 sp_executesql。有关详细信息,请参阅 MSDN

...
 WHERE 
    CreatedBy = @p
...

EXECUTE sp_executesql @sql, N'@p UNIQUEIDENTIFIER', @p = @p_CreatedBY
于 2009-06-24T07:15:00.970 回答
8

多参数语法。也许这会为某人节省额外的 Google 搜索:

exec sp_executesql 
    @qry, 
    N'@value1 int, @value2 int, @currentValue int', 
    @value1 = @value1, @value2 = @value2, @currentValue = @currentValue
于 2018-10-15T13:49:20.987 回答
4
DECLARE @ParmDefinition NVARCHAR(500)
SET @ParmDefinition = '@p_CreatedBy UNIQUEIDENTIFIER'

EXEC sp_executesql @sql, @ParmDefinition, @p_CreatedBy = @p_CreatedBy
于 2009-06-24T07:13:46.633 回答
0

我不确定您的变量是以字符串格式还是二进制填充,但您可能需要在 where 子句中引用 uniqueidentifier。如果您只选择 uniqueidentifier 字段,它会以字符串还是二进制形式返回?

于 2009-06-24T07:02:43.740 回答