GetDBCount executes two queries

Posts   
 
    
Posts: 48
Joined: 14-Feb-2008
# Posted on: 15-Feb-2008 16:26:04   

Hi!

We're using GetDbCount() in a validation procedure to verify a certain piece of data is unique in it's table (vehicle registration number, to be precise):

DataAccessAdapter adapter = new DataAccessAdapter();
IEntityFields2 fields = new EntityFields2(1);
fields.DefineField(BOHelper.CarFields.Registration, 0);
RelationPredicateBucket bucket = new RelationPredicateBucket((BOHelper.CarFields.Registration == _car.Registration) & (BOHelper.CarFields.Id != _car.Id));
int regCount = adapter.GetDbCount(fields, bucket);

While monitoring the trace output, we discovered that LLBL (v.2.5. Final) creates 2 SQL statements to retrieve this count:

[16:17:18]Method Enter: CreateRowCountDQ [16:17:18]Method Enter: CreateSelectDQ [16:17:18]Method Enter: CreateSelectDQ [16:17:18]Generated Sql query: Query: SELECT [Car].[Registration] FROM [Car] WHERE ( ( [Car].[Registration] = @Registration1 AND [Car].[ID] <> @Id2)) Parameter: @Registration1 : String. Length: 10. Precision: 0. Scale: 0. Direction: Input. Value: "SB-345-AB". Parameter: @Id2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1.

[16:17:18]Method Exit: CreateSelectDQ [16:17:18]Generated Sql query: Query: SELECT COUNT(*) AS NumberOfRows FROM (SELECT [Car].[Registration] FROM [Car] WHERE ( ( [Car].[Registration] = @Registration1 AND [Car].[ID] <> @Id2))) TmpResult Parameter: @Registration1 : String. Length: 10. Precision: 0. Scale: 0. Direction: Input. Value: "SB-345-AB". Parameter: @Id2 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1.

[16:17:18]Method Exit: CreateRowCountDQ

Why is this so?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 15-Feb-2008 16:54:45   

The first is the subquery of the second, so 1 query is executed, 2 queries are generated (the trace is inside the DQE, which is called twice simple_smile ). If you want to see which query gets executed use the ORMPersistence tracer instead.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 48
Joined: 14-Feb-2008
# Posted on: 15-Feb-2008 17:11:49   

Thank you! I feel kinda silly now simple_smile