--數據如下create table student(
stu_id varchar(10),
stu_name nvarchar(10),
class_id varchar(10),
class_name varchar(20)
)
insert into student select '1001',   '张建明',   '200205012',  'HBS-I-0602'
union all select '1002',   '王楠',     '200408008',  'HBS-III-0503'
union all select '1003',   '吴郝',     '200304015',  'HBS-II-0701'
union all select '1004',   '邱源',     '200506023',  'HBS-I-0902'create table score(
cour_id varchar(10),
cour_name varchar(10),
score int,
stu_id varchar(10)
)
insert score select 'C01',     'Java',      89,    1001
union all select 'C02',     'Oracle',    85.5,  1001
union all select 'C03',     'C++',       65,    1003
union all select 'C04',     'C#',        78,    1004--SQL 語句如下--相同列名可以
 select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score
--相同列名就報錯
select stu_name, class_id, class_name, tmp.* 
from student
inner join ( 
 select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score
 )tmp on student.stu_id=tmp.stu_id 第一句可以, 第二句就報錯
為什麽啊????

解决方案 »

  1.   

    改成这样
    select a.stu_name, a.class_id, class_name, tmp.* 
    from student a
    inner join ( 
     select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score
     )tmp on a.stu_id=tmp.stu_id 
      

  2.   

    select相当于取名,from相当与从人群中找人
      

  3.   

    http://community.csdn.net/Expert/topic/5126/5126450.xml?temp=.620907
    是為了解決以上這個貼子的--我寫的動態SQLdeclare @sql nvarchar(4000)set @sql = 'select stu_name, class_id, class_name, tmp.* 
    from student
    inner join (  select top 1  stu_id=''1001'', 'select @sql=@sql+'cour_name='+quotename(cour_name, '''')+',', 
     @sql=@sql+'score='+cast(score as nvarchar)+',' from score where stu_id='1001'set @sql=left(@sql, len(@sql)-1)
    set @sql=@sql+' from score )tmp on student.stu_id=tmp.stu_id 'print @sql
    --打印結果
    select stu_name, class_id, class_name, tmp.* 
    from student
    inner join (  select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score )tmp on student.stu_id=tmp.stu_id 
    --打印結查一執行就報錯
      

  4.   

    --打印結果
    select stu_name, class_id, class_name, tmp.* 
    from student
    inner join (  select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score )tmp on student.stu_id=tmp.stu_id 
    这句sql错到不知道哪个八国去了。
    你想打什么,用文字说出来听听看。
      

  5.   

    不是最外一层的select,不允许出现重复列名
      

  6.   

    oop80(RedSky) ( ) 信誉:99    Blog  2006-11-02 16:51:00  得分: 0  
     
     
       --打印結果
    select stu_name, class_id, class_name, tmp.* 
    from student
    inner join (  select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score )tmp on student.stu_id=tmp.stu_id 
    这句sql错到不知道哪个八国去了。
    你想打什么,用文字说出来听听看。
      
     
    -- 是 @sql 變量的 print 結果, 這句SQL有什麽錯啊
      

  7.   

    我估计
    单独使用
    select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 from score 
    生成一个结果集,虽然其中字段名称有一样的,但是不会有影响。把这个结果集与其他表做连接,就要把这个结果集查入到临时表空间里去,这样就要受到SQL SERVER的约束。
    我改了一下第一句,以证明上面假设。
    select top 1  stu_id='1001', cour_name='Java',score=89,cour_name='Oracle',score=85 into #t from score 出现与第二句一样错误。