无法在 where 子句中获取变量来处理我的查询

Can't get variable in where clause to work with my query

我试图在我的 where 子句中使用一个变量,但无法让它工作。当我填写变量的实际值时,它起作用了。

我通过以下方式定义变量:

DECLARE @SalesorderTracking nvarchar(MAX)
SET @SalesorderTracking = ''; select @SalesorderTracking = CAST('''' + c_loadcarrier as Nvarchar(max)) + '''' + ','  + @SalesorderTracking from  [ApportMM].[dbo].[#SalesorderTracking]
SET @SalesorderTracking = LEFT(@SalesorderTracking, LEN(@SalesorderTracking) -1)
PRINT @SalesorderTracking

打印命令returns这个值:'PL10275703','PL10275700','PL10269031','PL10269030','PL10271642'

运行以下查询returns无记录。

SELECT  [MyDB].[dbo].[t_Item_log].[c_id]
        ,[MyDB].[dbo].[t_Item_log].[c_Info]
        ,CASE [c_TypeRef]
            WHEN '1' THEN 'Loadcarrier From'
            WHEN '2' THEN 'Loadcarrier To'
        END AS [Type]
        ,[c_loadcarrier]
        ,[c_WorkerName]
        ,[c_Material]
        ,[c_Qty]
        ,[c_Created]     
        ,[c_ShelfLabelFrom]
        ,[c_WarehouseFrom]
        ,[c_WareHouseAreaFrom]
        ,[c_ZoneFrom]
        ,[c_TransactionId]
        ,[c_ItemRef]
        FROM dbo.t_Item_log INNER JOIN dbo.t_Item_log_LoadCarrier ON dbo.t_Item_log.c_id = dbo.t_Item_log_LoadCarrier.c_Item_LogRef
        where c_loadcarrier in (@SalesorderTracking) AND (c_TypeRef = '1' OR c_TypeRef = '2')
        order by [c_loadcarrier] desc, [Type] desc

但是 运行 以下查询,我已将 @SalesorderTracking 变量更改为实际值 returns 预期记录。

SELECT  [MyDB].[dbo].[t_Item_log].[c_id]
        ,[MyDB].[dbo].[t_Item_log].[c_Info]
        ,CASE [c_TypeRef]
            WHEN '1' THEN 'Loadcarrier From'
            WHEN '2' THEN 'Loadcarrier To'
        END AS [Type]
        ,[c_loadcarrier]
        ,[c_WorkerName]
        ,[c_Material]
        ,[c_Qty]
        ,[c_Created]     
        ,[c_ShelfLabelFrom]
        ,[c_WarehouseFrom]
        ,[c_WareHouseAreaFrom]
        ,[c_ZoneFrom]
        ,[c_TransactionId]
        ,[c_ItemRef]
        FROM dbo.t_Item_log INNER JOIN dbo.t_Item_log_LoadCarrier ON dbo.t_Item_log.c_id = dbo.t_Item_log_LoadCarrier.c_Item_LogRef
        where c_loadcarrier in ('PL10275703','PL10275700','PL10269031','PL10269030','PL10271642')  AND (c_TypeRef = '1' OR c_TypeRef = '2')
        order by [c_loadcarrier] desc, [Type] desc

我真的想不通我对那个变量做错了什么。

感谢任何帮助和建议,谢谢。

你的SQL服务器版本是多少?版本14.0

我认为这应该可行:

    SELECT  [MyDB].[dbo].[t_Item_log].[c_id]
            ,[MyDB].[dbo].[t_Item_log].[c_Info]
            ,CASE [c_TypeRef]
                WHEN '1' THEN 'Loadcarrier From'
                WHEN '2' THEN 'Loadcarrier To'
            END AS [Type]
            ,[c_loadcarrier]
            ,[c_WorkerName]
            ,[c_Material]
            ,[c_Qty]
            ,[c_Created]     
            ,[c_ShelfLabelFrom]
            ,[c_WarehouseFrom]
            ,[c_WareHouseAreaFrom]
            ,[c_ZoneFrom]
            ,[c_TransactionId]
            ,[c_ItemRef]
            FROM dbo.t_Item_log 
            INNER JOIN dbo.t_Item_log_LoadCarrier 
                ON dbo.t_Item_log.c_id = dbo.t_Item_log_LoadCarrier.c_Item_LogRef
            where c_loadcarrier in 
                 (SELECT c_loadcarrier 
                 FROM [ApportMM].[dbo].[#SalesorderTracking]) 
            AND (c_TypeRef = '1' OR c_TypeRef = '2')
            order by [c_loadcarrier] desc, [Type] desc