当没有数据的时候临时表中添加一条数据量为零的存储过程
我现在写一个存储过程,建了一个临时表,存数据库中按要求遍历数据,当天没有该关键字的信息的时候,我需要在临时表也是添加一条数据,但是此时的数据量为零,不知道哪位大侠能够指导一下,下面是我写的存储过程
ALTER PROCEDURE [dbo].[sp_Statistics_KeyWordInfo_GetKeyDailyWordInfoByProjectIdAndBetweenCatchTime]
@ProjectID CHAR(36)
,@StartCatchTime DATETIME
,@EndCatchTime DATETIME
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @KeyWordInfo TABLE
(
KeyWord NVARCHAR(1000)
,JustInfoCount INT
,NegInfoCount INT
,MidInfoCount INT
,UnknownInfoCount INT
,CatchTime DATETIME
)
DECLARE @InfoCount TABLE
(
KeyWord NVARCHAR(1000)
,InfoCount INT
,CatchTime DATETIME
)
DECLARE @KeyWord TABLE
(
KeyWord NVARCHAR(1000)
)
DECLARE @CatchTime TABLE
(
CatchTime DATETIME
)
INSERT INTO @KeyWordInfo
(
KeyWord
,JustInfoCount
,NegInfoCount
,MidInfoCount
,UnknownInfoCount
,CatchTime
)
SELECT KeyWord,JustInfoCount,NegInfoCount,MidInfoCount,UnKnownInfoCount,CatchTime
FROM [dbo].[Statistics_KeyWordInfo]
WHERE ProjectID = @ProjectID AND CONVERT(VARCHAR(10),CatchTime,120) BETWEEN CONVERT(VARCHAR(10),@StartCatchTime,120) AND CONVERT(VARCHAR(10),@EndCatchTime,120)
INSERT INTO @KeyWord
(
KeyWord
)
SELECT KeyWord
FROM @KeyWordInfo
GROUP BY KeyWord
INSERT INTO @CatchTime
(
CatchTime
)
SELECT CatchTime
FROM @KeyWordInfo
GROUP BY CatchTime
SELECT kwi.KeyWord,JustInfoCount+NegInfoCount+MidInfoCount+UnknownInfoCount,kwi.CatchTime
FROM @KeyWordInfo kwi,@KeyWord kw,@CatchTime ct
WHERE kwi.KeyWord = kw.KeyWord AND kwi.CatchTime = ct.CatchTime
SELECT *
FROM @InfoCount
ORDER BY KeyWord,CatchTime DESC
END
现在的情况是我只能获取数据库中存在的数据!!!
------解决方案--------------------
关键字一个表,日期一个表,然后交叉连接,再去判断是否存在.
insert into tb1(关键字字段,日期字段 , 数量字段)
select m.关键字字段 , n.日期字段 , 0 from
关键字表 m, 日期表 n
where not exists(select 1 from tb1 where tb1.关键字字段 = m.关键字字段 and tb1.日期字段 = n.日期字段)