我需要 用存储过程 处理 一些事情,并且输出了一条SQL语句。然后 在 创建一个视图,在视图中来调用 这个
下面是我写好的createprocProc_ReturnSql@userIdint,@sqlvarchar(1000)outputasdeclare@MgrClassva...
下面是我写好的
create proc Proc_ReturnSql
@userId int,@sql varchar(1000) output
as
declare @MgrClass varchar(500),@MgrClassed varchar(200)
select @MgrClass = MgrClass_Id from TeacherBaseInfo where userId = @userId
set @MgrClass = @MgrClass + ','
set @sql = 'select * from Condition where SSBJ in('
while(CharIndex(',',@MgrClass) <> 0)
begin
set @MgrClassed = substring(@MgRClass,1,CharIndex(',',@MgrClass))
set @MgrClass = replace(@MgrClass,@MgrClassed,'')
set @MgrClassed = substring(@MgrClassed,1,(CharIndex(',',@MgrClassed)-1))
set @MgrClassed = dbo.fun_split(@MgrClassed,'-',3)
set @sql = @sql + @MgrClassed +','
end
set @sql = @sql +')'
set @sql = replace(@sql,',)',')')
go
//视图这样创建不行啊
create view View_test
as
declare @sql varchar(1000)
exec Proc_ReturnSql 38636,@sql output
exec(@sql)
go 展开
create proc Proc_ReturnSql
@userId int,@sql varchar(1000) output
as
declare @MgrClass varchar(500),@MgrClassed varchar(200)
select @MgrClass = MgrClass_Id from TeacherBaseInfo where userId = @userId
set @MgrClass = @MgrClass + ','
set @sql = 'select * from Condition where SSBJ in('
while(CharIndex(',',@MgrClass) <> 0)
begin
set @MgrClassed = substring(@MgRClass,1,CharIndex(',',@MgrClass))
set @MgrClass = replace(@MgrClass,@MgrClassed,'')
set @MgrClassed = substring(@MgrClassed,1,(CharIndex(',',@MgrClassed)-1))
set @MgrClassed = dbo.fun_split(@MgrClassed,'-',3)
set @sql = @sql + @MgrClassed +','
end
set @sql = @sql +')'
set @sql = replace(@sql,',)',')')
go
//视图这样创建不行啊
create view View_test
as
declare @sql varchar(1000)
exec Proc_ReturnSql 38636,@sql output
exec(@sql)
go 展开
3个回答
展开全部
create proc Proc_ReturnSql
@userId int,@sql varchar(1000) output
as
declare @MgrClass varchar(500),@MgrClassed varchar(200)
select @MgrClass = MgrClass_Id from TeacherBaseInfo where userId = @userId
set @MgrClass = @MgrClass + ','
set @sql = 'select * from Condition where SSBJ in('
while(CharIndex(',',@MgrClass) <> 0)
begin
set @MgrClassed = substring(@MgRClass,1,CharIndex(',',@MgrClass))
set @MgrClass = replace(@MgrClass,@MgrClassed,'')
set @MgrClassed = substring(@MgrClassed,1,(CharIndex(',',@MgrClassed)-1))
set @MgrClassed = dbo.fun_split(@MgrClassed,'-',3)
set @sql = @sql + @MgrClassed +','
end
set @sql = @sql +')'
set @sql = replace(@sql,',)',')')
exec (@sql) /*你这样写通过存储过程不就已经查询出来了?为啥还非要方到试图里面,而且试图不支持你那样的写法,你是希望用户查看视图时,数据会自动刷新。通过sql独立实现还是挺难的。如果你之用reporting server做报表展示的话,直接掉用这个存储过程就可以了,没必要再写视图*/
go
@userId int,@sql varchar(1000) output
as
declare @MgrClass varchar(500),@MgrClassed varchar(200)
select @MgrClass = MgrClass_Id from TeacherBaseInfo where userId = @userId
set @MgrClass = @MgrClass + ','
set @sql = 'select * from Condition where SSBJ in('
while(CharIndex(',',@MgrClass) <> 0)
begin
set @MgrClassed = substring(@MgRClass,1,CharIndex(',',@MgrClass))
set @MgrClass = replace(@MgrClass,@MgrClassed,'')
set @MgrClassed = substring(@MgrClassed,1,(CharIndex(',',@MgrClassed)-1))
set @MgrClassed = dbo.fun_split(@MgrClassed,'-',3)
set @sql = @sql + @MgrClassed +','
end
set @sql = @sql +')'
set @sql = replace(@sql,',)',')')
exec (@sql) /*你这样写通过存储过程不就已经查询出来了?为啥还非要方到试图里面,而且试图不支持你那样的写法,你是希望用户查看视图时,数据会自动刷新。通过sql独立实现还是挺难的。如果你之用reporting server做报表展示的话,直接掉用这个存储过程就可以了,没必要再写视图*/
go
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询