exec('select * from ('+@sql+') A inner join 表2 B on A.name=B.name')--在另一帖已回
解决方案 »
- 安装SQL SERVER 2005 找不到dtcutil.dll
- 请问字段怎么作为函数的参数?
- 关于创建带参数的临时表的问题
- SQL的老鸟,帮我看看这个问题怎么解决
- 如何在触发器中更改文件名称?(生手问题,在线等,问题解决马上送分)
- 已经用工具把access传换成了sql脚本,再用mssql的dts把数据导入成功,怎么连接上asp?
- 在 C#.net里通过什么方法可以实时的检测到数据库表的的变化
- 关于 TRANSACTION 的问题请教!
- 安装问题,在线等
- 如何把一台服务器上的SQL SERVER中的一个数据库完全移植到另一台服务器上的SQL SERVER上
- 最后70分,全拿出来了,如要解决了,另外几个相关帖分一并送,郁闷!
- 事务日志满了,该怎么办?
set @sql = 'select Name'
select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'
from (select distinct Subject from CJ) as a
select @sql = @sql+' ,nation from CJ a,xx b where a.name=b.name group by a.name'
exec(@sql)
(
name varchar(10),
subject varchar(10),
result int
)create table xx
(
name varchar(10),
nation varchar(10)
)insert into cj select '张三', '语文', 80
insert into cj select '张三', '数学', 90
insert into cj select '张三', '物理', 85
insert into cj select '李四', '语文', 85
insert into cj select '李四', '数学', 92
insert into cj select '李四', '物理', 82insert into xx select '张三', '汉族'
insert into xx select '李四', '黎族'--语句
declare @sql varchar(4000)
set @sql = 'select a.Name'
select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'
from (select distinct Subject from CJ) as a
select @sql = @sql+',nation from CJ a,xx b where a.name = b.name group by a.name,b.nation'
exec(@sql)--结果
张三 90 85 80 汉族
李四 92 82 85 黎族
create table CJ(Name nvarchar(10), Subject nvarchar(10), Result int)
insert CJ select '张三', '语文', 80
union all select '张三', '数学', 90
union all select '张三', '物理', 85
union all select '李四', '语文', 85
union all select '李四', '数学', 92
union all select '李四', '物理', 82
create table xx(name nvarchar(10), nation nvarchar(10))
insert xx select '张三', '汉族'
union all select '李四', '黎族'declare @sql varchar(4000)
set @sql = 'select CJ.Name,xx.nation'
select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'
from (select distinct Subject from CJ) as a
select @sql = @sql+' from CJ left join xx on CJ.Name=xx.name group by CJ.Name, xx.nation'
exec(@sql)--result
Name nation 数学 物理 语文
---------- ---------- ----------- ----------- -----------
张三 汉族 90 85 80
李四 黎族 92 82 85