i have following query,which is used to insert data to table dynamically,i have checked the data type of table and parameter which im passing both are same,but still having following error:
4
1
Msg 245, Level 16, State 1, Line 43
Conversion failed when converting the varchar value 'insert into tblAccessRights (RoleId,CustomerId,IsCustomerExclusive,SubcustomerId,
IsSubCustomerExclusive,ClinicId,CreatedBy,CreatedDate,IsDeleted,IsActive)
values(' to data type int.
this is my query plz check
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
m i doing wrong in query????,plz help
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
this is my table structure