SQL在同一列和表中的单个SELECT中设置不同的变量,条件不同

时间:2016-08-12 11:15:46

标签: sql-server

我掌握了SQL的基础知识。但是我可能需要一些关于这个问题的高级帮助。

我正在开发一个执行不同SQL准备语句的API,现在我正在尝试优化这些查询。

目前我的一个预准备语句有多个select语句,可以从同一个表中选择,但每个语句都有自己的条件,例如:

DECLARE 
  @DESCRIPTION_1 NVARCHAR(200)
, @DESCRIPTION_2 NVARCHAR(200)
, @DESCRIPTION_3 NVARCHAR(200)

SELECT @DESCRIPTION_1 = [DESCRIPTION] FROM [Objectives] WHERE [OBJECTIVE_ID] = '4E2DEA7B-025A-4958-8696-2D13EDD3E08E';
SELECT @DESCRIPTION_2 = [DESCRIPTION] FROM [Objectives] WHERE [OBJECTIVE_ID] = '81B9B7E5-A833-4C5A-9B73-CBACFB055404';
SELECT @DESCRIPTION_3 = [DESCRIPTION] FROM [Objectives] WHERE [OBJECTIVE_ID] = 'A065CF91-01C9-41A8-A326-A00402329833';

*这只是我查询的精简版本。原件看起来像这样:

SELECT 
  @OBJECTIVE_1_DESCRIPTION = [dbo].[Objectives].[OBJECTIVE_DESCRIPTION]
, @OBJ1_LVL = [dbo].[Objectives].[OBJECTIVE_LEVEL]
, @OBJ1_TARGET = [dbo].[Objectives].[OBJECTIVE_COMPETION_COUNT]
, @REWARD_1_CODE = [dbo].[Objectives].[OBJECTIVE_REWARD]
FROM [dbo].[Objectives] 
WHERE [dbo].[Objectives].[OBJECTIVE_ID] = @OBJECTIVE1_ID;

有什么方法可以将它组合成一个SELECT语句吗?

4 个答案:

答案 0 :(得分:1)

DECLARE 
  @DESCRIPTION_1 NVARCHAR(200)
, @DESCRIPTION_2 NVARCHAR(200)
, @DESCRIPTION_3 NVARCHAR(200)

Select @DESCRIPTION_1 = max(case when [OBJECTIVE_ID] = '4E2DEA7B-025A-4958-8696-2D13EDD3E08E' then [DESCRIPTION] else null end) 
      ,@DESCRIPTION_2 = max(case when [OBJECTIVE_ID] = '81B9B7E5-A833-4C5A-9B73-CBACFB055404' then [DESCRIPTION] else null end)      
      ,@DESCRIPTION_3 = max(case when [OBJECTIVE_ID] = 'A065CF91-01C9-41A8-A326-A00402329833' then [DESCRIPTION] else null end)      
FROM [Objectives]
Where [OBJECTIVE_ID] in ('4E2DEA7B-025A-4958-8696-2D13EDD3E08E','81B9B7E5-A833-4C5A-9B73-CBACFB055404','A065CF91-01C9-41A8-A326-A00402329833')

我还会声明3 @ OBJECTIVE_ID来减少文本和冗余

WHERE也是可选的,如果您有一个小表,请将其删除

答案 1 :(得分:0)

为什么不立即用子选择设置它们?

DECLARE @DESCRIPTION_1 NVARCHAR(200)=(SELECT [DESCRIPTION] FROM [Objectives] WHERE [OBJECTIVE_ID] = '4E2DEA7B-025A-4958-8696-2D13EDD3E08E');
DECLARE @DESCRIPTION_2 NVARCHAR(200)=(SELECT [DESCRIPTION] FROM [Objectives] WHERE [OBJECTIVE_ID] = '81B9B7E5-A833-4C5A-9B73-CBACFB055404');
DECLARE @DESCRIPTION_3 NVARCHAR(200)=(SELECT [DESCRIPTION] FROM [Objectives] WHERE [OBJECTIVE_ID] = 'A065CF91-01C9-41A8-A326-A00402329833');

假设您OBJECTIVE_ID上有一个索引,那么这个索引也应该非常快......

答案 2 :(得分:0)

好的,如果可以的话,还有一次拍摄。如您所知,SQL Server不支持数组,也不动态创建变量(无动态SQL)

我应该补充一下,IDENTITY会保留适当的顺序,以防它变得很重要。

Declare @KeyID Table (Key_PS int identity,Key_Value varchar(100))
Insert Into @KeyID values (@OBJECTIVE1_ID),(@OBJECTIVE2_ID),(@OBJECTIVE3_ID)

SELECT @OBJECTIVE_1_DESCRIPTION  = max(IIF(Key_PS=1 ,[OBJECTIVE_DESCRIPTION],null))
     , @OBJ1_LVL                 = max(IIF(Key_PS=1 ,[OBJECTIVE_LEVEL],null))
     , @OBJ1_TARGET              = max(IIF(Key_PS=1 ,[OBJECTIVE_COMPETION_COUNT],null))
     , @REWARD_1_CODE            = max(IIF(Key_PS=1 ,[OBJECTIVE_REWARD],null)) 
     --... [2 - 17] ... --
     , @OBJECTIVE_18_DESCRIPTION = max(IIF(Key_PS=18,[OBJECTIVE_DESCRIPTION],null))
     , @OBJ18_LVL                = max(IIF(Key_PS=18,[OBJECTIVE_LEVEL],null))
     , @OBJ18_TARGET             = max(IIF(Key_PS=18,[OBJECTIVE_COMPETION_COUNT],null))
     , @REWARD_18_CODE           = max(IIF(Key_PS=18,[OBJECTIVE_REWARD],null)) 
FROM (Select B.*,A.* 
       From  [dbo].[Objectives] 
       Join  @KeyID B on (A.OBJECTIVE_ID=B.Key_Value)
     ) A

答案 3 :(得分:-1)

使用UNION:

SELECT 
  @Objective1_Id Objective_Id,
  @OBJECTIVE_1_DESCRIPTION = [dbo].[Objectives].[OBJECTIVE_DESCRIPTION]
, @OBJ1_LVL = [dbo].[Objectives].[OBJECTIVE_LEVEL]
, @OBJ1_TARGET = [dbo].[Objectives].[OBJECTIVE_COMPETION_COUNT]
, @REWARD_1_CODE = [dbo].[Objectives].[OBJECTIVE_REWARD]
FROM [dbo].[Objectives] 
WHERE [dbo].[Objectives].[OBJECTIVE_ID] = @OBJECTIVE1_ID
UNION
SELECT 
  @Objective2_Id,
  @OBJECTIVE_1_DESCRIPTION = [dbo].[Objectives].[OBJECTIVE_DESCRIPTION]
, @OBJ1_LVL = [dbo].[Objectives].[OBJECTIVE_LEVEL]
, @OBJ1_TARGET = [dbo].[Objectives].[OBJECTIVE_COMPETION_COUNT]
, @REWARD_1_CODE = [dbo].[Objectives].[OBJECTIVE_REWARD]
FROM [dbo].[Objectives] 
WHERE [dbo].[Objectives].[OBJECTIVE_ID] = @OBJECTIVE2_ID
UNION
SELECT 
  @Objective3_Id,
  @OBJECTIVE_1_DESCRIPTION = [dbo].[Objectives].[OBJECTIVE_DESCRIPTION]
, @OBJ1_LVL = [dbo].[Objectives].[OBJECTIVE_LEVEL]
, @OBJ1_TARGET = [dbo].[Objectives].[OBJECTIVE_COMPETION_COUNT]
, @REWARD_1_CODE = [dbo].[Objectives].[OBJECTIVE_REWARD]
FROM [dbo].[Objectives] 
WHERE [dbo].[Objectives].[OBJECTIVE_ID] = @OBJECTIVE3_ID;

假设所有@ObjectiveX_id都存在行,您有一个查询将返回3行,每行包含与之相关的objective_id。