English 中文(简体)
页: 1
原标题:How to use like in stored procedure with int parameter?

我曾利用2008年视觉演播室的“彩虹造物”工具建立一个储存的程序。 这是审查稿:

IF EXISTS (SELECT * 
             FROM sysobjects 
            WHERE name =  SelectQuery  AND user_name(uid) =  dbo )
    DROP PROCEDURE dbo.SelectQuery
GO

CREATE PROCEDURE dbo.SelectQuery
(
    @StudentID int
)
AS
    SET NOCOUNT ON;
SELECT        StudentID, StudentName, StudentPhone, StudentAddress, 
              StudentBirthDay, StudentDescription, StudentStatus
FROM            tbl_Student
WHERE        (StudentID LIKE  %  + @StudentID +  % )
GO

But when I tried to execute it, I got an error:

Error Message: Conversion fail when converting the value  %  to datatype int.

请帮助我!

最佳回答

You want to find rows where @StudentID is a substring of StudentID? If so

WHERE StudentID LIKE  %  + cast(@StudentID as varchar(10)) +  % 

应开展工作。

问题回答

%不能用于分类数据类型。 如果你有绝对学生数据,为什么需要一定百分比? 仅与=

如果你真的坚持使用%,那么,你就必须改变学生信息数据库的数据类型,使之变形或像这样。

我认为,你实际上需要这样做。

WHERE RIGHT(StudentID,12) LIKE  %  + RIGHT(@StudentID,12) +  % 

如果你想的话,Rright就暗中将非包.数据转换成var。 更适当的办法是明确使用CONVERT,但正如你所看到的那样,时间已经延长。

WHERE (VARCHAR(20),StudentID) LIKE  %  + CONVERT(VARCHAR(20),@StudentID) +  % 

As Martin Point out, LIKE/2006/10ly transformations the LHS. Don 我想,我曾经尝试过这种方式,但这样做是行之有效的。

declare @studentid int set @studentid = 205

;with tmp as (select 201102050001 studentid)
select * from tmp
WHERE StudentID LIKE  %  + RIGHT(@StudentID,12) +  % 

-> output: 201102050001

解决这一问题的一个简单办法是将学生信息数据库转换成一个老板。 Just use CONVERT(varchar, StudentID).





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签