程序中的参数不起作用
Parameters in Procedure not working
我创建了以下查询:
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。
删除参数周围的单引号。它将使参数成为字符串文字。虽然参数是字符串类型,但是参数在求值的时候不需要单引号
where AuthorName=@AuthorName and BranchName=@BranchName
不需要单引号。参数已经是字符串。
所以,这样写查询:
where AuthorName = @AuthorName and BranchName = @BranchName
注意:您可能需要考虑函数为 "all values":
的参数
where (AuthorName = @AuthorName or @AuthorName is null) and
(BranchName = @BranchName or @BranchName is null)
我创建了以下查询:
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。
删除参数周围的单引号。它将使参数成为字符串文字。虽然参数是字符串类型,但是参数在求值的时候不需要单引号
where AuthorName=@AuthorName and BranchName=@BranchName
不需要单引号。参数已经是字符串。
所以,这样写查询:
where AuthorName = @AuthorName and BranchName = @BranchName
注意:您可能需要考虑函数为 "all values":
的参数where (AuthorName = @AuthorName or @AuthorName is null) and
(BranchName = @BranchName or @BranchName is null)