2014-02-07 58 views
1

我有表:計算從表中根據不同的柱從另一個tablecomputed柱的柱基於不同

create table Aprovizionari 
(
    ID_Aprovizionare int  identity(1,1) primary key, 
    CodCarte   char(3) foreign key references Carti(CodCarte), 
    CodLibrarie  char(3) foreign key references Librarii(CodLibrarie), 
    DataAprovizionare date default getdate(), 
    Cantitate   int  default 1 
) 
create table Returnari 
(
CodRet char(3) primary key, 
CodCarte char(3) foreign key references Carti(CodCarte), 
CodLibrarie char(3) foreign key references Librarii(CodLibrarie), 
CodEditura char(4) foreign key references Edituri(CodEditura), 
DataRet date, 
Cantitate int 
) 

我有遞減柱Cantitate(數量)從Aprovizionari(供應),而餘的觸發從另一張桌子Facturi(Invoices)添加Cantitate。

在Returnari(返回-of書籍),我應該有:

DataRet date,--this should be =DataAprovizionare+3 mounths 
Cantitate int--this should be=the remaining Cantitate(Quantity) from Aprovizionari at date=DataAprovizionare+3 mounts 
+0

是否在'Aprovizionari'記錄後3個月自動創建'Returnari'記錄? – OGHaza

+0

沒錯! :) – user3279198

回答

0

要做到這一點,你會需要檢查每天Aprovizionari記錄是3個月大。當你找到一個你會INSERTReturnari記錄。

爲此,您必須創建一個存儲過程,該存儲過程在執行時將創建Returnari記錄,然後計劃SQL Server代理作業以每天執行該過程。

該存儲過程將類似於:

CREATE PROCEDURE YourDailyProc (@Date DATE) 
AS 
    INSERT INTO Returnari (CodCarte, CodLibrarie, DataRet, Cantitate) 
    SELECT CodCarte, CobLibrarie, CAST(@Date AS DATE), Cantitate 
    FROM Aprovizionari 
    WHERE DataAprovizionare = CAST(DATEADD(MONTH, -3, @Date) AS DATE) 
GO 

目前尚不清楚,在那裏你會得到CodEditura但想必你只需要連接到另一臺。

現在你需要一份工作來執行EXEC YourDailyProc @Date=CURRENT_TIMESTAMP每一天。這個答案看起來像它有你需要做的所有信息 - how to schedule a job for sql query to run daily?