Solved

Need to Run SQL Query at the Beginning of Every Month

Posted on 2008-10-23
8
413 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now