Solved

Need to Run SQL Query at the Beginning of Every Month

Posted on 2008-10-23
8
419 Views
Last Modified: 2011-10-19
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.
0
Comment
Question by:pzozulka
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22787303
using a sql server job
0
 
LVL 5

Expert Comment

by:adlink_la
ID: 22787313
Create an Integration Servicess package and then schedule it in a job to run once a month.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22787393
http:#22787303 is correct.  To clarify it is a SQL Server Agent Job.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 8

Author Comment

by:pzozulka
ID: 22798057
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
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22798099
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
 
LVL 8

Author Comment

by:pzozulka
ID: 22798334
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
 
LVL 5

Expert Comment

by:adlink_la
ID: 22798341
You would be better off to create a procedure and call the procedure from the job.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22798357
use brackets [] not ".

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question