sql server 视图 的一个例子

这是一个 有点复杂的查询。我现在 想把他封装 成 视图  其中  B.RecordID= 41 提供给 视图外查询。

                    create view view_UserRecord     
                         as
                         select RecordID,PR.CommonPoint+(PR.OutputValue*0.01) as 'AllPoint',  
                    (PR.OutputValue*0.01) as   'OutputConvertValue', AwardOrPunishment ,
                    PR.CommonPoint ,
                    PR.OutputValue ,
                    PR. RulesID ,
                    RulesName ,
                    RulesOrderNumber ,
                    RulesType ,
                    Unit,  isnull(Cnt,0) as 'Cnt', 
                    isnull(   isnull(Cnt,0)*(B.CommonPoint+(B.OutputValue*0.01)),0) as 'UserAllPoint', 
                      isnull( isnull(Cnt,0)*(B.OutputValue*0.01),0) as 'UserOutputValue'
             from   dbo.PointRules PR  
             left join dbo.UserRecordDetails  B on PR.RulesID = B.RulesID 

 

 select * from  view_UserRecord where RecordID=41

然后 发现 结果 不一样了。

 

这就是 视图 需要注意的地方。他类似   select * from( select * from  tableName )View  这个样子。当里面的结果 集合 有 外连接的的时候 存在 空行的话,再在外面 包一层 ,空行自然 就过滤去了。

所以 只能写 成 :    select * from  view_UserRecord where RecordID=41 or RecordID is null

posted @ 2015-07-21 17:11  pengbg  阅读(686)  评论(0编辑  收藏  举报