导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

问个简单的问题?

zher 2003-12-25 04:18:48
DECLARE @sql varchar(8000)
SET @sql = 'select C_Name'
SELECT @sql = @sql + ',sum(case P_Name when ''' + P_Name + ''' then P_Value end) ['+ P_Name + ']'
FROM (SELECT DISTINCT P_Name FROM d) AS a
SELECT @sql = @sql + ' from d group by C_Name'
EXEC (@sql)


这是我一个纵向转横向的操作,在查询分析器中已经调试成功,那如果我要在页面上显示,我该如何写我的select 语句或如何连我的数据表。
还有那"As a"中的a是什么意思啊?

...全文
5 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
--上面的匆忙了一点,改:
create proc p_qry
as
set nocount on
DECLARE @sql varchar(8000)
SET @sql = 'select C_Name'
SELECT @sql = @sql + ',sum(case P_Name when ''' + P_Name + ''' then P_Value end) ['+ P_Name + ']'
FROM (SELECT DISTINCT P_Name FROM d) AS a
SELECT @sql = @sql + ' from d group by C_Name'
EXEC (@sql)
set nocount off
go

--在使用的时候,这样调用即可:
exec p_qry

回复
--改成存储过程就行了
create proc p_qry
as
set nocount on
DECLARE @sql varchar(8000)
SET @sql = 'select C_Name'
SELECT @sql = @sql + ',sum(case P_Name when ''' + P_Name + ''' then P_Value end) ['+ P_Name + ']'
FROM (SELECT DISTINCT P_Name FROM d) AS a
SELECT @sql = @sql + ' from d group by C_Name'
set nocount off
go

--在使用的时候,这样调用即可:
exec p_qry
EXEC (@sql)
回复
txlicenhe 2003-12-25
1:
create proc test
as
DECLARE @sql varchar(8000)
SET @sql = 'select C_Name'
SELECT @sql = @sql + ',sum(case P_Name when ''' + P_Name + ''' then P_Value end) ['+ P_Name + ']'
FROM (SELECT DISTINCT P_Name FROM d) AS a
SELECT @sql = @sql + ' from d group by C_Name'
EXEC (@sql)
go



2:
rs.open " exec test "

3:
FROM (SELECT DISTINCT P_Name FROM d) AS a

给(SELECT DISTINCT P_Name FROM d)指定一个别名,为a
回复
sdhdy 2003-12-25
rst.open "你的语句",cn


a是 记录集(SELECT DISTINCT P_Name FROM d)的别名
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告