SQL SERVER中可以在存储过程中创建视图,但要用动态SQL来执行。
create view view_name
as
...
不能直接使用, 可以改为exec('create view view_name
as
...').
你的可以改成
create proc ### as
begin
if exists (select * from sysobjects where name='***'and xtyp='v')
drop view ***;
exec('create view *** as
select A.xh,A.xm,sum(case when A.zy =B.value then B.code else 0) code
from A,B group by A.xh,A.xm
')
end
这样改应该也可以
create proc ### as
declare @sqlstr varchar(255)
select @sqlstr='
if exists (select * from sysobjects where name=''***''and xtyp=''v'')
drop view ***;
create view *** as
select A.xh,A.xm,sum(case when A.zy =B.value then B.code else 0) code
from A,B group by A.xh,A.xm
'
exec(@sqlstr)
go
汗。。这 你太想当然了吧
为什么不直接
create proc ### as
declare @sqlstr varchar(255)
if exists (select * from sysobjects where name='***'and xtyp='v')
drop view ***
create view *** as
select A.xh,A.xm,sum(case when A.zy =B.value then B.code else 0) code
from A,B group by A.xh,A.xm
go
PS。出现那个 对象B无效 是因为 exec(@sqlstr)这里,sqlstr是放在临时域中执行的,已经不在你当前这个域中了 所以就找不到b啦
把SQL语句提出来去SQL里面运行一下看看