July 25, 2012 at 9:58 am
HowardW (7/25/2012)[hr
Hmm, not sure I understand what you're asking. Yes, it would update if you changed a column in that row as any other deterministic function would - try it and see.
Maybe we don't understand each other because I don't understand how a function works (ya, I am that low).
For me, the @input means you have to give it "arguments" (I am guessing that I am wrong here), so how the tables know that @input1 is col1, @input2 is col2, etc for all the sixteen columns that it needs to get the max value from?
April 22, 2015 at 8:52 am
I know this is an old thread, but I want to share anyway.
The function uses schemabinding to make the computed column persisted.
USE TempDB
GO
IF OBJECT_ID('TempDB..#Test') IS NOT NULL
DROP TABLE #Test
GO
IF OBJECT_ID('dbo.ufn_GetHighestVal', 'FN') IS NOT NULL
DROP FUNCTION dbo.ufn_GetHighestVal
GO
CREATE FUNCTION ufn_GetHighestVal
(
@Val1 TinyInt,
@Val2 TinyInt = NULL,
@Val3 TinyInt = NULL,
@Val4 TinyInt = NULL,
@Val5 TinyInt = NULL,
@Val6 TinyInt = NULL,
@Val7 TinyInt = NULL,
@Val8 TinyInt = NULL
)
RETURNS TinyInt
WITH SCHEMABINDING
AS
BEGIN
DECLARE @RetVal TinyInt
SELECT @RetVal = MAX(V.Val) FROM (VALUES (@Val1),(@Val2),(@Val3),(@Val4),(@Val5),(@Val6),(@Val7),(@Val8)) V(Val)
RETURN ISNULL(@RetVal, 0)
END
GO
CREATE TABLE #Test
(
Val1 TinyInt,
Val2 TinyInt,
Val3 TinyInt,
Val4 TinyInt,
Val5 TinyInt,
Val6 TinyInt,
Val7 TinyInt,
Val8 TinyInt,
CompCol AS dbo.ufn_GetHighestVal(Val1, Val2, Val3, Val4, Val5, Val6, Val7, Val8) PERSISTED
)
INSERT #Test
SELECTTOP 10
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255),
ABS(CHECKSUM(NEWID()) % 255)
FROMsys.all_columns C1, sys.all_columns C2
SELECT* FROM #Test
GO
April 22, 2015 at 11:35 am
Why not just use MAX(columname) OVER () in any queries you have?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 23, 2015 at 12:50 am
Could you please demonstrate.
April 23, 2015 at 6:29 pm
Jeff Moden (4/22/2015)
Why not just use MAX(columname) OVER () in any queries you have?
I would have suggested a schema-bound iTVF instead of a sUDF, as in the end you'll be performing this over rows anyway. Just return a TABLE with one column (the max value).
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
April 23, 2015 at 7:10 pm
DennisPost (4/23/2015)
Could you please demonstrate.
Actually, I can't because I screwed up and misread. This thread is about a "GREATEST" function and I thought it was something else entirely. My most humble apologies. :blush:
--Jeff Moden
Change is inevitable... Change for the better is not.
April 24, 2015 at 4:25 am
LOL Jeff.
RBAR is good when reading posts. 😛
April 24, 2015 at 5:55 pm
DennisPost (4/24/2015)
LOL Jeff.RBAR is good when reading posts. 😛
+1 :hehe:
Don Simpson
Viewing 8 posts - 16 through 22 (of 22 total)
You must be logged in to reply to this topic. Login to reply