Sql server SQL Server存储过程空值
我有一个存储过程,它从视图中选择数据,并根据条件将其插入到一个诱人的视图中。我要做的是确保如果addeddate有空值,它们就会被排除Sql server SQL Server存储过程空值,sql-server,stored-procedures,null,Sql Server,Stored Procedures,Null,我有一个存储过程,它从视图中选择数据,并根据条件将其插入到一个诱人的视图中。我要做的是确保如果addeddate有空值,它们就会被排除 Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup, Rn, UserID) select Pop, PlanID, PopFull, InterviewDate, 1stAppt, Followup, rn, @UserID from (Select *,
Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup, Rn, UserID)
select Pop, PlanID, PopFull, InterviewDate, 1stAppt, Followup, rn, @UserID
from
(Select *, row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo) t
where rn = 1
and interviewdate >= @fromDate
and interviewDate <= @toDate
我该怎么做
我基本上是试图通过前面的addedate进行过滤,但排除可能出现的空日期
我有这个SP,我还有另一个存储过程,它做addedate DESC。但是我不知道这是否喜欢我只有一个日期的事实。对于ASC分区,它提取一个空值,对于DESC,它提取一个实际日期,只有一个日期。我希望能够在两个存储过程中都使用该日期,除非有多个日期-我希望它能够获取最早的日期和最晚的日期除非我遗漏了什么,否则派生表中的一个简单where子句应该可以做到这一点:
Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup, Rn, UserID)
select Pop,PlanID, PopFull,InterviewDate,1stAppt,Followup, rn, @UserID
from (
Select *,row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo
where AddedDate is not null
) t
where rn = 1
and interviewdate >=@fromDate
and interviewDate <=@toDate
更新
根据我们在评论中的对话,我认为这是您想要的:
Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup, Rn, UserID)
select Pop,PlanID, PopFull,InterviewDate,1stAppt,Followup, rn, @UserID
from (
Select *,row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo
where AddedDate is not null
) t
where rn = 1
and interviewdate >=@fromDate
and interviewDate <=@toDate
union
select Pop,PlanID, PopFull,InterviewDate,'2016-01-01',Followup, rn, @UserID
from (
Select *,row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo t1
where AddedDate is null
and not exists
(
select 1
from VInfo t
where AddedDate is not null
and interviewdate >=@fromDate
and interviewDate <=@toDate
)
) t
where rn = 1
and interviewdate >=@fromDate
and interviewDate <=@toDate
除非我遗漏了什么,否则派生表中的一个简单where子句应该可以做到:
Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup, Rn, UserID)
select Pop,PlanID, PopFull,InterviewDate,1stAppt,Followup, rn, @UserID
from (
Select *,row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo
where AddedDate is not null
) t
where rn = 1
and interviewdate >=@fromDate
and interviewDate <=@toDate
更新
根据我们在评论中的对话,我认为这是您想要的:
Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup, Rn, UserID)
select Pop,PlanID, PopFull,InterviewDate,1stAppt,Followup, rn, @UserID
from (
Select *,row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo
where AddedDate is not null
) t
where rn = 1
and interviewdate >=@fromDate
and interviewDate <=@toDate
union
select Pop,PlanID, PopFull,InterviewDate,'2016-01-01',Followup, rn, @UserID
from (
Select *,row_number() over (partition by PlanID order BY AddedDate ASC) as rn
from VInfo t1
where AddedDate is null
and not exists
(
select 1
from VInfo t
where AddedDate is not null
and interviewdate >=@fromDate
and interviewDate <=@toDate
)
) t
where rn = 1
and interviewdate >=@fromDate
and interviewDate <=@toDate
您使用分区编辑的内容是否足以让我按最早的日期提取数据,其中日期不为空???行数是使用select子句计算的,因此它仅在通过where子句过滤后计算行数。对于PlanID-在某些情况下,我有多个日期,但有时我也只有一个日期,其他值为空。为什么当我只有一个日期并运行我所讨论的SP时,当我按添加的ASC按PlanID顺序进行分区时,我会拉空值,然而,当我这样做时。。。按PlanID顺序按AddedDate Desc分区我得到日期。我希望在这两种情况下都能使用日期。出于某种原因,当我把addeddate不为null的地方包括进来时——我没有得到我所有的数据——我猜一些PlanID没有添加日期——我不明白你在这条评论中问什么。我是否可以指定如果addeddate为null,那么将其设为2016年1月1日——或者在这种情况下,我想说,如果addeddate为null,那么2016年1月1日和1stapt=null是什么您使用分区进行了编辑,足以让我按最早的日期提取数据,其中日期不为空???行数是使用select子句计算的,因此它仅在通过where子句过滤后对行进行计数。对于PlanID-在某些情况下,我有多个日期,但有时我也只有一个日期,其他值为空。为什么当我只有一个日期并运行我所讨论的SP时,当我按添加的ASC按PlanID顺序进行分区时,我会拉空值,然而,当我这样做时。。。按PlanID顺序按AddedDate Desc分区我得到日期。我希望在这两种情况下都能使用日期。出于某种原因,当我把addeddate不为null的地方包括进来时——我没有得到所有的数据——我猜一些PlanID没有添加日期——我不明白你在这条评论中问什么。我是否可以指定如果addeddate为null,那么将其设置为2016年1月1日——或者在这种情况下,我想说如果addeddate为null,那么2016年1月1日,1stapt=null