从sql server中的特定列中选择值?

时间:2017-06-14 06:59:54

标签: sql-server stored-procedures

  

存储过程: -

CREATE PROCEDURE [dbo].[RT_SelectAll_BatchHistory_By_SchedulerTime]
@Skip INT,
@Take INT,
@ResultCode AS INT = 0 OUTPUT,
@ResultText AS VARCHAR(50)=NULL OUTPUT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY 
SELECT
       [SchedulerTime]
      ,[ExecutedOn]
      ,[FinishedOn]
FROM   [T_Scheduler] WITH(NOLOCK)
WHERE [IsActive] = 1 
ORDER BY [SchedulerID]
OFFSET @Skip ROWS
FETCH NEXT @Take ROWS ONLY
SET @ResultCode = 0
SET @ResultText='SUCCESS'
END TRY
BEGIN CATCH
SET @ResultCode = -1
SET @ResultText='ERROR'
END CATCH
END

在此存储过程中,要从SchedulerTime列中选择所有行,并从executionOn和finishedOn中选择Null值。

怎样才能达到这个条件?

3 个答案:

答案 0 :(得分:1)

LinearRegression

这对我来说很好

答案 1 :(得分:0)

考虑到你知道代码中的脏读,你可以试试这样的东西..

SELECT
       [SchedulerTime]
      ,[ExecutedOn] = NULL
      ,[FinishedOn] = NULL
FROM   [T_Scheduler] WITH(NOLOCK)
WHERE [IsActive] = 1 
ORDER BY [SchedulerID]

答案 2 :(得分:0)

在SELECT查询中使用NULLIF

 SELECT
       [SchedulerTime]
      ,NULLIF([ExecutedOn],[ExecutedOn]) AS [ExecutedOn]
      ,NULLIF([FinishedOn],[FinishedOn]) AS [FinishedOn]
FROM   [T_Scheduler] WITH(NOLOCK)
WHERE [IsActive] = 1 
ORDER BY [SchedulerID]
-----------------------------------------------    
SELECT
       [SchedulerTime]
      ,NULL AS [ExecutedOn]
      ,NULL AS [FinishedOn]
FROM   [T_Scheduler] WITH(NOLOCK)
WHERE [IsActive] = 1 
ORDER BY [SchedulerID]