sqldatareader myreader=new sqldatareader(select 出发地 from table,con);
string returnstr;
while(myreader hasread())
{
  returnstr=returnstr+myreader[0];
}
return returnstr;

解决方案 »

  1.   

    DECLARE @TABLE TABLE
    (
    出发地  VARCHAR(10),
    目的地  VARCHAR(10)
    )
    INSERT INTO @TABLE VALUES('上海','杭州')
    INSERT INTO @TABLE VALUES('杭州','徐州')
    INSERT INTO @TABLE VALUES('徐州','江西')
    INSERT INTO @TABLE VALUES('江西','湖北')
    INSERT INTO @TABLE VALUES('湖北','上海')--以下的可以写成存储过程DECLARE @RESULT VARCHAR(100)
    DECLARE @出发地 VARCHAR(10)SELECT TOP 1 @RESULT = 出发地 FROM @TABLE  DECLARE #myCursor CURSOR FOR SELECT 出发地 FROM @TABLE
    OPEN #myCursor
    FETCH NEXT FROM #myCursor INTO @出发地
    WHILE (@@FETCH_STATUS=0)
    BEGIN
    SET @RESULT = @RESULT + '-' + (SELECT 目的地 FROM @TABLE WHERE 出发地 = @出发地)
    FETCH NEXT FROM #myCursor INTO @出发地
    END
    CLOSE #myCursor
    DEALLOCATE #myCursorSELECT @RESULT
    --上海-杭州-徐州-江西-湖北-上海
      

  2.   

    我一直在考虑,如果不使用游标( 楼上 sbqcel(空空儿) 实现的),使用递归SELECT 以及 CASE 能否实现?
      

  3.   

    create table tt
    (
    fromAdd varchar(30),
    TOAdd varchar(30)
    )insert into tt
    select '上海','杭州' union all
    select '杭州','徐州' union all
    select '徐洲','江西' union all
    select '江西','湖北' union all
    select '湖北','上海'
    select top 1 fromAdd from tt 
    union all
    select toadd from tt结果
    ----------------------
    fromAdd                        
    ------------------------------ 
    上海
    杭州
    徐州
    江西
    湖北
    上海(所影响的行数为 6 行)
      

  4.   

    select top 1 fromAdd from tt 
    union all
    select toadd from tt
      

  5.   

    sbqcel(空空儿) 
    的回答有欠缺,楼主给出的条件是: 表的记录是没有次序的。
    如果表中的记录不是按一个目地地到别一个目地连起来的话,你的那段程序就不对了。正确的解决方案:DECLARE @TABLE TABLE
    (
    出发地  VARCHAR(10),
    目的地  VARCHAR(10)
    )
    INSERT INTO @TABLE VALUES('上海','杭州')
    INSERT INTO @TABLE VALUES('江西','湖北')
    INSERT INTO @TABLE VALUES('徐州','江西')
    INSERT INTO @TABLE VALUES('杭州','徐州')
    INSERT INTO @TABLE VALUES('湖北','上海')
    DECLARE @RESULT VARCHAR(100)
    DECLARE @出发地 VARCHAR(10)DECLARE @tem VARCHAR(10) --增加一个变量
    DECLARE @tem2 VARCHAR(10) --增加一个变量
    SELECT TOP 1 @RESULT = 出发地,@tem=目的地 FROM @TABLE where 出发地='上海'declare @c int
    select @c=count(0) from @TABLE
    while(@c>0)
    begin
    SELECT TOP 1 @tem2 ='-'+出发地,@tem=目的地 FROM @TABLE where 出发地=@tem
    set @RESULT =@RESULT +'-'+@tem2
    set @c=@c-1
    end
    SELECT @RESULT
      

  6.   

    create table stations
    (
    start nvarchar(50),
    [end] nvarchar(50)
    )insert into stations values(N'上海', N'杭州')
    insert into stations values(N'江西', N'湖北')
    insert into stations values(N'杭州', N'徐州')
    insert into stations values(N'湖北', N'上海')
    insert into stations values(N'徐州', N'江西')declare @start nvarchar(50), @next nvarchar(50), @result nvarchar(4000)
    select @start = N'上海', @next = '', @result = ''select @result = @start + ' - ' + [end], @next = [end] 
    from stations 
    where start = @startwhile @@rowcount > 0
    if @next <> @start
    begin
    begin
    select top 1 @result = @result + ' - ' + [end], @next = [end] 
    from stations 
    where start = @next
    end
    end print @resultdrop table stations
      

  7.   

    说说我的笨办法,见笑
    --在oracle数据库下,表名定为city 列名一为startid 列名二 endid
    Create Table city
    (
    startid  Varchar2(10) Null,
    endid  Varchar2(10) Null
    )INSERT INTO city VALUES('上海','杭州');
    INSERT INTO city VALUES('杭州','徐州');
    INSERT INTO city VALUES('徐州','江西');
    INSERT INTO city VALUES('江西','湖北');
    INSERT INTO city VALUES('湖北','上海');select a.startid||'-'||a.endid||'-'||b.endid||'-'||c.endid||'-'||d.endid||'-'||e.endid 
    from city a,city b,city c,city d,city e
    where a.endid=b.startid and b.endid=c.startid and 
    c.endid=d.startid and d.endid=e.endid and a.startid ='上海'
      

  8.   

    select a.startid||'-'||a.endid||'-'||b.endid||'-'||c.endid||'-'||d.endid||'-'||e.endid 
    from city a,city b,city c,city d,city e
    where a.endid=b.startid and b.endid=c.startid and 
    c.endid=d.startid and d.endid=e.startid and a.startid ='上海'
      

  9.   


    alter proc cursorTest
    as
    declare @start varchar(20),@destination varchar(20),@strA varchar(80),@temp varchar(20)
    declare @IsFirst int,@n int
    declare CursorA cursor
    for select * from journey
    open cursorA
    set @IsFirst=1
    set @n=0
    fetch next from cursorA into @start,@destination
    while @n<20
    begin
    if @IsFirst=1
    begin
    set @strA='上海'
    if @start='上海'
    begin
    set @strA=@strA + ' - '+ @destination
    set @IsFirst=0
    set @temp=@destination
    end
    end
    else if @start=@temp
    begin
    set @strA=@strA+' - '+@destination
    set @temp=@destination
    end
    fetch next from cursorA into @start,@destination
    set @n=@n+1
    end
    close cursorA
    deallocate cursorA
    print @strA
      

  10.   

    这道题要是纯粹用SQL语句来解决好像有点点的困难……像楼上那位兄弟说“有无回路”,光做判断就能把机器跑死……毕竟SQL语言对逻辑的判断及对数据读取的控制不如强语言那么好。
      

  11.   

    学习,不会。仅仅用sql确实很困难
      

  12.   

    declare @City table( Value1 nvarchar(50), Value2 nvarchar(50) ) insert into @City values( '上海', '杭州' )
    insert into @City values( '杭州', '徐州' )
    insert into @City values( '徐州', '江西' )
    insert into @City values( '江西', '湖北' )
    insert into @City values( '湖北', '上海' )


    select replace( replace( replace( ( select Value1 +' - '+ Value2 as [Value] from @City for xml auto ), '"/><_x0040_City Value="', ' - ' ), '<_x0040_City Value="', '' ), '"/>', '' )
    一条sql,不过需要sql2005
      

  13.   

    print ‘上海 - 杭州 - 徐州 - 江西 - 湖北 - 上海’
      

  14.   

    和车站的sql有些相似
    n年前邹老大已经给出解法了
    顺便说一句
    ls的全部不怎么合乎要求 呵呵
      

  15.   

    if object_id('tempdb..#temp') is not null
    drop table #tempselect 
    出发地='  ',
    目的地='  '
    into #tempdelete #tempinsert into #temp(出发地,目的地)values('上海','杭州')
    insert into #temp(出发地,目的地)values('杭州','徐州')
    insert into #temp(出发地,目的地)values('徐州','江西')
    insert into #temp(出发地,目的地)values('江西','湖北')
    insert into #temp(出发地,目的地)values('湖北','上海')
    select * from #temp
    declare @temp_total nvarchar(200)
    declare @temp_from nvarchar(10)set @temp_total='上海'select @temp_from=目的地 from #temp where 出发地=@temp_totalwhile(@temp_from is not null and @temp_from<>'上海')
    begin
    set @temp_total = @temp_total + '-'+ @temp_from
    select @temp_from=目的地 from #temp where 出发地=@temp_from
    endset @temp_total = @temp_total + '-'+ '上海'select @temp_total
      

  16.   

    berlin8600(柏林) 
     的这个梯归好啊
      

  17.   

    这个题目不太严谨,如果能够保证数据库的记录是有回路且无分支的数据,只是记录的顺序不同则解法如下Create Table city
    (
    name Varchar2(50) Null,
    score  Varchar2(50) Null
    )INSERT INTO city VALUES('上海','杭州');
    INSERT INTO city VALUES('杭州','徐州');
    INSERT INTO city VALUES('徐州','江西');
    INSERT INTO city VALUES('江西','湖北');
    INSERT INTO city VALUES('湖北','上海');declare
    nods varchar2(20);
    node varchar2(20);
    road varchar2(500); 
    i pls_integer:=0;
    lastnode varchar2(50);
    cursor vsf is 
     select name , score  from temp_total;
    begin 
         road := '';     
         for v_sf in vsf loop
           if i = 0 then
              nods := v_sf.name;
              node := v_sf.score;
              lastnode := node;
              road := road || nods ||'-'|| node || '-'; 
            else
             select t.name , t.score into nods,node 
             from  temp_total t where t.name = lastnode;
             lastnode := node;    
             road := road  || node || '-';    
             
           end if;
             
             i:=i+1;         
         end loop;
          dbms_output.put_line(road);
    end ;
      

  18.   


    --drop procedure sp_SearchPath--gocreate procedure sp_SearchPath
    @fromCity varchar(50)
    as
    begin
    declare @bakFromCity varchar(50),@toCity varchar(50),@resultstr varchar(500)
    set @bakFromCity=@fromCity
    set @toCity=''
    set @resultstr=@bakFromCity

    while @toCity<>@bakFromCity
    begin

    select @toCity=d_to from t3 where d_from=''+ @fromCity +''
    set @resultstr=@resultstr+'-'+replace(@toCity,' ','')
    set @fromCity=@toCity
    end

    select @resultstr
    endgoexec sp_SearchPath '上海'