0

我有以下查询,用于动态向表中插入数据,我检查了表的数据类型和我传递的参数是否相同,但仍然出现以下错误:



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

这是我的表结构

4

2 回答 2

0

你的 RoleID 是一个整数,所以你尝试做一个隐式转换:'insert into table ......' as int

你应该尝试做类似 'foo' + CONVERT(varchar,@RoleId) + ' ba' 而不是你得到的东西:'foo 24 ba'

http://technet.microsoft.com/en-us/library/aa226054%28v=sql.80%29.aspx

set @sql='insert into tblAccessRights (RoleId,CustomerId,IsCustomerExclusive,SubcustomerId,
IsSubCustomerExclusive,ClinicId,CreatedBy,CreatedDate,IsDeleted,IsActive)
values(' + CONVERT(varchar, @RoleId) + ',' + CONVERT(varchar, @客户 ID) + ...

于 2014-02-02T11:19:22.207 回答
0

样品:

   SELECT field,CONVERT(SUBSTRING_INDEX(field,'-',-1),UNSIGNED INTEGER) AS num
    FROM table
    ORDER BY num;


在mysql查询中将文本转换为数字

于 2014-02-02T11:00:27.163 回答