Showing posts with label based. Show all posts
Showing posts with label based. Show all posts

Monday, March 19, 2012

Choosing field from same row based on an aggregate.

I have a table with two columns. Let's call them Value and Hour. Looks like this:

Value Hour

4 9:00

3 11:00

6 2:00

2 12:00

I want to be able to make a total line and give the Max(Value) and the time it happened. What would be the function to get the Hour value based on the Max(Value). Just for example, for this one it would be Max(Value) = 6 and it's Hour would be 2:00.

Hi,

what about getting this right back from the database system with a query ?

SELECT Hour,MAX(Value)
From SomeTable
Group by Hour

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

|||I'm actually getting my data from a Custom Data Processing extension. I would need a way to get it with a function straight out of the resultant dataset.|||

Can't you make a calculated field with the expression = Max(Value)?

That can be put in the footer of the table (which will display '6' in this example). To get the hour belonging to the value with an expression ...I'm not so sure how to do that ... maybe with a switch expression, based on the textbox where you show the MAX(Value)?

Choosing database between MSDE and Access

hi,

I need to choose a database based on the following criteria (using .NET app):
1) a light but fully functional database, preferably with the support of store proc and constraints, less than 8000 transaction a day.
2) portable or the database can be export/import very easily
3) reliable and stable
4) least maintenance

I have two db in my mind, Access and MSDE?
Does anyone have some hand-ons experience on the above two? Or any other better suggestions?

Any advice is appreciated.

thanks,
bryanUsing MSDE will generally be more trouble-free. A file-based database on a Web server is not ideal.

Sunday, March 11, 2012

choosing a collation in SQL 2005

I have been searching for advice on the 'right' collation to choose in SQL 2005. I am US based and can assume any language required not supported by Latin1_General will be in unicode. I have SQL 2000 servers using SQL_Latin1_General_CP1_CI_AS and SQL_Latin1_General_CP850_CI_AS. The ones using CP850 will be converted to whatever collation is chosen in SQL 2005. I do not know the implications of choosing the default SQL collation as opposed to choosing Latin1_General_CI_AS. I have seen warning messages when installing analysis services if the SQL_Latin1_General_CP1_CI_AS is chosen saying that analysis services will use the colaltion Latin1_General_... . I also use non-updating subscribers in transactional replication currently in teh SQL 2000 environment and will continue to use replication in SQL 2005.

What are the implications of choosing the windows vs. SQL collation if the choice is between SQL_Latin1_General_CP1_CI_AS and Latin1_Gernal_CI_AS ? I do not want a collation mixture like I currently have in SQL 2000.

There's a helpful topic in Books Online that addresses this:

http://msdn2.microsoft.com/en-us/library/ms144260.aspx

Buck Woody

|||Thank you very much. This is the information I needed to see. I am not sure why searching BOL myself did not turn up this information.|||

If I am doing a fresh installation of SQL 2005 for a new application which will utilize databse engine, report server and analysis services is my best choice Latin_General_BIN2?

Thank you,
Alex Ivanoff

|||My understanding is binary collations do not sort in order you may expect. I'm not sure it's the best unless you know you need it. But, I'm not expert and am interested in an experts opinion.

choosing a collation in SQL 2005

I have been searching for advice on the 'right' collation to choose in SQL 2005. I am US based and can assume any language required not supported by Latin1_General will be in unicode. I have SQL 2000 servers using SQL_Latin1_General_CP1_CI_AS and SQL_Latin1_General_CP850_CI_AS. The ones using CP850 will be converted to whatever collation is chosen in SQL 2005. I do not know the implications of choosing the default SQL collation as opposed to choosing Latin1_General_CI_AS. I have seen warning messages when installing analysis services if the SQL_Latin1_General_CP1_CI_AS is chosen saying that analysis services will use the colaltion Latin1_General_... . I also use non-updating subscribers in transactional replication currently in teh SQL 2000 environment and will continue to use replication in SQL 2005.

What are the implications of choosing the windows vs. SQL collation if the choice is between SQL_Latin1_General_CP1_CI_AS and Latin1_Gernal_CI_AS ? I do not want a collation mixture like I currently have in SQL 2000.

There's a helpful topic in Books Online that addresses this:

http://msdn2.microsoft.com/en-us/library/ms144260.aspx

Buck Woody

|||Thank you very much. This is the information I needed to see. I am not sure why searching BOL myself did not turn up this information.|||

If I am doing a fresh installation of SQL 2005 for a new application which will utilize databse engine, report server and analysis services is my best choice Latin_General_BIN2?

Thank you,
Alex Ivanoff

|||My understanding is binary collations do not sort in order you may expect. I'm not sure it's the best unless you know you need it. But, I'm not expert and am interested in an experts opinion.

Choices for a backup server at other site over WAN

We wish to look into setting up a mirrored server at another site in case we
lose access to our head office.
The company is based in London, fairly near potential bomb targets etc.
There are three other locations, one having a 4GB leased line to head
office.
Assuming we are not going to jump to Yukon in the short term it looks like
log shipping is probably the way to go but are there other alternatives?
Note that currently the system is not set up well for disaster recovery.
Simple recovery model with over night backups. No experience here of running
proper log backups on recovery mode "full". There's probably some "select
into" in the code plus we do some pretty big index rebuilds over night.
Database is currently 80GB across raid 10 arrays of 18Gb disks.
Thanks
Paul CahillHi Paul,
We have the same problem.At that time we use to transfer all the table
and data on access and zip it and copied on wan.
Other option we tried was sending all those tables wich are updated
daily or transaction occured.we had 2000+tables.
the other option was that we send the backup to the otherplace and
created a log backup every 30 mins.
as the log backup completes it copies to the network.and as the log
backup copies restore takes place on that server.
hope u will find some way.
from
killer.|||One of our problems is that we have no idea how much volume we could be
generating.
I'd like to be able to take the index rebuilds out of the equation. If we do
log shipping that won't be an option.
"doller" <sufianarif@.gmail.com> wrote in message
news:1125486973.550827.119140@.g43g2000cwa.googlegroups.com...
> Hi Paul,
> We have the same problem.At that time we use to transfer all the table
> and data on access and zip it and copied on wan.
> Other option we tried was sending all those tables wich are updated
> daily or transaction occured.we had 2000+tables.
> the other option was that we send the backup to the otherplace and
> created a log backup every 30 mins.
> as the log backup completes it copies to the network.and as the log
> backup copies restore takes place on that server.
>
> hope u will find some way.
> from
> killer.
>|||Hi,
I think you can go for Transaction replication.
--
Herbert
"Paul Cahill" wrote:
> We wish to look into setting up a mirrored server at another site in case we
> lose access to our head office.
> The company is based in London, fairly near potential bomb targets etc.
> There are three other locations, one having a 4GB leased line to head
> office.
> Assuming we are not going to jump to Yukon in the short term it looks like
> log shipping is probably the way to go but are there other alternatives?
> Note that currently the system is not set up well for disaster recovery.
> Simple recovery model with over night backups. No experience here of running
> proper log backups on recovery mode "full". There's probably some "select
> into" in the code plus we do some pretty big index rebuilds over night.
> Database is currently 80GB across raid 10 arrays of 18Gb disks.
> Thanks
> Paul Cahill
>
>|||Thanks Herbert
We were thinking about this but could be quite complicated to
setup/admin/switch roles.
"Herbert" <Herbert@.discussions.microsoft.com> wrote in message
news:EF5870A3-1962-4F00-9C2F-11812AC579EA@.microsoft.com...
> Hi,
> I think you can go for Transaction replication.
> --
> Herbert
>
> "Paul Cahill" wrote:
>> We wish to look into setting up a mirrored server at another site in case
>> we
>> lose access to our head office.
>> The company is based in London, fairly near potential bomb targets etc.
>> There are three other locations, one having a 4GB leased line to head
>> office.
>> Assuming we are not going to jump to Yukon in the short term it looks
>> like
>> log shipping is probably the way to go but are there other alternatives?
>> Note that currently the system is not set up well for disaster recovery.
>> Simple recovery model with over night backups. No experience here of
>> running
>> proper log backups on recovery mode "full". There's probably some "select
>> into" in the code plus we do some pretty big index rebuilds over night.
>> Database is currently 80GB across raid 10 arrays of 18Gb disks.
>> Thanks
>> Paul Cahill
>>

Choices for a backup server at other site over WAN

We wish to look into setting up a mirrored server at another site in case we
lose access to our head office.
The company is based in London, fairly near potential bomb targets etc.
There are three other locations, one having a 4GB leased line to head
office.
Assuming we are not going to jump to Yukon in the short term it looks like
log shipping is probably the way to go but are there other alternatives?
Note that currently the system is not set up well for disaster recovery.
Simple recovery model with over night backups. No experience here of running
proper log backups on recovery mode "full". There's probably some "select
into" in the code plus we do some pretty big index rebuilds over night.
Database is currently 80GB across raid 10 arrays of 18Gb disks.
Thanks
Paul CahillHi Paul,
We have the same problem.At that time we use to transfer all the table
and data on access and zip it and copied on wan.
Other option we tried was sending all those tables wich are updated
daily or transaction occured.we had 2000+tables.
the other option was that we send the backup to the otherplace and
created a log backup every 30 mins.
as the log backup completes it copies to the network.and as the log
backup copies restore takes place on that server.
hope u will find some way.
from
killer.|||One of our problems is that we have no idea how much volume we could be
generating.
I'd like to be able to take the index rebuilds out of the equation. If we do
log shipping that won't be an option.
"doller" <sufianarif@.gmail.com> wrote in message
news:1125486973.550827.119140@.g43g2000cwa.googlegroups.com...
> Hi Paul,
> We have the same problem.At that time we use to transfer all the table
> and data on access and zip it and copied on wan.
> Other option we tried was sending all those tables wich are updated
> daily or transaction occured.we had 2000+tables.
> the other option was that we send the backup to the otherplace and
> created a log backup every 30 mins.
> as the log backup completes it copies to the network.and as the log
> backup copies restore takes place on that server.
>
> hope u will find some way.
> from
> killer.
>|||Hi,
I think you can go for Transaction replication.
--
Herbert
"Paul Cahill" wrote:

> We wish to look into setting up a mirrored server at another site in case
we
> lose access to our head office.
> The company is based in London, fairly near potential bomb targets etc.
> There are three other locations, one having a 4GB leased line to head
> office.
> Assuming we are not going to jump to Yukon in the short term it looks like
> log shipping is probably the way to go but are there other alternatives?
> Note that currently the system is not set up well for disaster recovery.
> Simple recovery model with over night backups. No experience here of runni
ng
> proper log backups on recovery mode "full". There's probably some "select
> into" in the code plus we do some pretty big index rebuilds over night.
> Database is currently 80GB across raid 10 arrays of 18Gb disks.
> Thanks
> Paul Cahill
>
>|||Thanks Herbert
We were thinking about this but could be quite complicated to
setup/admin/switch roles.
"Herbert" <Herbert@.discussions.microsoft.com> wrote in message
news:EF5870A3-1962-4F00-9C2F-11812AC579EA@.microsoft.com...[vbcol=seagreen]
> Hi,
> I think you can go for Transaction replication.
> --
> Herbert
>
> "Paul Cahill" wrote:
>

Choices for a backup server at other site over WAN

We wish to look into setting up a mirrored server at another site in case we
lose access to our head office.
The company is based in London, fairly near potential bomb targets etc.
There are three other locations, one having a 4GB leased line to head
office.
Assuming we are not going to jump to Yukon in the short term it looks like
log shipping is probably the way to go but are there other alternatives?
Note that currently the system is not set up well for disaster recovery.
Simple recovery model with over night backups. No experience here of running
proper log backups on recovery mode "full". There's probably some "select
into" in the code plus we do some pretty big index rebuilds over night.
Database is currently 80GB across raid 10 arrays of 18Gb disks.
Thanks
Paul Cahill
Hi Paul,
We have the same problem.At that time we use to transfer all the table
and data on access and zip it and copied on wan.
Other option we tried was sending all those tables wich are updated
daily or transaction occured.we had 2000+tables.
the other option was that we send the backup to the otherplace and
created a log backup every 30 mins.
as the log backup completes it copies to the network.and as the log
backup copies restore takes place on that server.
hope u will find some way.
from
killer.
|||One of our problems is that we have no idea how much volume we could be
generating.
I'd like to be able to take the index rebuilds out of the equation. If we do
log shipping that won't be an option.
"doller" <sufianarif@.gmail.com> wrote in message
news:1125486973.550827.119140@.g43g2000cwa.googlegr oups.com...
> Hi Paul,
> We have the same problem.At that time we use to transfer all the table
> and data on access and zip it and copied on wan.
> Other option we tried was sending all those tables wich are updated
> daily or transaction occured.we had 2000+tables.
> the other option was that we send the backup to the otherplace and
> created a log backup every 30 mins.
> as the log backup completes it copies to the network.and as the log
> backup copies restore takes place on that server.
>
> hope u will find some way.
> from
> killer.
>
|||Hi,
I think you can go for Transaction replication.
Herbert
"Paul Cahill" wrote:

> We wish to look into setting up a mirrored server at another site in case we
> lose access to our head office.
> The company is based in London, fairly near potential bomb targets etc.
> There are three other locations, one having a 4GB leased line to head
> office.
> Assuming we are not going to jump to Yukon in the short term it looks like
> log shipping is probably the way to go but are there other alternatives?
> Note that currently the system is not set up well for disaster recovery.
> Simple recovery model with over night backups. No experience here of running
> proper log backups on recovery mode "full". There's probably some "select
> into" in the code plus we do some pretty big index rebuilds over night.
> Database is currently 80GB across raid 10 arrays of 18Gb disks.
> Thanks
> Paul Cahill
>
>
|||Thanks Herbert
We were thinking about this but could be quite complicated to
setup/admin/switch roles.
"Herbert" <Herbert@.discussions.microsoft.com> wrote in message
news:EF5870A3-1962-4F00-9C2F-11812AC579EA@.microsoft.com...[vbcol=seagreen]
> Hi,
> I think you can go for Transaction replication.
> --
> Herbert
>
> "Paul Cahill" wrote:

Thursday, February 16, 2012

Checking for duplicate row before inserting

I need to know how to check/compareall fields in a row based on Cust Id before inserting a new record. As it is right now, if the Add/Update button is pressed continually, it increments the record number and adds a duplicate record. And you know you'll never stop people from double clicking a button that only needs to be clicked once.

I thought about just disabling the Update button after the insert occurs, then re-enable it if they go back and edit a field, but that's not a solid solution.I'd do it in your stored procedure.

You've got something like:


CREATE PROCEDURE dbo.spInsertRec
(
@.OneVal INT,
@.OtherVal VARCHAR(50)
)
AS
INSERT CustTable (SomeField, OtherField) VALUES (@.OneVal, @.OtherVal)
GO

I'd change it to something like:


CREATE PROCEDURE dbo.spInsertRec
(
@.OneVal INT,
@.OtherVal VARCHAR(50)
)
AS
IF EXISTS
(SELECT CustID FROM CustTable
WHERE SomeField = @.OneVal AND OtherField = @.OtherVal)
BEGIN
RETURN
END
ELSE
BEGIN
INSERT CustTable (SomeField, OtherField) VALUES (@.OneVal, @.OtherVal)
END
GO

That's pretty simple, obviously...You'll likely want to return something if a match is found, or even if the new record gets inserted...But it shows you the basics, I hope.

Regards,

Xander|||That looks like what I'm looking for. Since the add buttons click event runs 5 functions to insert data into other tables; if I call the SP in the first function, that should intercept the insertion process all together and break right?

InsertReportData()Would calling the SP here break and prevent the other functions from firing?
InsertCustInterest()
InsertCustVisits()
InsertCustQuotes()
InsertHistory()

I suppose that I could code a boolean to catch if the SP returns a duplicate using True/False, then use it to display a message in my errmsg label; notifying the user that they have attempted to store a duplicate record.

Thanks for the help, any other info would be greatly appreciated.
PD|||InsertReportData()Would calling the SP here break and prevent the other functions from firing?
InsertCustInterest()
InsertCustVisits()
InsertCustQuotes()
InsertHistory()

No, the fact that a record wasn't inserted will not, in and of itself prevent the other functions from firing.

You've got the idea though...If you pass a boolean value back, you can use it to decide whether to fire the other functions or not.

You could pass an output parameter from the stored procedure, for instance, or return a scalar value that lets you know what the stored procedure did, and wrap your four subsequent functions in an IF block, (perhaps even inside the first function on successful insertion?) and that should give you what you want.|||I got it to work, but it does prevent the updating of the fields that are not part of the SP in one of my functions (Strange).

I am going to check it out. I thought about appending the table to store all the field data and re-code the SP to check all the fields, fire it first, and then let it yield to the other functions if no duplicate exists.

Thanks.|||Well, it has turned into a proverbial nightmare. If I yield to the other insert functions from my SP, then duplicate records can be created in those tables. So I guess I am going to have to write SP's for each of those functions to prevent duplicates there as well.

I inherited this app a couple of months ago from an outside firm, and what a mess it has been.

Sunday, February 12, 2012

Check to see if a row exists in another table before insert

I have a stored procedure that selects invoices based on the date range

delete from BillingCurrent

insert into BillingCurrent (CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE (CONVERT(varchar(15), Invoices.Date, 112) BETWEEN CONVERT(varchar(15), DATEADD(d, - 30, GETDATE()), 112) AND CONVERT(varchar(15),

GETDATE(), 112))

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billingCurrent

delete from Billing30

insert into Billing30(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-60,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -30, GETDATE()), 112)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing3

Now, I need to check to see if the row exists in Billing30, if it exists in Billing 30 then I don't want it to insert into BillingCurrent.

You can use something along the lines of the code listed below.

Chris

IF EXISTS (SELECT 1 FROM Billing30 WHERE <insert criteria>)

BEGIN

--Insert the SQL that you want to execute if a Billing30 record is found

END

ELSE

BEGIN

--Insert the SQL that you want to execute if a Billing30 record is not found

END

|||

Ok I understand that and it's very helpful but,

for

IF EXISTS (SELECT 1 FROM Billing30 WHERE <insert criteria>)

BEGIN

--Insert the SQL that you want to execute if a Billing30 record is found

I want it to do Nothing if the row exists but I don't want it to exit because I need to do this for 5 tables

END

|||

There's nothing to stop you using multiple IF statements or even nesting them if you desire, see below - note that I've reversed the logic.

Chris

IF NOT EXISTS (SELECT 1 FROM Billing30 WHERE <insert criteria>)

BEGIN

--Insert the SQL that you want to execute if a Billing30 record is not found

END

IF NOT EXISTS (SELECT 1 FROM Table2 WHERE <insert criteria>)

BEGIN

--Insert the SQL that you want to execute if a Table2 record is not found

END

IF NOT EXISTS (SELECT 1 FROM Table3 WHERE <insert criteria>)

etc....

|||

ok so I can nest all of the if not exists and then after those just

if exists

end

to make it do nothing if the row already exists?

|||

In my previous example none of the code within the BEGIN END blocks will execute if at least one row meeting the relevant criteria exists in each of the tables that you are checking. There's no need to add any additional code to make SQL Server do nothing - if a condition fails then the code within the associated BEGIN END block will not be executed, it's as simple as that. If all of the conditions fail then the batch will complete without executing any of the code within any of the BEGIN END blocks.

It isn't clear from the description of your scenario whether you will need to use nested or multiple IF statements so I can't help any further in that respect without more info.

Chris

|||

Ok, Heres my SP

This prints out (because I use a relation from the billing tables to the InvoiceDetails Table) A Billing statement for each customer, problem is: if a customer has an invoice this month and last month then if prints out two invoices. I'm trying to get it to check each table first billing120 then billing90 then billing60..... So the customer row only gets inserted once.

delete from Billing120

insert into Billing120(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112)Between CONVERT(varchar(15),dateadd (d,-150,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -215, GETDATE()), 112)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing120

delete from Billing90

IF NOT EXISTS (SELECT CustomerID FROM Billing120)--WHERE <insert criteria>)

BEGIN

insert into Billing90(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-120,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -90, GETDATE()), 112)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing90

END

delete from Billing60

IF NOT EXISTS (SELECT CustomerID FROM Billing90)

BEGIN

insert into Billing60(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-90,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -60, GETDATE()), 112)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing60

End

delete from Billing30

IF NOT EXISTS (SELECT CustomerID FROM Billing90)

BEGIN

insert into Billing30(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-60,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -30, GETDATE()), 112)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing30

END

delete from BillingCurrent

IF NOT EXISTS (SELECT CustomerID FROM Billing90)

BEGIN

insert into BillingCurrent (CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE (CONVERT(varchar(15), Invoices.Date, 112) BETWEEN CONVERT(varchar(15), DATEADD(d, - 30, GETDATE()), 112) AND CONVERT(varchar(15),

GETDATE(), 112))

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billingCurrent

END

RETURN

|||

Maybe you should try a different approach then, see below. This approach allows you to analyze the contents of the tables before performing any INSERTs etc...

Chris

DECLARE @.Billing120Exists BIT

DECLARE @.Billing90Exists BIT

DECLARE @.Billing60Exists BIT

DECLARE @.Billing30Exists BIT

SET @.Billing120Exists = CASE WHEN EXISTS (SELECT CustomerID FROM Billing120) THEN 1 ELSE 0 END

SET @.Billing90Exists = CASE WHEN EXISTS (SELECT CustomerID FROM Billing90) THEN 1 ELSE 0 END

SET @.Billing60Exists = CASE WHEN EXISTS (SELECT CustomerID FROM Billing60) THEN 1 ELSE 0 END

SET @.Billing30Exists = CASE WHEN EXISTS (SELECT CustomerID FROM Billing30) THEN 1 ELSE 0 END

--Insert logic here that examines the values of the @.BillingExists variables and performs the appropriate actions.

--If you want you can declare additional variables to indicate whether or not rows have subsequently been inserted into one of the tables.

|||

Actually I ended up doing it this way,

delete from Billing120

insert into Billing120(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct Customers.CustomerID, Customers.Name,Customers.Address, Customers.City, Customers.State, Customers.Zip

FROM Invoices INNER JOIN

Customers ON Invoices.CustomerID = Customers.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112)Between CONVERT(varchar(15),dateadd (d,-150,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -215, GETDATE()), 112)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing120

delete from Billing90

BEGIN

insert into Billing90(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct A.CustomerID, A.Name,A.Address, A.City, A.State, A.Zip

FROM Invoices INNER JOIN

Customers A ON Invoices.CustomerID = A.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-120,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -90, GETDATE()), 112)

And not exists (select 1 from billing120 B where B.CustomerID = A.CustomerId)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing90

END

delete from Billing60

BEGIN

insert into Billing60(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct A.CustomerID, A.Name,A.Address, A.City, A.State, A.Zip

FROM Invoices INNER JOIN

Customers A ON Invoices.CustomerID = A.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-90,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -60, GETDATE()), 112)

And not exists (select 1 from billing120 B where B.CustomerID = A.CustomerId)

And not exists (select 1 from billing90 c where c.CustomerID = A.CustomerId)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing60

End

delete from Billing30

BEGIN

insert into Billing30(CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct A.CustomerID, A.Name,A.Address, A.City, A.State, A.Zip

FROM Invoices INNER JOIN

Customers A ON Invoices.CustomerID = A.CustomerID

WHERE CONVERT(varchar(15), Invoices.Date, 112) Between CONVERT(varchar(15),dateadd (d,-60,GETDATE()), 112)and CONVERT(varchar(15),dateadd (d, -30, GETDATE()), 112)

And not exists (select 1 from billing120 B where B.CustomerID = A.CustomerId)

And not exists (select 1 from billing90 c where c.CustomerID = A.CustomerId)

And not exists (select 1 from billing60 D where D.CustomerID = A.CustomerId)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billing30

END

delete from BillingCurrent

BEGIN

insert into BillingCurrent (CustomerID,[Name],Address,City,State,Zip)

SELECT Distinct A.CustomerID, A.Name,A.Address, A.City, A.State, A.Zip

FROM Invoices INNER JOIN

Customers A ON Invoices.CustomerID = A.CustomerID

WHERE (CONVERT(varchar(15), Invoices.Date, 112) BETWEEN CONVERT(varchar(15), DATEADD(d, - 30, GETDATE()), 112) AND CONVERT(varchar(15),

GETDATE(), 112))

And not exists (select 1 from billing120 B where B.CustomerID = A.CustomerId)

And not exists (select 1 from billing90 c where c.CustomerID = A.CustomerId)

And not exists (select 1 from billing60 D where D.CustomerID = A.CustomerId)

And not exists (select 1 from billing30 e where E.CustomerID = A.CustomerId)

select CustomerID,[Name],invoicetotal,invoiceID,[date] from billingCurrent

END

RETURN

Thanks for the Help!