Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to Run SQL Query at the Beginning of Every Month

Posted on 2008-10-23
8
Medium Priority
?
433 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

971 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