sqlserver 函數 詳細例子

 

  
  
  
  
  1. --兩種UDF  
  2.  
  3.  
  4. --標量函數  返回單個數據值,而且其類型是在return字句中定義的  
  5. create function SUMEmp2(@emp1 int)  
  6. returns  int   
  7. AS   
  8. begin  
  9. --聲明變量@emp2count ,存儲總值  
  10. declare @emp2Count int  
  11. select @emp2Count =SUM (emp2)  
  12. from dbo.pp where Emp1=@emp1  
  13. --若是變量爲空,從新賦值0  
  14. if(@emp2Count is null)  
  15. set @emp2Count =0 
  16. print @emp2count  
  17. return @emp2count  
  18. end  
  19.  
  20. --函數調用  
  21. select VerdorID,dbo.SUMEmp2(emp1) from pp  
  22.    
  23.  --PS :標量函數返回一個單獨的值,一般在列表和where子句中使用  
  24.  --標量函數位置  
  25.    
  26.  --查詢中: select 語句中  
  27.  -- where 或having 字句中  
  28.  -- 做爲 update 中set字句中  
  29.    
  30.  -- T-SQL語句: case表達式中  
  31.  -- print 語句中(只適用於字符串函數)  
  32.  -- 做爲存儲過程的return 語句(只適用於返回整數的標量函數)  
  33.     
  34.  -- 函數和存儲過程當中:做爲用戶自定義函數的return字句,前提是被調用的用戶函數返回的值可隱式轉換爲進行調用的函數的返回值類型  
  35.    
  36.    
  37.    
  38.    
  39.    
  40.    
  41.  ---表值函數  
  42.  --表值函數遵照與標量函數相同的規則,區別在於表值函數返回一個表做爲輸出  
  43.  ---通常在select 語句的from 字句中進行引用,並能夠與其餘表或視圖進行鏈接  
  44.    
  45.  
  46.  --內聯表值函數能夠實現參數化視圖的功能  
  47.  --視圖的一個侷限性是在建立視圖是,不容許在視圖中包括用戶提供的參數,一般能夠在調用視圖時提供的where字句解決問題  
  48.  --建立表值函數  
  49.  create function FunctionAA (@emp1 int)  
  50.  returns table  --return 指定表爲返回的數據類型  
  51.  as   
  52.  return(  
  53.  select A.* from pp A inner join pp B on a.VerdorID=b.VerdorID   
  54.  where a.Emp1=@emp1)--select 語句的結果定義了返回變量的格式  
  55.    
  56.  --函數的內容是一個單條的select語句,內聯函數使用的select語句與視圖中使用的select語句,受到一樣的限制  
  57.  --該函數的主體不須要包含在begin ..end 中  
  58.  --調用   
  59.  select * from FunctionAA(4)   
  60.    
  61.  --可在一般使用視圖的任何地方使用內聯表值函數  
  62.    
  63.    
  64.    
  65.  
  66.  --多語句表值函數 是視圖和存儲過程的結合,可以使用返回表的用戶定義函數來代替存儲過程或視圖  
  67.  create function functionBB (@sex varchar(20))  
  68.  returns @emptable table  ---returns 定義表爲返回值類型,並定義告終構(名稱和格式)  
  69.  (id int identity(1,1) primary key not null,name varchar(20),class varchar(20))  
  70.  AS  
  71.  begin --begin...end 界定函數主體  
  72.   if(@sex='1')  
  73.   begin  
  74.   insert into @emptable  
  75.   select name,('T'+class) from student3 where sex=1   
  76.   --或 select name,('T'+class) from student3 where sex='true' 
  77.   end   
  78.   else if(@sex='0')  
  79.   begin   
  80.   insert into @emptable  
  81.   select name,('F'+class) from student3 where sex='false' 
  82.   --或 select name,('F'+class) from student3 where sex=0 
  83.   end   
  84.   else  
  85.   begin  
  86.   insert into @emptable  
  87.   select name,('null'+class) from student3 where sex is null  
  88.   end  
  89.  return   
  90.  end  
  91.    
  92.  --調用  
  93.  select * from functionBB ('0')  
  94.     
  95.   ---表值函數返回一個表變量,並在from字句使用  
  96.    
  97.  
  98.  
  99. ---肯定性與非肯定性函數  
  100. --對於相同的輸入值函數,每次調用肯定性函數則返回相同的值 如:內置函數cos  
  101. --對於相同的輸入值函數,每次調用非肯定性函數則返回不一樣的值 如:內置函數getdate()  
  102. --一個函數是非肯定性仍是肯定性,決定了是否在該函數結果集上創建索引,以及可否在引用該函數  
  103. --的視圖上定義彙集函數, 若是一個函數是非肯定行的,就不能索引該函數的結果  
相關文章
相關標籤/搜索