Home All Groups Group Topic Archive Search About

Execute SQL based on number rows in two tables

Author
25 Nov 2005 6:46 PM
Pasha
I have a table that I would like to backup each morning only if the number of
records in this table is greater than in another table.  How can achieve
this?  Basically, I would like it to be like this:

IF (rowcount in A) > (rowcount in B) then
Execute SQL
ELSE --NOTHING
END IF

Author
25 Nov 2005 7:03 PM
Ole Kristian Bangås
"=?Utf-8?B?UGFzaGE=?=" <Pa***@discussions.microsoft.com> wrote in
news:DA8B4552-A94A-4974-9C4A-9A639CC7C1EC@microsoft.com:

> I have a table that I would like to backup each morning only if the
> number of records in this table is greater than in another table.  How
> can achieve this?  Basically, I would like it to be like this:
>
> IF (rowcount in A) > (rowcount in B) then
> Execute SQL
> ELSE --NOTHING
> END IF

Something like this?

if ((select count(*) from sys.tables) > (select count(*) from sys.views))
begin
  print 'More tables than views'
end

--
Ole Kristian Bangås
MCT, MCDBA, MCDST, MCSE:Security, MCSE:Messaging
Author
25 Nov 2005 7:25 PM
Pasha
Below is what I was able to create.  Not sure if it is the best solution
though:

if ((select rows FROM sysindexes WHERE id = OBJECT_ID('Daily_Fin_Apps') AND
indid < 2) > (select rows FROM sysindexes WHERE id =
OBJECT_ID('BACKUP_Daily_Fin_Apps') AND indid < 2))
begin
    truncate table [BACKUP_Daily_Fin_Apps]
    insert into [BACKUP_Daily_Fin_Apps]
    select * from [Daily_Fin_Apps]
end


Thanks,
Pasha

Show quote
"Ole Kristian Bangås" wrote:

> "=?Utf-8?B?UGFzaGE=?=" <Pa***@discussions.microsoft.com> wrote in
> news:DA8B4552-A94A-4974-9C4A-9A639CC7C1EC@microsoft.com:
>
> > I have a table that I would like to backup each morning only if the
> > number of records in this table is greater than in another table.  How
> > can achieve this?  Basically, I would like it to be like this:
> >
> > IF (rowcount in A) > (rowcount in B) then
> > Execute SQL
> > ELSE --NOTHING
> > END IF
>
> Something like this?
>
> if ((select count(*) from sys.tables) > (select count(*) from sys.views))
> begin
>   print 'More tables than views'
> end
>
> --
> Ole Kristian Bangås
> MCT, MCDBA, MCDST, MCSE:Security, MCSE:Messaging
>

AddThis Social Bookmark Button