我创建了以下查询:
SELECT Title, sum(No_Of_Copies) as 'Total Copies'
FROM BOOK_COPIES bc
join BOOK_AUTHORS ba
on bc.BookId=ba.BookId
join BOOK b
on b.BookId=bc.BookId
join LIBRARY_BRANCH lb
on bc.BranchId=lb.BranchId where AuthorName='George Orwell' and BranchName='Central'
group by b.Title
从这个查询我想创建一个程序,可以搜索任何作者或分支名称的书籍副本数量,所以我创建了这个:
CREATE PROC GetTotalCopies @AuthorName varchar(100), @BranchName varchar(100)
AS
SELECT Title, sum(No_Of_Copies) as 'Total Copies'
FROM BOOK_COPIES bc
join BOOK_AUTHORS ba
on bc.BookId=ba.BookId
join BOOK b
on b.BookId=bc.BookId
join LIBRARY_BRANCH lb
on bc.BranchId=lb.BranchId where AuthorName='@AuthorName' and BranchName='@BranchName'
group by b.Title
但是当我使用作者姓名和分支名称运行它时,我得到空表。谁知道为什么? 我从以下流程图创建了这个数据库: https://www.learncodinganywhere.com/learningmanagementsystem/links/07_DB/SQL_Drill.pdf 使用SQL Server 2008。
答案 0 :(得分:3)
无需单引号。参数已经是字符串。
所以,写下这样的查询:
where AuthorName = @AuthorName and BranchName = @BranchName
注意:您可能需要考虑充当"所有值":
的参数where (AuthorName = @AuthorName or @AuthorName is null) and
(BranchName = @BranchName or @BranchName is null)
答案 1 :(得分:2)
删除参数周围的单引号。它将使参数成为字符串文字。虽然参数是字符串类型,但在评估参数时,参数不需要单引号
where AuthorName=@AuthorName and BranchName=@BranchName