我有以下查询,用于动态向表中插入数据,我检查了表的数据类型和我传递的参数是否相同,但仍然出现以下错误:
4 1 消息 245,级别 16,状态 1,第 43 行将 varchar 值“插入到 tblAccessRights(RoleId、CustomerId、IsCustomerExclusive、SubcustomerId、 IsSubCustomerExclusive、ClinicId、CreatedBy、CreatedDate、IsDeleted、IsActive)
值(”转换为数据时转换失败输入整数。
这是我的查询请检查
declare @SubcustomerId varchar(max)
set @SubcustomerId = '4,5'
declare @IsSubCustomerExclusive varchar(max)
set @IsSubCustomerExclusive = '1,0'
declare
@RoleId int ,
@CustomerId int,
@IsCustomerExclusive int,
@ClinicId varchar(max)=null,
@CreatedBy int,
@CreatedDate datetime,
@ModifiedBy int,
@ModifiedDate datetime,
@IsDeleted bit,
@IsActive bit,
@RoleName varchar(50)
set
@RoleId =24
set @IsCustomerExclusive =0
set @ClinicId =null
set @CreatedBy=null
set @CreatedDate =null
set @ModifiedDate =null
set @IsDeleted=0
set @IsActive =1
set @RoleName='ts1'
declare @sql varchar(max)
while(len(@SubcustomerId) > 0 and LEN(@IsSubCustomerExclusive)>=0)
begin
print left(@SubcustomerId, charindex(',', @SubcustomerId+',')-1)
print left(@IsSubCustomerExclusive, charindex(',', @IsSubCustomerExclusive+',')-1)
if(LEN(@IsSubCustomerExclusive)=0 )
begin
set @IsSubCustomerExclusive='0'
end
set @sql='insert into tblAccessRights (RoleId,CustomerId,IsCustomerExclusive,SubcustomerId,
IsSubCustomerExclusive,ClinicId,CreatedBy,CreatedDate,IsDeleted,IsActive)
values('+@RoleId+','+@CustomerId+','+@IsCustomerExclusive+','+left(@SubcustomerId, charindex(',', @SubcustomerId+',')-1)+',
'+left(@IsSubCustomerExclusive, charindex(',', @IsSubCustomerExclusive+',')-1)+','+@ClinicId+','+@CreatedBy+','+@CreatedDate+','+@IsDeleted +','+@IsActive+')'
print @sql
--exec(@sql)
set @SubcustomerId = stuff(@SubcustomerId, 1, charindex(',', @SubcustomerId+','), '')
set @IsSubCustomerExclusive = stuff(@IsSubCustomerExclusive, 1, charindex(',', @IsSubCustomerExclusive+','), '')
end
我在查询中做错了????,请帮助
pid int
RoleId int
CustomerId int
IsCustomerExclusive int
SubcustomerId varchar(MAX)
IsSubCustomerExclusive varchar(MAX)
ClinicId varchar(MAX)
CreatedBy varchar(50)
CreatedDate datetime
ModifiedBy varchar(50)
ModifiedDate datetime
IsDeleted bit
IsActive bit
这是我的表结构