-2

我尝试以这种方式编写搜索查询

alter procedure search  
   @Book_ID int,
   @Book_name varchar(50),
   @Category_type_ID int,
   @Number_of_copies int,
   @Author_ID int
as
    select * from Book_info
    where [Book_name] like '[textboxofsearch]%'

有一个书籍信息记录,我通过名称搜索书籍,为此我首先为其创建程序......记录就像这样

  bookid book_name            Category_type_ID Number_of_copies Author_ID
  ----------------------------------------------------------------------
  55     Software Engineering      2               150             39
  56     mbr                       5               150             44
  57     Marketing Management      5                33             40

当我这样检查时:

search mbr

它向我显示了这个错误:

消息 8114,级别 16,状态 5,过程搜索,第 0
行将数据类型 nvarchar 转换为 int 时出错。

4

3 回答 3

1

只需通过您不完整的过程语句,您的过程中的参数是:

   @Book_ID int,
   @Book_name varchar(50),
   @Category_type_ID int,
   @Number_of_copies int,
   @Author_ID int

如果您对 proc 的执行是:

search mbr

Then you have 2 issue. One is that all of your parameters are required and you are only sending in 1 parameter value mbr. The second is that it is seeing that first item mbr as invalid. Is mbr a variable (if so it needs to be @mbr) or a literal string "mbr" (if so it needs to be 'mbr'? In either case it is going south here because mbr is not an INT and/or it is malformed from some previous statement. To help answer this better you are going to have to give more info.

于 2013-11-08T19:28:01.920 回答
0

If you are returning all the column by the book name passed I dont know why you need all there other parameters in your stored Procedure but say if you has only one parameter @Book_Name and your proc will return all the details about all the books where book name is LIKE @Book_Name, you could do something like this

alter procedure search  
@Book_name varchar(50)
as
DECLARE @Sql NVARCHAR(MAX)

SET @Sql =    'select * from Book_info
              where [Book_name] like '''+ @Book_name +'%'''

Execute sp_executesql @Sql
于 2013-11-08T19:33:42.307 回答
0

The problem is the way of call because the other parameters don't have default values.

In the declaration you say: @Book_ID int, as first param. but you pass a text for the second param.

In other words use:

alter procedure search  
@Book_ID int = null,
@Book_name varchar(50) = null,
@Category_type_ID int = null,
@Number_of_copies int = null,
@Author_ID int = null
as

after try:

search @Book_name = 'xxxxx'

or change the order of params

alter procedure search  
@Book_name varchar(50) = null,   
@Book_ID int = null,  
@Category_type_ID int = null,
@Number_of_copies int = null,
@Author_ID int = null
as

and try:

search 'xxxxx'
于 2013-11-08T19:34:22.313 回答