为什么特定SQL的记录数为-1?

时间:2016-02-05 11:19:58

标签: sql-server tsql ado

我已经拿到了一些代码。记录集打开如下......

rs.open sql, db, 3, 3, 1

使用以下SQL,记录集的RecordCount属性是正确的。

SELECT client.id, 
       client.NAME, 
       postcode, 
       locationx, 
       locationy, 
       NULL     AS blacklisted, 
       ' ' AS distance 
FROM   client 
       LEFT JOIN county 
              ON client.county = county.id 
WHERE  hidden = 0 
       AND client.NAME LIKE '%' 
       AND ( address LIKE '%%' 
              OR county.NAME LIKE '%%' 
              OR postcode LIKE '%%' 
              OR phone LIKE '%%' 
              OR fax LIKE '%%' ) 
ORDER  BY client.NAME 

与以下野兽一样,RecordCount属性为-1。

SELECT          booking.id, 
                booking.site, 
                site.NAME, 
                booking.client, 
                client.NAME AS clientname, 
                booking.confirmed, 
                Count(appointment.id)                   AS apps, 
                Sum(Cast(appointment.confirmed AS INT)) AS conf, 
                dates.[date]                            AS startdate, 
                h.hoursfull, 
                h.hours, 
                d.overdue, 
                e.soon 
FROM            booking 
LEFT OUTER JOIN site 
ON              booking.site = site.id 
LEFT OUTER JOIN dates 
ON              booking.id = dates.booking 
LEFT OUTER JOIN appointment 
ON              dates.id = appointment.[date] 
LEFT OUTER JOIN client 
ON              booking.client = client.id 
LEFT OUTER JOIN 
                ( 
                SELECT          booking, 
                                Sum(dates.hours)    AS hours, 
                                Sum(apps1.hourssum) AS hoursfull 
                FROM            dates 
                LEFT OUTER JOIN 
                                ( 
                                            SELECT   [date], 
                                                    Sum(hours) AS hourssum 
                                            FROM     appointment 
                                            GROUP BY [date] ) AS apps1 
                ON              dates.id = apps1.[date] 
                GROUP BY        booking ) h 
ON              booking.id = h.booking 
LEFT OUTER JOIN 
                ( 
                       SELECT 1 AS overdue) d 
ON              dates.[date] <= Getdate() 
LEFT OUTER JOIN 
                ( 
                       SELECT 1 AS soon) e 
ON              dates.[date] <= Dateadd(hh, 48, Getdate()) 
LEFT OUTER JOIN 
                ( 
            SELECT DISTINCT a.booking 
            FROM            ( 
                                            SELECT          dates.booking, 
                                                            Sum(apps2.hourssum) AS filled,
                                                            sum(dates.hours)   AS hours
                                            FROM            dates 
                                            LEFT OUTER JOIN 
                                                            ( 
                                                                        SELECT   [date],
                                                                                sum(hours) AS hourssum
                                                                        FROM     appointment
                                                                        GROUP BY [date] ) AS apps2
                                            ON              dates.id = apps2.[date] 
                                            GROUP BY        dates.booking )a 
            WHERE           ( 
                                            filled < hours) 
            OR              ( 
                                            filled IS NULL) )b 
ON              b.booking = booking.id 
WHERE           ( 
                                booking.hidden = 0) 
AND             booking.client = 2543 
AND             booking.confirmed = 1 
AND             ((( 
                SELECT TOP 1 
                    dates.id 
                FROM            dates 
                LEFT OUTER JOIN appointment 
                ON              dates.id = appointment.[date] 
                WHERE           ( 
                                dates.booking = booking.id)
                GROUP BY        dates.id, 
                                dates.booking 
                ORDER BY        dates.booking) = dates.id)
                OR              dates.id IS NULL) 
GROUP BY        booking.id, 
                booking.site, 
                site.NAME, 
                booking.confirmed, 
                dates.[date], 
                booking.client, 
                client.NAME, 
                h.hoursfull, 
                h.hours, 
                d.overdue, 
                e.soon 
ORDER BY        startdate

我可以通过在同一个&#34;打开&#34;之前切换SQL来重复此行为。

2 个答案:

答案 0 :(得分:2)

如果您使用ADO recordset,我怀疑您是,如果您想访问RecordCount property,则必须使用静态或键集游标类型。

从MSDN获取

  

Recordset对象的游标类型是否影响数量   记录可以确定。 RecordCount属性将返回-1   只向前游标;静态或键集游标的实际计数;   并且-1或动态游标的实际计数,具体取决于   数据源。

修改

如果我完整阅读你的问题会有所帮助!在更大的更复杂的查询上,即使使用正确的游标类型,RecordCount也可以返回-1。这可能由于多种原因而发生。一个例子是当你开始使用它时,记录集仍在填充。尝试跳转到最终记录,然后查询记录数。

答案 1 :(得分:0)

我更改了用于打开记录集的代码...

rs.open sql, db, 3, 3, 1

为...

rs.open sql, db, adOpenStatic, adLockReadOnly, adCmdText

非常感谢this网站。