跪求一视图写法
表一:Dept
DeptCode(主键),DeptName
表二: Person
PersonCode(主键),PersonName,DeptCode
表三: View
PersonCode,DeptCode
View表与前两个表没有主外键关系,
只是PersonCode的值来自Person,DeptCode的值来自Dept,可重复
写视图,取出Dept中的DeptName,但过滤掉View
中的DeptName
例如:
Dept
1 人力
2 财务
3 市场
Person
05 张三
06 李四
View
05 1
那么得到的张三的结果就是
2 财务
3 市场
李四的是
1 人力
2 财务
3 市场
问题点数:100、回复次数:5Top
1 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-11-18 16:33:14 得分 20
无法生成这样的视图,因为视图所依赖的筛选条件[PersonName]是非确定性的,无法在视图创建时给定。Top
2 楼520zyb(欲穷千里目,更上一层楼)回复于 2005-11-18 16:58:09 得分 20
写存储过程:
select * from Dept D where not exists(select * from View V JOIN Person P ON V.PersonCode = P.PersonCode WHERE p.PersonCode = @PersonCode AND V.DeptCode = D.DeptCode)Top
3 楼love_AC(呵呵,来分享!)回复于 2005-11-18 17:07:20 得分 0
谢谢楼上,我也想到过.
存储过程好写,但这里必须要用到视图.Top
4 楼520zyb(欲穷千里目,更上一层楼)回复于 2005-11-18 17:29:01 得分 50
想把结果当作表来使用,那就改用函数吧:
CREATE FUNCTION fn
(@Id int)
returns @ret table(DeptCode int,DeptName varchar(20)
as
begin
insert @ret
select * from Dept D where not exists(select * from View V JOIN Person P ON V.PersonCode = P.PersonCode WHERE p.PersonCode = @PersonCode AND V.DeptCode = D.DeptCode)
return
endTop
5 楼iwl()回复于 2005-11-19 00:20:13 得分 10
为什么是必须 ,不放换个思路来考虑
楼上的用函数也是一个好的方式Top




