0

我正在尝试创建一个 tsql 存储过程,它将根据一些参数输出行。最终目标是将行移动到另一个表,这就是为什么我有计数 - 我想跟踪我移动了多少行。有两个表 -NotesExtraNotesExtraNotes保存来自第一个表的溢出信息。

我正在使用该if语句根据调用的参数选择正确的行,NoteType但我不知道如何在if. 我知道每个中的选择语句if是错误的

select * 
from dbo.Notes 
left join dbo.ExtraNotes on Notes.NoteID = dbo.ExtraNotes.NoteID 
where NoteDate <= @Date

任何人都可以提供一些关于我如何输出正确的行并可能更好地重组它的指针吗?

完整的代码在这里。

alter proc selectrows 
   --external variables
   @Date datetime,
   @NoteType varchar(2)
as
--internal variables
--Count variables, before any changes
declare @count_rowsBefore int
declare @count_Extra_rowsBefore int

--Count variables of selected rows to be moved

declare @count_SelectedRows int
declare @count_Extra_SelectedRows int

select @count_rowsBefore = count(*)
from dbo.Notes

select @count_Extra_SelectedRows = count(*)
from dbo.ExtraNotes


if(@NoteType= 'B')
begin
select @count_SelectedRows = count(dbo.Notes.NoteID), @count_Extra_SelectedRows =            count(dbo.ExtraNotes.NoteID)
        from dbo.Notes
        left join dbo.ExtraNotes on
        Notes.NoteID = dbo.ExtraNotes.NoteID
        where NoteDate <= @Date

        select *
        from dbo.Notes
        left join dbo.ExtraNotes on
        Notes.NoteID = dbo.ExtraNotes.NoteID
        where NoteDate <= @Date
end
else if(@NoteType = 'S')
  begin
  select @count_SelectedRows = count(dbo.Notes.NoteID), 
@count_Extra_SelectedRows = count(dbo.ExtraNotes.NoteID)
        from dbo.Notes
        left join dbo.ExtraNotes on
        Notes.NoteID = dbo.ExtraNotes.NoteID
        where NoteDate <= @Date
        and NoteType = 'S'

        select *
        from dbo.Notes
        left join dbo.ExtraNotes on
        Notes.NoteID = dbo.ExtraNotes.NoteID
        where NoteDate <= @Date
end
else if (@NoteType = 'M')
begin
select @count_SelectedRows = count(dbo.Notes.NoteID),
@count_Extra_SelectedRows =  count(dbo.ExtraNotes.NoteID)
        from dbo.Notes
        left join dbo.ExtraNotes on
        Notes.NoteID = dbo.ExtraNotes.NoteID
        where NoteDate <= @Date
        and NoteType = 'M'

        select *
        from dbo.Notes
        left join dbo.ExtraNotes on
        Notes.NoteID = dbo.ExtraNotes.NoteID
        where NoteDate <= @Date
  end
 else
  begin
  raiserror('Please enter a valid Note Read Type= M, S or B',16,1)
  end

Print 'Total Number of rows: ' + cast(@count_rowsBefore as varchar(10))
Print 'Total Number of "Extra" rows: ' + cast(@count_Extra_RowsBefore as varchar(10))

Print '-----------------------------------------------'
Print 'Total Number of rows to Move: ' + cast(@count_SelectedRows as varchar(10))
Print 'Total Number of "Extra" Rows to Move: ' + cast(@count_Extra_SelectedRows 
as varchar(10))

输出截图

4

2 回答 2

0

如果您只想对您的行进行计数,您可以使用 OVER 子句来生成它们。如果 NoteType 在表中只能是 S 或 M,那么传递一个 NULL 并使用 ISNULL(, ) 应该会得到你想要的。下面的代码提取所有注释、所有相关注释,前两列是每个表中源行的总计数。我知道这是示例代码,所以我不会对 select *.

底部的链接越过 OVER 子句。

CREATE PROCEDURE selectRows
    ( @Date DATETIME
      , @NoteType VARCHAR(2) -- Pass NULL for both
    )
AS
    select count(dbo.notes.noteid) 
               over() as notecount
           , count(dbo.ExtraNotes.NoteID)
               over() as extraNoteCount
           , *
    from dbo.Notes 
    left join dbo.ExtraNotes
        on Notes.NoteID = dbo.ExtraNotes.NoteID
    where NoteDate <= @Date
      and NoteType = ISNULL(@NoteType, NoteType);

您不一定需要所有这些计数副本,因此在标题行中合并可能更符合您的想法。下面的代码提取所有注释和一个标题行(noteid 为空),只有计数:

CREATE PROCEDURE selectRows
    ( @Date DATETIME
      , @NoteType VARCHAR(2) -- Pass NULL for both
    )
AS
    select *
       , CAST(NULL AS INT) noteCount
       , CAST(NULL AS INT) extraNoteCount
    from dbo.Notes 
    left join dbo.ExtraNotes
        on Notes.NoteID = dbo.ExtraNotes.NoteID
    where NoteDate <= @Date
      and NoteType = ISNULL(@NoteType, NoteType)
    UNION ALL
    SELECT NULL
       , NULL -- Repeat for as many columns as are in the select * above
       , count(dbo.Notes.NoteID)
       , count(dbo.ExtraNotes.NoteID)
    from dbo.Notes 
    left join dbo.ExtraNotes
        on Notes.NoteID = dbo.ExtraNotes.NoteID
    where NoteDate <= @Date
      and NoteType = ISNULL(@NoteType, NoteType)

;

http://msdn.microsoft.com/en-us/library/ms189461.aspx

于 2012-04-19T22:20:19.520 回答
0

目前尚不清楚您要做什么。似乎您正在寻找数据以及有关数据的一些统计信息,但我们不知道您真正想要什么数据,或者结构应该是什么。将它们分成单独的逻辑部分会更容易吗?映射出您想要输出的数据并分别为每个项目构建代码可能会更简单。

您可以澄清是否可以提供几行包含列和示例数据的行,以及如果该数据通过您的代码运行时您想要的示例输出。

一个建议是,If/then 语句不过滤任何内容,因此您将在第二个查询中获得比您想要的更多的数据,因为过滤器仅适用于每个查询。如果这对您有意义:您已经有一个带有 NoteType 的变量,您可以使用它来过滤。这会正确过滤数据,并且应该替换整个 if-else if 语句集,因为代码现在使用变量直接选择正确的数据。因此,您可以使用而不是使用 if 语句

select @count_SelectedRows = count(dbo.Notes.NoteID), 
        @count_Extra_SelectedRows =  count(dbo.ExtraNotes.NoteID)
  from dbo.Notes left join dbo.ExtraNotes 
    on Notes.NoteID = dbo.ExtraNotes.NoteID
  where NoteDate <= @Date
  and NoteType = @NoteType

select * 
  from dbo.Notes left join dbo.ExtraNotes 
    on Notes.NoteID = dbo.ExtraNotes.NoteID         
  where NoteDate <= @Date and NoteType = @NoteType
于 2012-04-19T19:03:54.083 回答