如何在 SQL Server 2005 中的函数中声明表

发布于 2024-11-09 09:54:29 字数 3480 浏览 0 评论 0原文

我需要创建一个函数,对要返回到 SQL 视图的数据进行某些处理。

我设计了如下功能,但收到错误消息

必须声明表变量 “@最终结果”

虽然我已将其定义为表。

你能帮我使这个函数可执行吗?

感谢您的帮助!

CREATE FUNCTION dbo.names(@CUSTID varchar(20), @effdt varchar(20))
RETURNS @FinalResults1 (Name1 nvarchar(254), Name2 nvarchar(254))
AS
BEGIN   
  DECLARE TABLE @FinalResults (Name254 nvarchar(254), SRNO nvarchar(3))

  CREATE TABLE @FinalResults (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

SELECT @COUNT = COUNT(*) FROM @FinalResults 

WHILE @i < @COUNT
BEGIN
  IF  @FULLNAME1 = ''  
    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 IF
  ELSE 
    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
      SET @FREEZENAME1 = 'TRUE';

    IF @FULLNAME2 = ''   
       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 IF
    ELSE
       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-IF
       END-IF
       END-IF
       END-IF

IF @append = '' 
            @append = (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i );
         Else
            @append = @append + '/'+ (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i );
          END-IF

     END-IF
SET @i = @i +1
END
END-WHILE

  If (Len(@append) < 40) 
      @FULLNAME1 = RTrim(@FULLNAME2, '/');
   End-If;
   If ((Len(@append) > 40) And
         (Len(@append) < 80)) 

      @FULLNAME2 = RTrim(@FULLNAME2, '/');
   End-If;

BEGIN
INSERT INTO #FinalResults1 VALUES ( @FULLNAME1, @FULLNAME2)
END
RETURN 

END 

GO 
 SELECT Name1 
 , Name2 
  FROM @FinalResults1

I need to create a function do to certain processing on data to be returned to a SQL view.

I have designed the function as below, but am getting error saying

Must declare the table variable
"@FINALRESULTS"

although I have defined it as table.

Can you please help me making this function executable.

Appreciate your help!!

CREATE FUNCTION dbo.names(@CUSTID varchar(20), @effdt varchar(20))
RETURNS @FinalResults1 (Name1 nvarchar(254), Name2 nvarchar(254))
AS
BEGIN   
  DECLARE TABLE @FinalResults (Name254 nvarchar(254), SRNO nvarchar(3))

  CREATE TABLE @FinalResults (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

SELECT @COUNT = COUNT(*) FROM @FinalResults 

WHILE @i < @COUNT
BEGIN
  IF  @FULLNAME1 = ''  
    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 IF
  ELSE 
    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
      SET @FREEZENAME1 = 'TRUE';

    IF @FULLNAME2 = ''   
       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 IF
    ELSE
       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-IF
       END-IF
       END-IF
       END-IF

IF @append = '' 
            @append = (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i );
         Else
            @append = @append + '/'+ (SELECT NAME254 FROM @FinalResults WHERE SRNO = @i );
          END-IF

     END-IF
SET @i = @i +1
END
END-WHILE

  If (Len(@append) < 40) 
      @FULLNAME1 = RTrim(@FULLNAME2, '/');
   End-If;
   If ((Len(@append) > 40) And
         (Len(@append) < 80)) 

      @FULLNAME2 = RTrim(@FULLNAME2, '/');
   End-If;

BEGIN
INSERT INTO #FinalResults1 VALUES ( @FULLNAME1, @FULLNAME2)
END
RETURN 

END 

GO 
 SELECT Name1 
 , Name2 
  FROM @FinalResults1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-11-16 09:54:29
DECLARE TABLE @FinalResults (Name254 nvarchar(254), SRNO nvarchar(3))


CREATE TABLE @FinalResults (Name254 nvarchar(254), SRNO nvarchar(3))

应该是

DECLARE @FinalResults TABLE  (Name254 nvarchar(254), SRNO nvarchar(3))

另外,函数的返回类型应该是

RETURNS @FinalResults1 TABLE (Name1 nvarchar(254), Name2 nvarchar(254))

似乎还有更多语法错误(为什么你使用 END-IF?)但这应该回答所提出的问题,我无意解决所有这些问题。

DECLARE TABLE @FinalResults (Name254 nvarchar(254), SRNO nvarchar(3))


CREATE TABLE @FinalResults (Name254 nvarchar(254), SRNO nvarchar(3))

Should just be

DECLARE @FinalResults TABLE  (Name254 nvarchar(254), SRNO nvarchar(3))

Additionally the return type of the function should be

RETURNS @FinalResults1 TABLE (Name1 nvarchar(254), Name2 nvarchar(254))

There seem to be still more syntax errors (why are you using END-IF?) but that should answer the question asked and I have no intention of going through fixing them all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文