Link to home
Start Free TrialLog in
Avatar of jay_eire
jay_eireFlag for United States of America

asked on

Run Stored Procedure In Stages/Batches

Hi guys I have a large student image database which contains four tables:

I run this Stored-procedure to get counts of the images in the database based on ImageType and Image Area, the SP writes the values to my ImgCountsTypeAreaLocCity table

to run the SP i do i enter the ImageType and ImgArea e.g: exec GetImage 'group shot','Campus 3'

create procedure [GetImage](@type varchar(50),@place varchar(50)) as
begin
      insert ImgCountsTypeAreaLocCity(ImgTYpe2,ImgAreaLocCity,ImgCount)
      SELECT @type,@place,COUNT(*) AS ImgCount
      FROM ImageLibrary
      WHERE
      (ImgType2 = @type) AND (Area = @place or Location = '' or city = @place) AND Status = 'L'
end

I would like to be able to run the Stored-procedure in batches based on the dbo.ImageType ImgTypes , so if I want to Process image type 1 through to 100 today it will
loop through all the rows in the dbo.ImgArea table using the ImgTypes 1 to 100 until its finished, then the next day I can process dbo.ImageType ImgTypes 101-500

 how do i go about doing this? To run the stored-procedure I would just type exec GetImage 1, 100 then the next day it could be exec GetImage 101, 500



and so on how do i go about doing this?

here is a sample of the data in my tables
dbo.ImageLibrary
photoID      sName            Camera            status      ImgType1      ImgType2      Area            Location      City            Misc1
1      Student Group a      Sony SLR      L               NA      group shot      Campus 3      Campus 3      BOS            NULL
2      Student Group b Cannon Digi      L               NA      single            Building 5 nine      Boston            Building 5 nine      NULL
3      Student a      Sony SLR      D               NA       group shot      Campus 3      BOS            Campus 3      NULL
4      Student Group b      NIKON            L               NA      single            Building 5 nine      Building 5 nine Building 5 nine      NULL
5      Student Group d      Cannon Digi      L               NA      Group Animated      New York City      New York City      New York City      NULL
6      Student b      NIKON            L               NA      group shot      Campus 3      Campus 3      New York City      NULL
7      Student a      Cannon Digi       L               NA      groups 3 to 4      New York City      New York City      New York City      NULL
8      Student Group c      Cannon Digi      L               NA       group shot      Campus 3      Campus 3      BOS            NULL
9      Student a      NIKON            L               NA      Group Animated      Campus 3      Campus 3      BOS            NULL

dbo.ImageType
TypeID      ImgType
1      range group close
2      Angle Shot Close Up
3      group shot
4      single
5      Group Animated
6      double sports play
7      groups 3 to 4

dbo.ImgArea
AreaID ImgArea
1      Campus 3
2      New York City
3      Campus 2
4      Building 5 nine
5      Area 2 stairs
6      campus 1
7      florida beach lawn
8      outside lawn

dbo.ImgCountsTypeAreaLocCity
ID ImgTYpe2     ImgAreaLocCity  ImgCount
1 outside lawn  campus 3         25
2 outside lawn  campus 2         5
3 outside lawn  campus 9         0

ASKER CERTIFIED SOLUTION
Avatar of piba
piba

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jay_eire

ASKER

@piba thanks mate, this works a charm :) If 0 results are found is it possible not to write those values to the table or am i better just to write everything so I know it has run fully and remove the 0 results later?

You have thought me a few really neat tricks i didn't think were possible in SQL
Avatar of piba
piba

you could add a HAVING criteria

create procedure [GetImage](@type varchar(50),@place varchar(50)) as
begin
      insert ImgCountsTypeAreaLocCity(ImgTYpe2,ImgAreaLocCity,ImgCount)
      SELECT @type,@place,COUNT(*) AS ImgCount
      FROM ImageLibrary
      WHERE
      (ImgType2 = @type) AND (Area = @place or Location = '' or city = @place) AND Status = 'L'
      HAVING COUNT(*) > 0
end

b.t.w.   are the imagetypes really as varchar column in the database or are they really int numbers referencing the ImageType table with a ForeignKey?
to answer the question i think your better off filtering the 0 values in the first place, otherwise there will probably be a lot of 0 values.. kinda depending on the data and your requirement to "know" it was run and completed though..
they ImageTypes are Varchars in the database, is SQL case sensitive? if say one character is lowercase in the dbo.ImageType  ImgType but uppercase in dbo.ImageLibrary ImgType2  will the SP still match it?

Yes I agree mate
if SQL is if left default it is normally NOT case-sensitive in my experience, it does depend on the used database "collation" so if your using binary collation or Latin1_General_CS_AS instead of  Latin1_General_CI_AS then it IS CaseSensitive.  The _AS part stands for AccentSensitive.. some languages use accents and sort differently then i suppose..
Thanks Mate
Thanks for the points,

for the casesensitive part here an example with "on the fly" collation settings:

select 'casesensitive',1 where 'a' collate Latin1_General_CS_AS = 'A' collate Latin1_General_CS_AS
select 'NOT casesensitive',1 where 'a' collate Latin1_General_CI_AS = 'A' collate Latin1_General_CI_AS