CREATE TABLE _Transaction (
transaction_id int,
date_time datetime default sysdatetime(),
amount real,
description varchar(500),
responsible_party varchar(50),
CONSTRAINT _transaction_pk PRIMARY KEY(transaction_id),
CONSTRAINT check_res_prty CHECK(responsible_party IN ('[ATM]','[Teller]','[Bank]','[Standing Order]','[Cheque]','[On-Line]','[Other]'))
);
CREATE TABLE _Transaction_account(
account_no int,
transaction_id int,
affect_type varchar(50),
CONSTRAINT _Transaction_account_pk PRIMARY KEY(account_no,transaction_id),
CONSTRAINT _Transaction_account_fk1 FOREIGN KEY(transaction_id) REFERENCES _transaction(transaction_id),
CONSTRAINT _Transaction_account_fk2 FOREIGN KEY(account_no) REFERENCES account(account_no),
CONSTRAINT check_affect_type CHECK(affect_type IN ('[credit]','[debit]'))
);
我想寫一個存儲過程以上表這需要賬號和返回本月內該賬戶處理檢查次數。所以我寫了下面的存儲過程如何獲得當月在T-SQL的SQL Server(2008)
CREATE PROCEDURE pro_check_cheques (@accountNo int)
AS
BEGIN
DECLARE @chques int
SELECT @chques=COUNT(t.responsible_party)
FROM _Transaction t JOIN _Transaction_account ta ON
[email protected] AND ta.transaction_id=t.transaction_id
WHERE t.responsible_party = '[Cheque]' AND t.date_time=MONTH(GETDATE())
print @chques;
END
但這不行。當我執行sproc時總是得到0計數,但是當我刪除「AND t.date_time = MONTH(GETDATE())」的部分時,它工作正常。 請誰能幫助我? 謝謝
和t.date_time = MONTH(GETDATE()),你比較了一些與此日期時間... –
請注意,標籤獨立。也就是說,你不能合併多個標籤來創建一個概念。標籤'[stored]'和'[procedure]'與單個'[stored-procedure]'標籤不同。請務必閱讀選擇標籤時出現的說明! – Charles