久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL函數從多列返回值

SQL Function to return value from multiple columns(SQL函數從多列返回值)
本文介紹了SQL函數從多列返回值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我一直在開發一些存儲過程,并且我一直在重復一部分代碼,這些代碼根據其他幾列派生出一列.因此,我沒有將這段代碼從一個存儲過程復制到另一個存儲過程,而是考慮使用一個函數來獲取輸入列并生成輸出列.

I've been developing a few stored procedure and I have been repeating a portion of codes that derives a column based on a few other columns. So instead of copy this piece of code from one stored procedure to another, I'm thinking of having a function that takes the input columns and produces the output columns.

基本上,函數如下:

SELECT columnA, columnB, columnC, myFunction(columnA, columnB) as columnD FROM myTable

如我們所見,此函數將 A 列和 B 列作為輸入,然后返回 D 列.

As we can see, this function will take column A and column B as inputs, then return column D.

但是,根據一些研究,使用這樣的 UDF(用戶定義)函數時似乎存在一些性能問題.真的嗎?處理這種情況的最佳方法是什么?

However, based on some research, it seems to have some performance issues when using UDF (user-defined) function like this. Is that true? What's the best way to handle this situation?

謝謝各位.

推薦答案

標量函數和多語句表值用戶定義函數會導致性能問題,因為它們隱式地將基于集合的操作轉換為基于游標的操作.

Scalar functions and multi statement table valued user defined functions can cause performance issues, because they implicitly turn your set based operation into a cursor based operation.

然而,內聯表值的用戶定義函數不會遇到這個問題.他們很快.

However, inline table valued user defined functions do not suffer from this problem. They're fast.

區別在于您如何聲明函數,以及它們內部的代碼是什么樣的.多語句函數執行它在罐頭上所說的 - 它允許您有多個語句.像這樣:

The difference is how you declare the fuction, and what the code looks like inside them. A multi statement function does what it says on the tin - it lets you have multiple statements. Like this:

create function slow() returns @t table(j int, k int) as 
begin
    declare @j int = 1; -- statement 1
    declare @k int = 2; -- statement 2
    insert @t values (@j, @k); -- statement 3
    return; -- statement 4
end

內聯表值函數不會返回填充在函數內部的命名表.它返回一個選擇語句:

An inline table valued function does not return a named table which is populated inside the function. It returns a select statement:

create function quick() returns table as
return
(
    select j = 1, k = 2
);

內聯表值函數可以內聯";進入外部 select 語句,與視圖的方式大致相同.當然,區別在于 UDF 可以接受參數,而視圖不能.

The inline table valued function can be "inlined" into the outer select statement, in much the same way as a view. The difference, of course, being that the UDF can take parameters, whereas a view cannot.

您還必須以不同的方式使用它們.使用交叉應用:

You also have to use them differently. Use cross apply:

select       t.columnA, t.columnB, u.j, u.k
from         MyTable t
cross apply  quick(t.columnA, t.columnB) u

如果不清楚 - 是的,在您的情況下,您只需要一個標量"返回值,但這只是一個表值函數,它返回單列和單行.因此,與其編寫標量函數,不如編寫一個執行相同工作的內聯表值函數,然后交叉應用它.

In case it's not clear - yes, in your case you only want a "scalar" value back, but that's just a table valued function which returns a single column and a single row. So instead of writing a scalar function, write an inline table valued function that does the same job, and cross apply it.

這篇關于SQL函數從多列返回值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 亚洲国产成人av好男人在线观看 | 久久99网站 | 久久久久久久久久久久亚洲 | 日操操夜操操 | 日韩欧美精品一区 | 国产激情视频 | 久久婷婷香蕉热狠狠综合 | 午夜成人免费视频 | 成人午夜免费在线视频 | 天堂一区 | 欧美在线一区二区三区 | 欧美日本一区二区 | 日韩av一区二区在线观看 | 在线亚洲一区 | 欧美精品一区三区 | 九九久久国产精品 | www国产成人免费观看视频,深夜成人网 | 欧美精品综合 | 欧美xxxx网站| 在线欧美激情 | 日韩黄| 看av网址| 欧美精品99 | 欧美在线视频一区二区 | 久久久精品一区二区三区 | 亚洲成人av在线播放 | 国产精品一区二区不卡 | 久久精品国产亚洲一区二区 | 国产999精品久久久 日本视频一区二区三区 | 中文字幕一区在线观看视频 | 福利视频一区 | 精品丝袜在线 | 欧美三级成人理伦 | 国产精品久久一区二区三区 | 午夜精品在线观看 | 日本三级电影免费观看 | 99精品亚洲国产精品久久不卡 | 91精品国产综合久久福利软件 | 午夜在线小视频 | 超碰人人做 | 日韩精品二区 |