0

我已经在 SQL 2008 中成功创建了一个函数:

ALTER FUNCTION [dbo].Func_raw_data_xref (@ColName AS NVARCHAR(255)) 
RETURNS @VVSrcCDs TABLE ( 
  VV_SRC_CD      NVARCHAR(255) NULL, 
  VV_CD          NVARCHAR(255) NULL, 
  VV_SRC_CD_DESC NVARCHAR(255) NULL, 
  VV_DSC         NVARCHAR(255) NULL ) 
AS 
  BEGIN 
      DECLARE @vv_SRC_CD      NVARCHAR(255), 
              @vv_CD          NVARCHAR(255), 
              @vv_SRC_CD_DESC NVARCHAR(255), 
              @vv_DSC         NVARCHAR(255); 

      SELECT @vv_SRC_CD = A.VV_SRC_CD, 
             @vv_CD = A.VV_CD, 
             @vv_SRC_CD_DESC = A.VV_SRC_CD_DESC, 
             @vv_DSC = A.VV_DSC 
      FROM   DBO.VALUES A 
             JOIN VVLOOKUP B 
               ON A.VV_SRC_CD = b.VV_SRC_CD 
      WHERE  B.[CLIENT COLUMN NAME] = @ColName 

      RETURN; 
  END 

问题是当我调用函数时:

SELECT * 
FROM   DBO.Func_raw_data_xref('_CD'); 

我没有得到任何结果。我得到的是函数中引用的列,但没有数据。如果我将 select 语句从函数中复制出来并使用 @ColName 的有效参数运行它,我会得到结果。

4

1 回答 1

2

您不必将选择的结果分配给局部变量。您需要做的是将该 SELECT 的结果插入到您声明为返回值的表变量中:

代替

  DECLARE 
     @vv_SRC_CD NVARCHAR(255),
     @vv_CD NVARCHAR(255),
     @vv_SRC_CD_DESC NVARCHAR(255),
     @vv_DSC NVARCHAR(255);

    SELECT 
        @vv_SRC_CD = A.vv_SRC_CD,
        @vv_CD = A.vv_CD, 
        @vv_SRC_CD_DESC = A.vv_SRC_CD_DESC,
        @vv_DSC = A.vv_DSC
    FROM DBO.values A
    JOIN VVLookup B
    ON A.vv_SRC_CD = b.vv_SRC_CD
    WHERE B.[Client Column Name] = @ColName

尝试

INSERT INTO @VVSrcCDs
SELECT 
            A.vv_SRC_CD,
            A.vv_CD, 
            A.vv_SRC_CD_DESC,
            A.vv_DSC
        FROM DBO.values A
        JOIN VVLookup B
        ON A.vv_SRC_CD = b.vv_SRC_CD
        WHERE B.[Client Column Name] = @ColName
于 2013-08-26T21:24:45.870 回答