语句如下,经过游标处理后,似乎查询时,条件没有起到作用,我直接用insert的语句时,结果是我要的结果,是我的游标有问题吗?declare @select varchar(2000)
declare @id int
declare sunny cursor for
select cmid from tbl_cust where country_id='cn'
open sunny
fetch next from sunny into @id
while @@fetch_status=0
begin
set @select='insert into timelist(cust_name,keydesc,t_enddate) '
set @select=@select + 'select ct.cust_name,dt.keydesc,dt.t_enddate from tbl_cust as ct join tbl_date as dt on ct.cmid=dt.cmid and dt.cmid=' + convert(varchar(2),@id)
set @select + ' and dt.tmid in (select distinct tmid from tbl_pos_' + convert(varchar(2),@id) + ') where ct.cmid=' + convert(varchar(2),@id)
--print @select
exec (@select)
fetch next from sunny into @id
end
close sunny
deallocate sunny

解决方案 »

  1.   

    declare @select varchar(2000) 
    declare @id int 
    declare sunny cursor for 
    select cmid from tbl_cust where country_id='cn' 
    open sunny 
    fetch next from sunny into @id 
    while @@fetch_status=0 
    begin 
    set @select='insert into timelist(cust_name,keydesc,t_enddate) ' 
    set @select=@select + 'select ct.cust_name,dt.keydesc,dt.t_enddate from tbl_cust as ct join tbl_date as dt on ct.cmid=dt.cmid and dt.cmid=' + ltrim(@id)
    set @select + ' and dt.tmid in (select distinct tmid from tbl_pos_' + ltrim(@id) + ') where ct.cmid=' + ltrim(@id) 
    --print @select 
    exec (@select) 
    fetch next from sunny into @id 
    end 
    close sunny 
    deallocate sunny