使用动态插入数据类型varchar插入数据时出错无法转换为int

时间:2014-02-02 10:55:13

标签: sql-server sql-server-2008-r2 dynamicquery

我有以下查询,用于动态地向表中插入数据,我检查了表和参数的数据类型,我通过两者是相同的,但仍然有以下错误:


4 1 Msg 245,Level 16,State 1,Line 43 将varchar值'insert转换为tblAccessRights(RoleId,CustomerId,IsCustomerExclusive,SubcustomerId,
)时转换失败 IsSubCustomerExclusive,ClinicId,CreatedBy,CreatedDate,请将isDeleted,IsActive)
values('到数据类型int。


这是我的查询plz检查

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  
我在查询中做错了????,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

这是我的表结构

2 个答案:

答案 0 :(得分:0)

样本:

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


convert text into number in mysql query

答案 1 :(得分: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

将@ sql ='insert插入tblAccessRights(RoleId,CustomerId,IsCustomerExclusive,SubcustomerId,
IsSubCustomerExclusive,ClinicId,CreatedBy,CreatedDate,请将isDeleted,IsActive)
值('+ CONVERT(varchar,@ RoleId)+','+ CONVERT(varchar,@ CustomerId)+ ...