新建了一张表 TB
create table tb
(
Name varchar2(10) ,
Subject varchar2(10) ,
Result number
);
insert into tb(Name , Subject , Result) values(‘张三’,’语文’,74)
insert into tb(Name , Subject , Result) values(‘张三’,’数学’,83)
insert into tb(Name , Subject , Result) values(‘张三’,’物理’,93)
insert into tb(Name , Subject , Result) values(‘李四’,’语文’,74)
insert into tb(Name , Subject , Result) values(‘李四’,’数学’,84)
insert into tb(Name , Subject , Result) values(‘李四’,’物理’,94)
想把数据转换成如下格式:
姓名 语文 数学 物理
张三 74 83 93
李四 74 84 94
写了一个存储过程:
create or replace procedure test1 as
declare @sQl varchar(8000) --声明一个变量
set @sql = 'select name as 姓名'
select @sql = @sql + ' , max(case subject when ”' + subject+ '“ then score else 0 end) [' + subject+ ']'
from (select distinct subject from Stu) as a
set @sql = @sql + ' from Stu group by name'
print @sql --打印生成的sql
exec(@sql) --执行该sql
怎么报错:
PROCEDURE TEST.TEST1 编译错误
错误:PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor external language
行:2
文本:declare @sql varchar(8000) --声明一个变量
请问题是什么原因? |
|