Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need to Run SQL Query at the Beginning of Every Month

Posted on 2008-10-23
8
Medium Priority
?
435 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 2000 total points
ID: 22798357
use brackets [] not ".

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

580 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