Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want counts with your rows you could use the OVER clause to generate them. If NoteType can only be S or M in the table, then passing a NULL and using ISNULL(, ) should get what you want. The code below pulls all notes, all related notes, and the first two columns are overall counts of source rows from each table. I know this is example code, so I won't get all twitchy about the select *. </p> <p>The link at bottom goes over the OVER clause.</p> <pre><code>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 &lt;= @Date and NoteType = ISNULL(@NoteType, NoteType); </code></pre> <p>You don't necessarily need all of those copies of the counts, so unioning in a header row might be more what you had in mind. The code below pulls all notes and a header row (noteid is null) with only counts:</p> <pre><code>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 &lt;= @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 &lt;= @Date and NoteType = ISNULL(@NoteType, NoteType) </code></pre> <p>;</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms189461.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms189461.aspx</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload