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 "