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