SQL Server進階(十五)常用函數
阿新 • • 發佈:2019-01-12
mon har 標量 reat 類型 rom 入參 bold 常用函數
在SQL 2012基礎教程中列出子句是按照以下順序進行邏輯處理。
FROM WHERE GROUP BY HAVING SELECT ORDER BY
IF EXISTS (SELECT 1 FROM TableName WHERE UserCode = @UserCode) BEGIN UPDATE TableNameSET NotPushBeginTime = @NotPushBeginTime, NotPushEndTime = @NotPushEndTime WHERE UserCode = @UserCode END ELSE BEGIN INSERTINTO TableName (UserCode, NotPushBeginTime, NotPushEndTime, CreateTime) VALUES(@UserCode, @NotPushBeginTime, @NotPushEndTime, Getdate()) END
sql中的表值函數與標量值函數區別與用法
通俗來講:
聽名字就知道區別了
表值函數返回的是一張表結果,就和一個select查詢語句一樣,只不過裏面帶入了參數或者很復雜;
標量值函數返回的只是一個值
一 、表值函數又分為內聯函數與多語句函數
(1)內聯函數就是沒有函數主體表是單個 SELECT。
下面是一個不帶輸入參數的表值函數 create function tvpoints() returns table as return ( select * from tb_users );
(2)下面是帶參的內聯表值函數
對於多語句表值函數,在 BEGIN...END 語句塊中定義的函數體包含一系列 Transact-SQL 語句,這些語句可生成行並將其插入將返回的表中。 以下示例創建了一個表值函數. create function tvpoints() returns @points table (x float, y float) as begin insert @points values(1,2); insert @points values(3,4); return; end
(3)標量函數(返回一個具體類型的值而不是一張表了)
CREATE FUNCTION FUN_DataFormat (@strDate datetime) RETURNS varchar(20) AS BEGIN declare @date varchar(20) set @date = DATENAME(YY,@strDate)+‘年‘+Convert(VARCHAR,MONTH(@strDate))+‘月‘+Convert(VARCHAR,DAY(@strDate))+‘日‘ return @date END
可以通過select dbo.FUN_DataFormat(getdate())來使用。(用的時候一定要在函數前面添加一個dbo或者的話數據庫語句報錯的會提示不認識的)
SELECT * FROM dbo.Acceptance FOR XML PATH
SQL Server 中WITH (NOLOCK)淺析
https://www.cnblogs.com/kerrycode/p/3946268.html
SQL Server進階(十五)常用函數