2015-10-14 63 views
3

我有SQL 2012年我已經習慣了作業調度和自動化與SQL 2000一個可怕的時間,但我是新來的20122012 SQL作業問題

我寫了一個非常基本的腳本(見下文),無論我做什麼腳本都作爲工作的第一步失敗,但在查詢窗口中作爲獨立查詢運行良好。

任何幫助都會走很長的路。

declare @AccountID as int 
declare @ID as int 
declare @SaleDate as datetime 
declare @todaysdate as datetime 
declare @sentdate as datetime 
declare @writeToFile as int 
begin 
set @todaysdate = getdate() 
set @writeToFile = 0 
select @id = ID, @AccountID = AccountID, @SaleDate = SaleDate from TransHeader where Transheader.ID = 19680 
--print @@ROWCOUNT 

select AccountID, max(SurveySent) as "THEFINALDATE" from tbl_survey_email where AccountID = @AccountID AND datediff(dd, SurveySent, @todaysdate) <= 182 Group By AccountID 
set @writeToFile = @@ROWCOUNT 
--print @writeToFile 

if (@writeToFile = 0) 
    begin 

     insert into tbl_survey_email ('AccountID','TransactionID', 'datetime') VALUES (@AccountID, @ID, @SaleDate) 
    end 

end 

enter image description here

enter image description here

後亂投醫,使這項工作小時,但仍然沒有工作。我甚至改變了SQL Server代理服務作爲我運行(域管理)

我甚至改變我的腳本只是插入腳本,但仍然幾乎相同的錯誤。有任何想法嗎? enter image description here

enter image description here

enter image description here

+0

你知道,如果有正確的約束條件,你可以將整個工作重新寫入一個聲明中,對嗎? –

回答

4

你必須給NT服務用戶的權限來訪問數據庫中的表。您的Keith用戶擁有權限,但NT SERVICE不具備此權限。它應該有權限這樣的:

enter image description here

當然,在我的屏幕截圖,用戶是不完全一樣的東西,我認爲你是,但你的想法。

+3

或者,更好的是,爲該工作設置一個特定的用戶。 –