简体   繁体   中英

Error converting data type varchar to numeric and overflow

declare @Commission decimal(18,2)
select @Commission=percentage from Commission
declare @qry varchar(Max)

set @qry='select 5 +'+@Commission +''

EXEC(@qry)

Here

The Error converting data type varchar to numeric.

Don't pass values into dynamic SQL as strings. Instead, learn to use sp_executesql :

declare @Commission decimal(18, 2);

select @Commission = percentage
from Commission;

declare @qry varchar(Max);

set @qry='select 5 + @Commission';

exec sp_executesql @qry, 'N@Commission decimal(18, 2)', @Commission=@Commission;
set @qry='select 5 +'+convert(nvarchar(max),@FranchiseeCommission)+''

Try it like this:

SET @qry='select 5 +'+CAST(@FranchiseeCommission AS varchar(30))+''

Since your variable is a decimal, you have to cast it as varchar in order to combine it with your string.

SQL Server's implicit conversion rules makes it attempt to implicitly convert your varchar to decimal . You need to explicitly convert the decimal to varchar :

set @qry='select 5 +'+ CAST(@FranchiseeCommission as varchar(20))

如果2012+,总会有concat()

set @qry=concat('select 5 +',@Commission)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM