2
SELECT  
   wl.WatchListId, wl.Code, wl.[Description], wl.DateCreated,
   wl.CreatedBy, wl.DateModified, wl.ModifiedBy,
   wpi.ParameterExpression as IndividualExpression,
   wpb.ParameterExpression as BusinessExpression,
   wpd.ParameterExpression as DefaultExpression,
   CASE 
       WHEN EXISTS(SELECT 1 FROM SourceWatchList 
                   WHERE SourceId = @SourceId AND WatchListId = wl.WatchListId)  
       THEN 1 ELSE 0 
   END AS IsActive
FROM 
   [WatchList] wl 
LEFT JOIN  
   SourceWatchList swl ON wl.WatchListId = swl.WatchListId AND swl.SourceId = @SourceId
LEFT JOIN 
   (SELECT 
        ParameterExpression, SourceId, WatchListId 
    FROM WatchListParameter 
    WHERE EntityType = 'INDIVIDUAL') wpi ON wpi.SourceId = @SourceId 
                                         AND wpi.WatchListId = wl.WatchListId
LEFT JOIN 
    (SELECT 
         ParameterExpression, SourceId, WatchListId 
     FROM WatchListParameter 
     WHERE EntityType = 'BUSINESS') wpb ON wpb.SourceId = @SourceId 
                                        AND wpb.WatchListId = wl.WatchListId
LEFT JOIN 
    (SELECT ParameterExpression, SourceId, WatchListId 
     FROM WatchListParameter 
     WHERE EntityType = 'DEFAULT') wpd ON wpd.SourceId = @SourceId 
                                       AND wpd.WatchListId = wl.WatchListId
WHERE 
     wl.IsActive = 1

我有上面的查询。很简单。

这是 SQL Server Management Studio 表中的一个片段:

在此处输入图像描述

并从 Visual Studio 中的调试器:在此处输入图像描述

Visual Studio 表在 3 个表达式列中没有数据,而 Management Studio(正确)有。谁能告诉我为什么会这样,我可以采取哪些步骤来解决这个问题?

我 100% 确定我在两者之间也使用了相同的参数。

string sql = @"SELECT  wl.WatchListId,wl.Code,wl.[Description],wl.DateCreated,
                                wl.CreatedBy,wl.DateModified,wl.ModifiedBy,
                                wpi.ParameterExpression as IndividualExpression,
                                wpb.ParameterExpression as BusinessExpression,
                                wpd.ParameterExpression as DefaultExpression,
                                CASE WHEN EXISTS(Select 1 FROM SourceWatchList 
                                    WHERE SourceId = @SourceId AND WatchListId = wl.WatchListId)  
                                        THEN 1 ELSE 0 END AS IsActive
                        FROM [WatchList] wl 

                        LEFT JOIN  SourceWatchList swl on wl.WatchListId = swl.WatchListId and swl.SourceId = @SourceId
                        LEFT JOIN (Select ParameterExpression, SourceId, WatchListId FROM WatchListParameter WHERE EntityType = 'INDIVIDUAL') wpi
                                        ON wpi.SourceId = @SourceId AND wpi.WatchListId = wl.WatchListId
                        LEFT JOIN (Select ParameterExpression, SourceId, WatchListId FROM WatchListParameter WHERE EntityType = 'BUSINESS') wpb
                                        ON wpb.SourceId = @SourceId AND wpb.WatchListId = wl.WatchListId
                        LEFT JOIN (Select ParameterExpression, SourceId, WatchListId FROM WatchListParameter WHERE EntityType = 'DEFAULT') wpd
                                        ON wpd.SourceId = @SourceId AND wpd.WatchListId = wl.WatchListId
                        where wl.IsActive = 1";
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WatchListCompliance"].ConnectionString);
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("@SourceId", SourceId);

        DataTable dt = new DataTable();
        using (SqlDataAdapter a = new SqlDataAdapter(cmd))
        {
            a.Fill(dt);
        }
4

2 回答 2

0

This might be a problem with types and equality. You can try using SqlDataType when you define the parameters on the sqlcommand. When you use addwithvalue without declaring a type it could be selecting the wrong type. Then you should use the type you declared in the sql expression.

cmd.Parameters.Add("@SourceId", SqlDbType.VarChar, 20);
cmd.Parameters["@SourceId"].Value = SourceId;
于 2013-08-09T20:09:34.280 回答
0

疯狂猜测:您确定您的会话配置方式相同吗?尤其是 SET ANSI_NULLS 选项,它改变了比较针对空值的行为方式。

值得注意的是,有些数据库可能有不寻常的默认值,但有些客户端(例如 ADO.NET)在连接时明确定义了自己的会话值,即使您没有要求他们这样做。

于 2014-07-02T23:40:26.253 回答