--數據如下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 第一句可以, 第二句就報錯
為什麽啊????
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 第一句可以, 第二句就報錯
為什麽啊????
解决方案 »
- sql语句怎么写
- Sql 2005 Reporting Services 矩阵中的小计如何自定义?
- 100分求sql语句
- 一个 分组求最大值的问题 哪位哥哥姐姐帮忙看下 谢谢了
- 请教大侠,msde2000怎么安装到一半就静止不动了?也没有安装上去!
- 各位师兄新手求助
- !请问 已经声明了为什么还让我声明?
- 如果字段中有记录如: 河口(上)这样的记录,怎么可以把这种记录只保留"("前面的字符,就是不要(上),只留下"河口"
- sql 中的 N'abc' 写法,怎么应用到参数(SqlParameter)中
- 一个sqlserver2005生成的查询计划中并行度的问题
- 关于数据插入的问题
- QQ群召集爱学程序的人,汇编,数据结构,数据库,计算机组成
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
是為了解決以上這個貼子的--我寫的動態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
--打印結查一執行就報錯
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错到不知道哪个八国去了。
你想打什么,用文字说出来听听看。
--打印結果
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有什麽錯啊
单独使用
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 出现与第二句一样错误。