Need to Run SQL Query at the Beginning of Every Month

Greetings Experts,

We need to create a SQL query to run at the beginning of every month. This query will need to check the Total Sum of a field, and then write that value to another table.

We will then create a report in SSRS 2005 to query this new table.

Question: What is the best way to run an SQL query on a scheduled basis.
LVL 8
pzozulkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

momi_sabagCommented:
using a sql server job
0
adlink_laCommented:
Create an Integration Servicess package and then schedule it in a job to run once a month.
0
BrandonGalderisiCommented:
http:#22787303 is correct.  To clarify it is a SQL Server Agent Job.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pzozulkaAuthor Commented:
Does is only allow to run stored procedures. I took a snap shot of the Window I am referring to and attached it below. I tested my query and it works OK in SQL Management Studio, but when I try to run it through SQL Server Agent Job it fails.

I also attached my query below. Its pretty straight forward, but it fails in Server Agent with the following error:
Executed as user: NT AUTHORITY\SYSTEM. Invalid column name 'Current'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'Over30'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'Over60'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'Over90'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'Over120'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'Over180'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'TotalDue'. [SQLSTATE 42S22] (Error 207)  Invalid column name 'Entered'. [SQLSTATE 42S22] (Error 207).
USE RB8
--Declare Variables
DECLARE @Current DEC(11,2)
DECLARE @Over30 DEC(11,2)
DECLARE @Over60 DEC(11,2)
DECLARE @Over90 DEC(11,2)
DECLARE @Over120 DEC(11,2)
DECLARE @Over180 DEC(11,2)
DECLARE @TotalDue DEC(11,2)
 
--Set each variable to equal individual time intervals (ie. current, 30, 60, 90, etc.)
SELECT 
@Current = (SELECT SUM(I.Balance)
FROM RB8.dbo.Invoices I JOIN RB8.dbo.Contacts C ON C.ContactNo = I.BillContactNo
                JOIN RB8.dbo.Firms F ON F.FirmNo = C.FirmNo
WHERE I.InvType = 1120 AND
I.Balance > 0 AND
I.PostDate IS NOT NULL AND
I.InvDate BETWEEN DATEADD(Day,-30,GETDATE()) AND GETDATE()),
@Over30 = (SELECT SUM(I.Balance)
FROM RB8.dbo.Invoices I JOIN RB8.dbo.Contacts C ON C.ContactNo = I.BillContactNo
                JOIN RB8.dbo.Firms F ON F.FirmNo = C.FirmNo
WHERE I.InvType = 1120 AND
I.Balance > 0 AND
I.PostDate IS NOT NULL AND
I.InvDate BETWEEN DATEADD(Day,-60,GETDATE()) AND DATEADD(Day,-30,GETDATE())),
@Over60 = (SELECT SUM(I.Balance)
FROM RB8.dbo.Invoices I JOIN RB8.dbo.Contacts C ON C.ContactNo = I.BillContactNo
                JOIN RB8.dbo.Firms F ON F.FirmNo = C.FirmNo
WHERE I.InvType = 1120 AND
I.Balance > 0 AND
I.PostDate IS NOT NULL AND
I.InvDate BETWEEN DATEADD(Day,-90,GETDATE()) AND DATEADD(Day,-60,GETDATE())),
@Over90 = (SELECT SUM(I.Balance)
FROM RB8.dbo.Invoices I JOIN RB8.dbo.Contacts C ON C.ContactNo = I.BillContactNo
                JOIN RB8.dbo.Firms F ON F.FirmNo = C.FirmNo
WHERE I.InvType = 1120 AND
I.Balance > 0 AND
I.PostDate IS NOT NULL AND
I.InvDate BETWEEN DATEADD(Day,-120,GETDATE()) AND DATEADD(Day,-90,GETDATE())),
@Over120 = (SELECT SUM(I.Balance)
FROM RB8.dbo.Invoices I JOIN RB8.dbo.Contacts C ON C.ContactNo = I.BillContactNo
                JOIN RB8.dbo.Firms F ON F.FirmNo = C.FirmNo
WHERE I.InvType = 1120 AND
I.Balance > 0 AND
I.PostDate IS NOT NULL AND
I.InvDate BETWEEN DATEADD(Day,-180,GETDATE()) AND DATEADD(Day,-120,GETDATE())),
@Over180 = (SELECT SUM(I.Balance)
FROM RB8.dbo.Invoices I JOIN RB8.dbo.Contacts C ON C.ContactNo = I.BillContactNo
                JOIN RB8.dbo.Firms F ON F.FirmNo = C.FirmNo
WHERE I.InvType = 1120 AND
I.Balance > 0 AND
I.PostDate IS NOT NULL AND
I.InvDate < DATEADD(Day,-180,GETDATE())),
@TotalDue = @Current + @Over30 + @Over60 + @Over90 + @Over120 + @Over180
 
USE AR
INSERT INTO AR.dbo.AgedAR ("Current","Over30","Over60","Over90","Over120","Over180","TotalDue","Entered")
VALUES (@Current,@Over30,@Over60,@Over90,@Over120,@Over180,@TotalDue,GETDATE())

Open in new window

Job-Step-Properties.bmp
0
BrandonGalderisiCommented:
you can have ad-hoc sql or procs.  but you don't want to have the "USE" statement for the db in there.  If you're getting those errors, it's either not running in the right db or the columns don't exist.

you can insert into a different db without changing to it (like you're doing with the AR database at the end.
0
pzozulkaAuthor Commented:
OK I got rid of all the "USE" in the query. Still same error message. The only thing about hte columns is I had to put quotes around them. The column "Current" is being HIGHLIGHTED in BLUE, so it must think its a system function or a clause statement or something. So I put all my column names in double-quotes because it was erroring out as seen below: I tried both ways, and they work fine when querying outside of the SQL Server Agent Job

INSERT INTO AR.dbo.AgedAR ("Current",Over30,Over60,Over90,Over120,Over180,TotalDue,Entered)
VALUES (@Current,@Over30,@Over60,@Over90,@Over120,@Over180,@TotalDue,GETDATE())
***************************
INSERT INTO AR.dbo.AgedAR ("Current","Over30","Over60","Over90","Over120","Over180","TotalDue","Entered")
VALUES (@Current,@Over30,@Over60,@Over90,@Over120,@Over180,@TotalDue,GETDATE())


Should I perhaps make this into a stored procedure, since it works when executed in a normal environment.
0
adlink_laCommented:
You would be better off to create a procedure and call the procedure from the job.
0
BrandonGalderisiCommented:
use brackets [] not ".

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.