在第一个SELECT中分配的变量不在第二个SELECT中检索

时间:2018-07-19 14:36:25

标签: sql-server

在执行以下语句时,会为Start_IndexEnd_index返回非空/空值的混合值:

SELECT 
    CHARINDEX('_',  dbo.Components.Cast, 1) AS Start_Index,
    CHARINDEX('_', dbo.Components.Cast, CHARINDEX('_', dbo.Components.Cast, 1) + 1) AS End_Index
FROM dbo.Components

由于我希望同时执行赋值和数据检索,因此我将SELECT语句分为两部分。但是,Start_IndexEnd_index现在为所有行返回NULL

DECLARE @start_index INT
DECLARE @end_index INT

SELECT 
    @start_index = CHARINDEX('_',  dbo.Components.Cast, 1),
    @end_index = CHARINDEX('_', dbo.Components.Cast, CHARINDEX('_', dbo.Components.Cast, 1) + 1)
FROM dbo.Components

SELECT
    @start_index AS Start_Index,
    @end_index AS End_Index
FROM dbo.Components

我在做什么错了?

0 个答案:

没有答案