The Project Experience Small collection (2) ___ combination query stored procedure, error "varchar jbid=& #39;& #39; Failed when converting to data type int "

Source: Internet
Author: User

A stored procedure that writes a combination query today encounters the problem: failed to convert the varchar value ' SELECT * from View_dls_wxjd_customer where 1=1 and jbid = ' to the data type int. What the error details see: By Baidu: String variables and integer variable connections can not be connected with +.

So I took the cast () function to convert the integer variable DLSJB to a string, so the problem was overcome.

The correct code looks like the following:

ALTER PROCEDURE [dbo]. [Proc_sh_wxjdlist] @DH varchar (+),--       @dlsjb int,--    agent level Id@dlsname varchar (+)-   - Agent name Asbegindeclare @sqlStr varchar  --store SQL statement Set @sqlStr = ' SELECT * from View_dls_wxjd_customer WHERE 1 = 1 '--synthetic SQL statement, without entering the condition Select All if (@DH! = ")--inference is to write the number set @sqlStr = @sqlStr + ' and DH like '% ' + @DH + '% ' if (@DLSJB! =-1)-                    - Infer whether to select the agent level set @sqlStr = @sqlStr + ' and jbid = ' + ' +cast (@DLSJB as varchar (5)) + ' if (@DLSName! = ')--infer whether the agent name set is selected @sql STR = @sqlStr + ' and dlsname like '% ' + @DLSName + '% ' "EXEC (@sqlStr + ' ORDER by DH DESC ') END

   cast () is a conversion function, just like the CONVERT function we use in ASP. is a data type conversion to a data type. This article is precisely using the cast () function to force an integer variable into a string type, so that the string can be connected using +.

   
 

The project Experience Small collection (2) ___ Query stored procedure, error "varchar jbid=& #39;& #39; conversion to data type int fails "

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.