我有以下动态查询在没有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, 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
答案 0 :(得分:24)
您必须将参数传递给sp_executesql。 See MSDN for details
...
WHERE
CreatedBy = @p
...
EXECUTE sp_executesql @sql, N'@p UNIQUEIDENTIFIER', @p = @p_CreatedBY
答案 1 :(得分:3)
DECLARE @ParmDefinition NVARCHAR(500)
SET @ParmDefinition = '@p_CreatedBy UNIQUEIDENTIFIER'
EXEC sp_executesql @sql, @ParmDefinition, @p_CreatedBy = @p_CreatedBy
答案 2 :(得分:2)
多个参数语法。也许这样可以为某人节省额外的Google搜索:
exec sp_executesql
@qry,
N'@value1 int, @value2 int, @currentValue int',
@value1 = @value1, @value2 = @value2, @currentValue = @currentValue
答案 3 :(得分:0)
我不确定您的变量是以字符串格式还是二进制格式填充,但您可能需要在where子句中引用uniqueidentifier。如果您只选择uniqueidentifier字段,它是以字符串还是二进制形式返回?