Tuesday, March 20, 2012

Repetitive SQL Tasks

Thanks in advance.

I am new to sql script area.

Below is my ugly program.

Help, please)

There should be a better code to handle these repetitive tasks.

/* DECLARE Part Omitted */

SET @.Job_Code_A = '(Job_Type = ''A'')';

SET @.Job_Code_B = '(Job_Type = ''B'')';

.... And so on

SET @.SQL1 = 'Select Count(*) FROM tblWorkorders WHERE' + @.Job_Code_A;

SET @.SQL2 = 'Select Count(*) FROM tblWorkorders WHERE' + @.Job_Code_B;

..... And so on

EXEC (@.SQL1);

EXEC (@.SQL2);

..... And so on

Why do you need dynamic SQL? You can do:

select o.Job_Type, count(*)

from tblWorkOrders as o

where o.Job_Type in ('A', 'B')

group by o.Job_Type;

|||

Thank you for reply.

I need to use three-level depth loops from outer conditions.

No comments:

Post a Comment