How to do a NOT IN when selecting table values

KAdamsInCo
Member Posts: 28
I know how to do this in SQL:
insert into [dbo].[SAM-TEST$Constituent Category] ([Constituent No_],[Line No_],
[Category Type],[Category Code],[Spouse Related])
select No_,
LineNum = (select isnull((max([Line No_]) + 10000),10000) from [dbo].[SAM-TEST$Constituent Category]
where [Constituent No_] = [dbo].[SAM-TEST$Contact].[No_]),
'DONORSTAT', '', 0
from [dbo].[SAM-TEST$Contact]
where [No_] NOT IN (SELECT [Constituent No_] from [dbo].[SAM-TEST$Constituent Category]
where [Category Type] = 'DONORSTAT');
But how do I do a "NOT IN" in C/SIDE? I know how to apply FILTERs and SETRANGEs and such, but what is the best way to select where a value is NOT IN a list selected from the same table?
Thanks!
#-o
insert into [dbo].[SAM-TEST$Constituent Category] ([Constituent No_],[Line No_],
[Category Type],[Category Code],[Spouse Related])
select No_,
LineNum = (select isnull((max([Line No_]) + 10000),10000) from [dbo].[SAM-TEST$Constituent Category]
where [Constituent No_] = [dbo].[SAM-TEST$Contact].[No_]),
'DONORSTAT', '', 0
from [dbo].[SAM-TEST$Contact]
where [No_] NOT IN (SELECT [Constituent No_] from [dbo].[SAM-TEST$Constituent Category]
where [Category Type] = 'DONORSTAT');
But how do I do a "NOT IN" in C/SIDE? I know how to apply FILTERs and SETRANGEs and such, but what is the best way to select where a value is NOT IN a list selected from the same table?
Thanks!
#-o
0
Answers
-
There is no simple way. Better will be when you describe by words what you want to do and it will lead to some solution quicker than this "unreadable" SQL command... ;-)0
-
You want to fill your table "Constituent Category" based on table Contact.
variables: ConstCategory Record Constituent Category Contact Record Contact LineNo Integer LineNo := 10000; //go through the Contacts Contact.reset; if Contact.findset then begin repeat //is there a 'DONORSTAT' category for this contact? If yes, do nothing for this contact ConstCategory.reset; ConstCategory.setrange("Constituent No.",Contact."No."); ConstCategory.setrange("Category Type",'DONORSTAT'); if ConstCategory.isempty then begin //No ConstCatogory found ConstCategory.setrange("Category Type"); ConstCategory.locktable; if ConstCategory.find('+') then //find last Line no LineNo := ConstCategory."Line No." + 10000; ConstCategory.init; ConstCategory."Constituent No." := Contact."No."; ConstCategory."Line No." := LineNo; ConstCategory."Category Type" := 'DONORSTAT'; ConstCategory."Category Code" := ''; ConstCategory."Spouse Related" := 0; ConstCategory.insert; end; until Contact.next = 0; end;
Do you mean this?Do you make it right, it works too!0 -
Only you don't want to do line number like that
variables: ConstCategory Record Constituent Category Contact Record Contact LineNo Integer //LineNo := 10000; // this way it's going to increment the number, where it should start at 10000 for each contact //go through the Contacts Contact.reset; if Contact.findset then begin repeat //is there a 'DONORSTAT' category for this contact? If yes, do nothing for this contact ConstCategory.reset; ConstCategory.setrange("Constituent No.",Contact."No."); ConstCategory.setrange("Category Type",'DONORSTAT'); if ConstCategory.isempty then begin //No ConstCatogory found ConstCategory.setrange("Category Type"); ConstCategory.locktable; // if ConstCategory.find('+') then //find last Line no // LineNo := ConstCategory."Line No." + 10000; if ConstCategory.FINDLAST then //find last Line no, but use FINDLAST, as we only want 1 record LineNo := ConstCategory."Line No." + 10000 else LineNo := 10000; ConstCategory.init; ConstCategory."Constituent No." := Contact."No."; ConstCategory."Line No." := LineNo; ConstCategory."Category Type" := 'DONORSTAT'; ConstCategory."Category Code" := ''; ConstCategory."Spouse Related" := 0; ConstCategory.insert; end; until Contact.next = 0; end;
0 -
or so with the "line no."
I increment the "line No." because i doesn't know his Primary key structure. And in his SQL query he also increment (intended or not intended)Do you make it right, it works too!0 -
Sure, point taken
My assumption was: primary key = Contact No, Line No
0 -
This example code worked perfectly. Thank you for taking the time to post the reply, it really helped!
-Keaton
=D>0 -
-
Please and welcomeDo you make it right, it works too!0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions