Wednesday, March 7, 2012

Execute SQL based on number rows in two tables

I have a table that I would like to backup each morning only if the number o
f
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"examnotes" <Pasha@.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 Bangs
MCT, MCDBA, MCDST, MCSE:Security, MCSE:Messaging|||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
"Ole Kristian Bang?s" wrote:

> "examnotes" <Pasha@.discussions.microsoft.com> wrote in
> news:DA8B4552-A94A-4974-9C4A-9A639CC7C1EC@.microsoft.com:
>
> 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
>

No comments:

Post a Comment