I want to see if there is any way to do what I'm trying to do without using cursors, or any looping logic. I have these 2 tables. There is a ONE-MANY relationship between FileRunData
and Invoice
, and they're linked by RunId/FileRunId
.
CREATE TABLE [dbo].[FileRunData](
[RunId] [uniqueidentifier] primary key,
[Status] [varchar](25) NOT NULL,
[IsEmailSent] [bit] NOT NULL
)
CREATE TABLE [dbo].[Invoice](
[FileRunId] [uniqueidentifier] NULL,
[InvoiceId] [uniqueidentifier] primary key,
[InvoiceType] [varchar](20) NOT NULL,
[Status] [varchar](25) NULL
)
I want to send an email notification for the following condition.
In the FileRunData
if Status='Processed' and IsEmailSent=0
, then I have to check all the rows in the Invoice
table for that FileRunId
, and if their Status is 'Invoiced'
, then I have to send an email.
My approach (will be using cursor here)
- select RunIds from FileRunData
- for each RunId, get the number of rows for that RunId from Invoice
- get the number of rows for that RunId which have Status='Invoiced'
- If both 3&4 are equal, then send the email