SQL Server 2008 相关问题
我有一张表记录了申请信用卡的人
CREATE TABLE [dbo].[CREDITCARD_APPLICATION](
[CREDITCARD_APPLICATION_ID] [bigint] IDENTITY(1,1) NOT NULL,
[PRIMARY_CUSTOMER_ID] [int] NOT NULL,
[SECONDARY_CUSTOMER_ID] [int] NULL,
[PRIMARY_CARD_ID] [int] NULL,
[SECONDARY_CARD_ID] [int] NULL,
[STORE_ID] [int] NULL,
[CARD_REFERRAL_SOURCE] [nvarchar](50) NULL,
[CAMPAIGN_CODE] [varchar](30) NULL,
[CREATED_DATE] [datetime2](3) NOT NULL,
[CREATED_BY] [varchar](50) NOT NULL,
[LAST_MODIFIED_DATE] [datetime2](3) NULL,
[LAST_MODIFIED_BY] [varchar](50) NULL,
[VERSION] [timestamp] NOT NULL
)
我还有一个连接到这个表的视图,其中包含卡片和客户。我需要从该表中获取一条信息,即campaign_code
,并且我在视图中通过使用coalesce
嵌套选择来执行此操作。
SELECT....some data....
,coalesce((SELECT TOP 1 CAMPAIGN_CODE FROM dbo.CREDITCARD_APPLICATION cca where cca.PRIMARY_CARD_ID=cc.card_id ORDER BY cca.CREATED_DATE DESC),
(SELECT TOP 1 CAMPAIGN_CODE FROM dbo.CREDITCARD_APPLICATION cca where cca.SECONDARY_CARD_ID=cc.card_id ORDER BY cca.CREATED_DATE DESC))
AS LatestCampaignCode
我想知道的是 2 个 select 语句是否将始终执行,或者如果它从第一个语句中找到结果,它将在那里停止处理。总是执行这些选择似乎有点低效,事实上在很多(大多数)情况下,表中不会有任何匹配的记录