我正在尝试加入一串数据库值,但在此声明中:
SET @sql = ('select' + @var1 + '+' + convert(varchar,@var2) +
'from [VoucherType] where [DeletedBy] is null and [AutoID] = 1')
我收到错误说
数据类型nvarchar和bit在add中不兼容 操作
@var2
我的数据库中的[IsBirthdayVoucher]
是bit
类型,但我已经在其上应用了convert(varchar, @var2)
。请善意地告诉我做错了什么。感谢。
SPROC:
declare @Str as varchar(max)
declare @var1 as varchar(30)=null
declare @var2 as varchar(30)=null
declare @var3 as varchar(30)=null
declare @var4 as varchar(30)=null
declare @var5 as varchar(30)=null
declare @sql as varchar(355)
set @Str = '[Code][IsBirthdayVoucher][IsReusable]VouT'
Set @var1= (select LEFT(@Str, CHARINDEX(']', @Str)))
set @Str = SUBSTRING(@Str, CHARINDEX(']', @Str) + 1, LEN(@Str))
Set @var2 =(select LEFT(@Str, CHARINDEX(']', @Str)))
set @Str = SUBSTRING(@Str, CHARINDEX(']', @Str) + 1, LEN(@Str))
Set @var3 =(select LEFT(@Str, CHARINDEX(']', @Str)))
set @Str = SUBSTRING(@Str, CHARINDEX(']', @Str) + 1, LEN(@Str))
SET @sql = ('select' + @var1 + '+' + convert(varchar,@var2) + 'from [VoucherType] where [DeletedBy] is null and [AutoID] = 1')
EXEC(@sql)
答案 0 :(得分:5)
您未将[IsBirthdayVoucher]
转换为VARCHAR
,而是将@var2
转换为VARCHAR
。
尝试:
SET @sql = ('select ' + @var1 + ' + convert(varchar, ' + @var2 + ') from [VoucherType] where [DeletedBy] is null and [AutoID] = 1')
答案 1 :(得分:0)
试试这个:
SET @sql = ('select CONCAT(' + @var1 + ',' + @var2 + ') from [VoucherType] where [DeletedBy] is null and [AutoID] = 1')