HI,
我正在尝试在SQL Server 2005中创建以下函数。当我尝试对@FULLNAME2 & @FULLNAME1
中携带的值进行RTrim时,它会给出异常。要求争论Rtrim的异常,但它已经通过那里。
请注意我已经能够创建此功能,不包括Rtrim部分。
另一个问题是我需要在视图中调用此函数,我使用select names ('val1','val2')
调用它。它声明这不是一个公认的功能。但是我可以在sysobjects表中看到这个函数有'TF'类型。
请建议。
CREATE FUNCTION dbo.names( @CUSTID varchar(20),@effdt varchar(20))
RETURNS @FinalResults1 TABLE (Name1 nvarchar(254), Name2 nvarchar(254))
AS
BEGIN
DECLARE @FinalResults TABLE (Name254 nvarchar(254), SRNO nvarchar(3))
INSERT INTO @FinalResults
SELECT (C.NAME1),ROW_NUMBER() OVER(ORDER BY A.SEQ_NBR)
FROM PS_ARB_CU_CLST_STN A , PS_ARB_CU_STATIONS C
WHERE A.EFF_STATUS = 'A'
AND A.EFFDT = (
SELECT MAX(B.EFFDT)
FROM PS_ARB_CU_CLST_STN B
WHERE A.SETID = B.SETID
AND A.CUST_ID = B.CUST_ID
AND B.EFFDT <= @effdt)
AND A.SETID = C.SETID
AND A.ARB_STATION_ID =C.CUST_ID
AND A.CUST_ID = @CUSTID
AND C.EFFDT = (SELECT MAX(D.EFFDT) FROM PS_ARB_CU_STATIONS D WHERE C.CUST_ID = D.CUST_ID
AND D.SETID = C.SETID AND D.EFFDT <= @effdt)
ORDER BY A.SEQ_NBR
DECLARE @Name nvarchar(254), @FULLNAME1 nvarchar(128), @FREEZENAME1 nvarchar(10), @append NVARCHAR (254)
DECLARE @FULLNAME254 nvarchar(254), @FULLNAME2 nvarchar(128), @FREEZENAME2 nvarchar(10), @COUNT INT, @i INT
SET @Name = ''
SET @FREEZENAME1 = 'FALSE'
SET @FREEZENAME2 = 'FALSE'
SET @FULLNAME1 = ''
SET @FULLNAME2 = ''
SET @FULLNAME254 = ''
SET @COUNT = 0
SET @i = 0
SET @append = ''
SELECT @COUNT = COUNT(*) FROM @FinalResults
WHILE @i < @COUNT
BEGIN
IF @FULLNAME1 = ''
BEGIN
IF(LEN((SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/')<= 40 AND @FREEZENAME1 = 'FALSE' )
SET @FULLNAME1 = (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i + '/');
ELSE
SET @FREEZENAME1 = 'TRUE';
END
ELSE
BEGIN
IF (LEN(@FULLNAME1 +(SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/') <=40 AND @FREEZENAME1 = 'FALSE' )
SET @FULLNAME1 = (@FULLNAME1 +(SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/') ;
ELSE
BEGIN
SET @FREEZENAME1 = 'TRUE';
IF @FULLNAME2 = ''
BEGIN
IF (LEN((SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/')<= 40 AND @FREEZENAME2 = 'FALSE' )
SET @FULLNAME2 = ((SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/') ;
ELSE
SET @FREEZENAME2 = 'TRUE';
END
ELSE
BEGIN
IF (LEN(@FULLNAME2 +(SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/') <=40 AND @FREEZENAME2 = 'FALSE')
SET @FULLNAME2 = (@FULLNAME2 +(SELECT NAME254 FROM @FinalResults WHERE SRNO = @i )+ '/') ;
ELSE
SET @FREEZENAME2 = 'TRUE';
END
END
END
END
IF @append = ''
SET @append = (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i );
Else
SET @append = @append + '/'+ (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i );
SET @i = @i +1
If (Len(@append) < 40)
SET @FULLNAME1 = RTrim(@FULLNAME1, '/');
End
If ((Len(@append) > 40) And
(Len(@append) < 80))
SET @FULLNAME2 = RTrim(@FULLNAME2, '/');
End
INSERT INTO @FinalResults1 VALUES (@FULLNAME1, @FULLNAME2)
RETURN
END
答案 0 :(得分:1)
如果它是一个表值函数(返回一个表),你应该像这样调用它
select <fields> from dbo.name(<parameters>)
rtrim函数也不能按你想要的方式工作,它需要一个参数然后删除字符串右边的所有空格
您在调用RTRIM的条件中也缺少几个BEGIN,或者您也可以删除结束。
If (Len(@append) < 40)
BEGIN
SET @FULLNAME1 = RTrim(@FULLNAME1, '/');
End
If ((Len(@append) > 40) And (Len(@append) < 80))
BEGIN
SET @FULLNAME2 = RTrim(@FULLNAME2, '/');
End
我看了你的函数,而while循环有几个奇怪的东西,有一些代码的部分永远不会被执行,我试着重写一个等效函数,我希望这可以帮助你
CREATE FUNCTION dbo.names(@CUSTID varchar(20),@effdt datetime)
RETURNS @results TABLE (Name1 nvarchar(254), Name2 nvarchar(254))
AS
BEGIN
DECLARE rCursor CURSOR FOR
SELECT (C.NAME1) AS name
FROM PS_ARB_CU_CLST_STN AS A , PS_ARB_CU_STATIONS AS C
WHERE A.EFF_STATUS = 'A' AND
A.EFFDT = (
SELECT MAX(B.EFFDT)
FROM PS_ARB_CU_CLST_STN AS B
WHERE A.SETID = B.SETID AND
A.CUST_ID = B.CUST_ID AND
B.EFFDT <= @effdt
) AND
A.SETID = C.SETID AND
A.ARB_STATION_ID = C.CUST_ID AND
A.CUST_ID = @CUSTID AND
C.EFFDT = (
SELECT MAX(D.EFFDT)
FROM PS_ARB_CU_STATIONS D
WHERE C.CUST_ID = D.CUST_ID AND
D.SETID = C.SETID AND
D.EFFDT <= @effdt
)
ORDER BY A.SEQ_NBR
DECLARE @name nvarchar(254),
@fullname1 nvarchar(128),
@fullname2 nvarchar(128),
@append NVARCHAR (254);
SET @fullname1 = '';
SET @fullname2 = '';
SET @append = ''
OPEN rCursor;
FETCH NEXT FROM rCursor INTO @name
WHILE @@FETCH_STATUS = 0 AND LEN(@fullname1 + @name) < 40
BEGIN
SET @fullname1 = @fullname1 + '/' + @name
FETCH NEXT FROM rCursor INTO @name
END
WHILE @@FETCH_STATUS = 0 AND LEN(@fullname2 + @name) < 40
BEGIN
SET @fullname2 = @fullname2 + '/' + @name;
FETCH NEXT FROM rCursor INTO @name;
END
--Append is not used
WHILE @@FETCH_STATUS = 0
BEGIN
SET @append = @append + '/' + @name;
FETCH NEXT FROM rCursor INTO @name;
END
CLOSE rCursor;
DEALLOCATE rCursor;
INSERT INTO @results VALUES (@fullname1, @fullname2)
RETURN
END
GO
答案 1 :(得分:1)
函数RTRIM
用于从字符串的右侧删除空格;
DECLARE @string VARCHAR(20)
SET @string = ' text and text '
SELECT RTRIM(@string)
这将返回值:' text and text'
表值函数需要被视为select语句中的数据源:
SELECT * from dbo.values('val1', 'val2')