请问在SQL的分析器里面,如果我输入select * from table_aselect * from table_b这样会输出2个结果,但是在我的实际应用中我要做这样的查询
declare @a int
select @a = id from table_aselect @a as id
我只想要 最后一个语句的结果,因为需要用asp.net 读出结果,如果2个都输出结果的话那么就会得到第一个查询的结果,请问如何能实现第一个语句只查询不输出结果,只输出最后一条语句的结果啊???应该如何修改???谢谢

解决方案 »

  1.   

    只要执行select 就会返回数据,虽然可能记录行数为0。除非你不是用select。
      

  2.   

    declare @a int
    set @a = (select top 1 id from table_a)select @a as id
      

  3.   

    declare @id int
    declare tempcur cursor for select id from table_a
    fetch next from tempcur into @id
    while @@fetch_status=0
    begin
     select * from table_b where id=@id
     fetch next from tempcur into @idend
    close tempcur
    deallocate tempcur
    go
      

  4.   

    wangtiecheng(不知不为过,不学就是错!) 
    请问如果要给多个变量赋值呢?
    例如 select @a=field1, @b=field2
    用你的方法要写成
    set @a = select field1 from table_a)
    set @b = select field2 from table_a)
    ???
      

  5.   


    SET NOCOUNT ON
    select * from table_a
    SET NOCOUNT OFF
    select * from table_b
      

  6.   

    --这样?create table table_a(id int)
    insert table_a select 1
    insert table_a select 2
    insert table_a select 3
    gocreate table table_b(id int)
    insert table_b select 4
    insert table_b select 5
    insert table_b select 6
    godeclare @a int
    select @a=id
    from
    (
    select id from table_a
    union all 
    select id from table_b
    )tselect @a as id--result
    id          
    ----------- 
    6(1 row(s) affected)
      

  7.   

    set nocount on 
    这一句很重要.用了它,就会只返回最后一个结果.