簡體   English   中英

View 從其 SELECT 中返回不同的結果

[英]View returns different results from its SELECT

我有一個 SQL 視圖,它返回的結果比其中的 select 少,我不知道為什么?

SQL 視圖代碼:

SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points,
       COALESCE (aph.added_point, 0) AS added_point,
       COALESCE (aph.spended_cash, 0) AS spended_cash
FROM dbo.customer_store AS cs 
INNER JOIN dbo.customer AS c
    ON cs.customer_id = c.id 
LEFT OUTER JOIN dbo.added_points_history AS aph
    ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id

上面的查詢返回 26 行,但是當我從視圖中選擇時,它只返回 7 行!

創建視圖的代碼:

--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
--Create view with schemabinding.
IF OBJECT_ID ('dbo.customers_store_with_points', 'view') IS NOT NULL
DROP VIEW dbo.customers_store_with_points ;
GO
CREATE VIEW dbo.customers_store_with_points
WITH SCHEMABINDING
AS
SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points,
    COALESCE (aph.added_point, 0) AS added_point,
    COALESCE (aph.spended_cash, 0) AS spended_cash
FROM dbo.customer_store AS cs 
INNER JOIN dbo.customer AS c
ON cs.customer_id = c.id 
LEFT OUTER JOIN dbo.added_points_history AS aph
ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id
GO
--Create an index on the view.
CREATE UNIQUE CLUSTERED INDEX IDX_customerId_storeId
ON dbo.customers_store_with_points (customer_id, store_id);
GO

從視圖中獲取結果的選擇語句:

SELECT TOP 1000 * 
FROM [dbo].[customers_store_with_points]
order by store_id

具有相同數據源的相同查詢將返回相同結果。
您正在查詢不同的數據庫,或者您的查詢是不同的。

我現在遇到了同樣的問題,我重新創建了視圖(通過Script View as > DROP And CREATE To )並且問題得到了解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM