0

我有一个关于 Transact SQL 函数的问题。我需要将两个 INT 值组合成一个 VARCHAR 值。例如下限 = '5',上限 = '15' 我的结果应该是“5 - 15”

表格如下所示: ID 下限 上限 1 5 15 2 3 19 等。

到目前为止的代码是这样的。剩下的就是结合两个结果:任何帮助将不胜感激!

CREATE FUNCTION [dbo].[GiveMeTheRange] (@argID INT)

RETURNS VARCHAR
AS
BEGIN
DECLARE @Range VARCHAR;
DECLARE @LowerLimit DOUBLE PRECISION;
DECLARE @UpperLimit DOUBLE PRECISION;


SELECT    @LowerLimit = [Lower Limit]
FROM         T_VormFactorKlassen
WHERE     (ID = (@argID)

SELECT    @UpperLimit = [Upper Limit]
FROM         T_VormFactorKlassen
WHERE     (ID = (@argID)


-- concattenate(@LowerLimit ' - ' @UpperLimit)  <----That ain't workin' 

RETURN @VormFactorKlasseNaam ;


END

谢谢

4

2 回答 2

1

你真的应该避免用标量函数做这种事情。也就是说,这应该有效:

CREATE FUNCTION [dbo].[GiveMeTheRange] (@argID INT)

RETURNS VARCHAR(23)   -- Always specify the length of the VARCHAR
AS
BEGIN
DECLARE @Range VARCHAR(23);
DECLARE @LowerLimit DOUBLE PRECISION;
DECLARE @UpperLimit DOUBLE PRECISION;


SELECT    @LowerLimit = [Lower Limit]
FROM         T_VormFactorKlassen
WHERE     ID = (@argID)

SELECT    @UpperLimit = [Upper Limit]
FROM         T_VormFactorKlassen
WHERE     ID = (@argID)


-- concattenate(@LowerLimit ' - ' @UpperLimit)  <----That ain't workin' 
SET @Range = CAST(@LowerLimit AS VARCHAR(10)) + ' - ' + 
             CAST(@UpperLimit AS VARCHAR(10))

RETURN @Range;


END
于 2013-09-23T14:47:02.560 回答
1

尝试这个:

SET @VormFactorKlasseNaam = CAST(@LowerLimit as varchar(5)) + ' - ' + 
     cast(@UpperLimit as varchar(5))  
于 2013-09-23T14:48:36.800 回答