参数化查询不限制在mssql服务器中

时间:2017-06-15 12:16:37

标签: sql-server

我有一个正在进行的项目,我一直在测试我的查询以查看它们是如何工作的,但问题是当我将参数传递给查询时,它不会将其限制为我传递的参数。

以下是查询语法:

SELECT C.*,
           F.Bank_VNO,
           F.sname
    FROM   customer C
           LEFT JOIN fssign F
                  ON c.cust_no = f.cust_no
                     AND F.cust_no = '100055'
           LEFT JOIN (SELECT Min(ac_no) ac_no
                      FROM   loans
                      WHERE  full_paid = '0'
                      GROUP  BY ac_no) AS l
                  ON c.cust_no = l.ac_no
                     AND cust_type = 'BUS' 

2 个答案:

答案 0 :(得分:0)

我认为你的意思并不是很清楚,但可能你应该删除GROUP BY:

 SELECT C.*
        ,F.Bank_VNO
        ,F.sname
    FROM customer C
    LEFT JOIN fssign F ON c.cust_no = f.cust_no
                AND F.cust_no = '100055'
    LEFT JOIN ( SELECT MIN(ac_no) ac_no
                FROM loans
                WHERE full_paid = '0'
/*              GROUP BY ac_no */
        ) AS l ON c.cust_no = l.ac_no
        AND cust_type = 'BUS'

可能是您要编写的查询是:

SELECT C.*
      ,F.Bank_VNO
      ,F.sname
FROM customer C
INNER JOIN fssign F ON c.cust_no = f.cust_no                    
INNER JOIN (SELECT MIN(ac_no) ac_no
            FROM loans
            WHERE full_paid = '0'   
            ) AS l ON c.cust_no = l.ac_no
WHERE  C.cust_type = 'BUS'
 AND  F.cust_no = '100055'

答案 1 :(得分:0)

Left Join不会过滤记录。除非您将过滤器放在Where子句中,否则它将带来左表中的所有记录。

我认为只有在cust_type = 'BUS'时才需要记录。将过滤器移动到Where子句。假设cust_type列来自customer

SELECT C.*,
           F.Bank_VNO,
           F.sname
    FROM   customer C
           LEFT JOIN fssign F
                  ON c.cust_no = f.cust_no
                     AND F.cust_no = '100055'
           LEFT JOIN (SELECT Min(ac_no) ac_no
                      FROM   loans
                      WHERE  full_paid = '0'
                      GROUP  BY ac_no) AS l  -- This query does not make more sense
                  ON c.cust_no = l.ac_no
Where C.cust_type = 'BUS'