Search code examples
c#selectoledb

OleDB Select Where not Exists


I want to SELECT all JobTitles that aren't used in the Employees table, so they do exist in the JobTitle table but are not associated with any Employees in that table. This is what I have tried so far;

private static void NullJobTitle()
{
    using (OleDbConnection dbfCon = new OleDbConnection(dbfConstr))
    {
        dbfCon.Open();
        var dbfCmd = new OleDbCommand(@"SELECT j.jbt_title
        FROM jobtitle j
        LEFT JOIN employs e ON j.jbt_title = e.em_title
        WHERE j.jbt_title IS NULL", dbfCon);

        var dTable = new DataTable();
        var dataAdapter = new OleDbDataAdapter(dbfCmd);
        dataAdapter.Fill(dTable);
        WriteToCSV(dTable, "presets");
    }
}

However it is not working so far, nothing is written to my CSV file but no errors are thrown either. How can I modify this to work properly?


Solution

  • I think your query string is wrong. If you want to get unused title from table jobtitle it should be like this:

    SELECT j.jbt_title
    FROM jobtitle j
    LEFT JOIN employs e ON j.jbt_title = e.em_title
    WHERE e.em_title IS NULL