A small problem with stored procedures

Source: Internet
Author: User

I used to write this stored procedure this afternoon. Encountered a number of problems, now resolved, to bask in the sun, to see if you have any better way, it is a matter of ...

This stored procedure is what I use to search for a user with a certain skill.

Stored procedure Create PROCEDURE [dbo].
    
[Selectuserbypageandsearch] @categoryid int, @sex int, @skillid int, @ishaved int, @pageindex int, @pagesize int As DECLARE @strSQL varchar (5000) DECLARE @whereSQL varchar (1000) Set @whereSQL = ' where 1=1 and dbo. Users.isskillopened=1 ' If @ishaved >0 set @whereSQL = @whereSQL + ' and dbo. Myskills.ishaved= ' +ltrim (STR (@ishaved)) + ' if @sex >=0 set @whereSQL = @whereSQL + ' and dbo. Users.sex= ' +str (@sex) + ' if @skillid >0 set @whereSQL = @whereSQL + ' and dbo. Myskills.sid= ' +str (@skillid) + ' if @skillid <=0 and @categoryid >=0 Set @whereSQL = @whereSQL + ' and dbo. Skills.categoryid= ' +str (@categoryid) + ' IF @pageindex = 1 BEGIN SET @strSQL = ' SELECT DISTINCT top ' + STR (@p agesize) + ' dbo. Users.id, dbo. Users.name, dbo. Users.nickname,dbo. Users.xuehao from dbo. Myskills INNER JOIN dbo. Skills on dbo. Myskills.sid = dbo. Skills.id INNER JOIN dbo. The Users on dbo. Myskills.uid = dbo. Users.id ' + STR (@whereSQL) + ' End ELSE is BEGIN SET @strSQL = ' SELECT DISTINCT top ' + STR (@pagesize) + ' dbo. Users.id, dbo. Users.name, dbo. Users.nickname,dbo. Users.xuehao from dbo. Myskills INNER JOIN dbo. Skills on dbo. Myskills.sid = dbo. Skills.id INNER JOIN dbo. The Users on dbo. Myskills.uid = dbo. Users.id ' + STR (@whereSQL) + ' and dbo. Users.id > select ISNULL (MAX ([id), 0) from (select Top ' +str (@pageindex-1) * @pagesize) + ' ID from [User] ' + STR (@whereS QL) + ' ORDER by id ' as A) ORDER by dbo. Users.id desc ' End Select @strSQL EXEC (@strSQL) go

After writing it, execute it ....

I'm using the following code

EXEC selectuserbypageandsearch 1,-1,0,0,1,10

There is a problem .... The level is too vegetable ... No way.. The error is displayed

Message 8114, Level 16, State 5, Process Selectuserbypageandsearch, line 27th

Error converting from data type varchar to float.

Well, I've seen this mistake, so revise it ... (although I do not know why to change this ... )

Put

Alter PROCEDURE [dbo].
    
[Selectuserbypageandsearch] @categoryid int, @sex int, @skillid int, @ishaved int, @pageindex int, @pagesize int As DECLARE @strSQL varchar (5000) DECLARE @whereSQL varchar (1000) Set @whereSQL = ' WHERE dbo. Users.isskillopened=1 ' If @ishaved >0 set @whereSQL = @whereSQL + ' and dbo. Myskills.ishaved= ' +ltrim (STR (@ishaved)) + ' if @sex >=0 set @whereSQL = @whereSQL + ' and dbo. Users.sex= ' +str (@sex) + ' if @skillid >0 set @whereSQL = @whereSQL + ' and dbo. Myskills.sid= ' +str (@skillid) + ' if @skillid <=0 and @categoryid >=0 Set @whereSQL = @whereSQL + ' and dbo. Skills.categoryid= ' +str (@categoryid) + ' IF @pageindex = 1 BEGIN SET @strSQL = ' SELECT DISTINCT top ' + STR (@p agesize) + ' dbo. Users.id, dbo. Users.name, dbo. Users.nickname,dbo. Users.xuehao from dbo. Myskills INNER JOIN dbo. Skills on dbo. Myskills.sid = dbo. Skills.id INNER JOIN dbo. The Users on dbo. Myskills.uid = dbo. Users.id ' + Convert (varchar, @whereSQL) + '--have @Wheresql all Modify the end ELSE BEGIN SET @strSQL = ' SELECT DISTINCT top ' + STR (@pagesize) + ' dbo. Users.id, dbo. Users.name, dbo. Users.nickname,dbo. Users.xuehao from dbo. Myskills INNER JOIN dbo. Skills on dbo. Myskills.sid = dbo. Skills.id INNER JOIN dbo. The Users on dbo. Myskills.uid = dbo. Users.id ' + Convert (varchar, @whereSQL) + ' and dbo. Users.id > select ISNULL (MAX ([id), 0) from (select Top ' +str (@pageindex-1) * @pagesize) + ' ID from [User] ' + Convert (var char, @whereSQL) + ' ORDER by id ' as A) ORDER by dbo. Users.id desc ' End Select @strSQL EXEC (@strSQL) go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.