sql server 視圖 的一個例子

這是一個 有點複雜的查詢。我如今 想把他封裝 成 視圖  其中  B.RecordID= 41 提供給 視圖外查詢。spa

                    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

而後 發現 結果 不同了。3d

 

這就是 視圖 須要注意的地方。他相似   select * from( select * from  tableName )View  這個樣子。當裏面的結果 集合 有 外鏈接的的時候 存在 空行的話,再在外面 包一層 ,空行天然 就過濾去了。code

因此 只能寫 成 :    select * from  view_UserRecord where RecordID=41 or RecordID is nullblog

相關文章
相關標籤/搜索