我在查询分析器里写了个脚本。内容是利用表A里的au和ed查出表B里的内容。由于表A里有多条记录,所以通过变量@i循环实现。
正常来说,肯定应该有结果的。但是现在检索结果为0条declare @i int 
set @i=1 
while @i<=2 
begin 
select * from istpallnew where au in (select au from temp where id=@i) and ed in (select ed from temp where id=@i)
set @i = @i + 1 
end我的问题是:
1、语句错在哪里
2、如何打印中间变量谢谢,挺着急的!

解决方案 »

  1.   

    如果, 表temp里id <=2就是没什么记录, 当然检索结果为0了.要打印中间变量:print cast(@i as varchar)
    你的查询能不能写成:select a.* 
    from istpallnew a
    inner join temp b on a.au = b.au and a.ed = b.ed
    where b.id = @i
      

  2.   

    declare @某字段 as varchar
    set @某字段 = ''
    declare @i int 
    set @i=1 
    while @i <=2 
    begin 
    select @某字段 = 某字段 from istpallnew where au in (select au from temp where id=@i) and ed in (select ed from temp where id=@i) 
    set @i = @i + 1 
    print(@某字段)
    end 
      

  3.   

    我按照二楼的写法执行了,还是没有结果。我想试试如果用like。请问二楼如果要写成 a.au like b.au 该怎么写呢
      

  4.   

    a.au like '%'+b.au+'%' 
      

  5.   

    select a.* 
    from istpallnew a
    inner join temp b on a.au = b.au and a.ed = b.ed
      

  6.   

    我换成这种写法,检索结果还是为空declare @i int 
    set @i=1 
    while @i<=1 
    begin 
      select a.* from istpallnew a inner join temp b on a.au like b.au + '%' and a.ed like b.ed + '%' where b.id = @i
      set @i = @i + 1 
    end
      

  7.   

    结果出来了。谢谢大家
    我原来temp里的值是通过拷贝粘贴过去的,现在我用select into就得到结果了,谢谢!
    马上给分!