# 确定几个SQL Server栏中的最大值

CREATE TABLE [dbo].[MinMax](

[MinMaxID] [int] IDENTITY(1,1) NOT NULL,

[Value1] [int] NULL,

[Value2] [int] NULL,

[Value3] [int] NULL,

[Value4] [int] NULL,

CONSTRAINT [PK_MinMax]PRIMARY KEY CLUSTERED

(

[MinMaxID] ASC

)WITH (PAD_INDEX= OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

 MinMaxID Value1 Value2 Value3 Value4 1 1 3 5 7 2 10 5 7 9 3 3 13 27 4 4 10 81 17 54

```CREATE FUNCTION dbo.Min2
(
@value1 int= 0,
@value2 int= 0
)
RETURNS int
AS
BEGIN
DECLARE @Result int
SET @value1 = ISNULL(@value1,0)
SET @value2 = ISNULL(@value2,0)
IF @value1 <= @value2
SET @Result = @value1
ELSE
SET @Result = @value2
RETURN @Result
END
GO

CREATE FUNCTION dbo.Max2
(
@value1 int= 0,
@value2 int= 0
)
RETURNS int
AS
BEGIN
DECLARE @Result int
SET @value1 =ISNULL(@value1,0)
SET @value2 =ISNULL(@value2,0)
IF @value1 >= @value2
SET @Result = @value1
ELSE
SET @Result = @value2
RETURN @Result
END

GO```

SELECT dbo.Min2( 23, 34 )

SELECT dbo.Max2( 23,NULL)

SELECT dbo.Min2( NULL, 34 )

SELECT dbo.Max2( 23,NULL)

SELECT dbo.Max2( Value1, dbo.Max2( Value2, dbo.Max2( Value3, Value4 ) ) )

FROM dbo.MinMax

7

10

27

81

• 博文量
1319
• 访问量
2110449