函数与表返回T-SQL

时间:2013-08-04 06:06:14

标签: sql-server tsql

请参阅这些t-sql代码:

    ALTER PROC [dbo].[SearchAllTables]
(
    @SearchStr nvarchar(100)
)
AS
BEGIN

    CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630),DocNo nvarchar(3630))
       SET NOCOUNT ON

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

    WHILE @TableName IS NOT NULL
    BEGIN
        SET @ColumnName = ''
        SET @TableName = 
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
        BEGIN

            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
                    AND    @TableName IN ('[dbo].[Header]','[dbo].[Padid]','[dbo].[Publisher]','[dbo].[rade]',
                                          '[dbo].[Subjects]','[dbo].[Title]','[dbo].[Description1]')
            )

            IF @ColumnName IS NOT NULL
            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                   ,'+@TableName + '.DocNo'  +' FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE CONTAINS( ' + @ColumnName + ' , ' + @SearchStr2+')'
                )

            END
        END    
    END

    SELECT Document.DocNo FROM Document INNER JOIN #Results ON #Results.DocNo=Document.DocNo COLLATE DATABASE_DEFAULT

END

这个存储过程显示docno。 看到这个(我把它命名为Code-One):

DECLARE @DocNo nvarchar(10)
DECLARE @RadeType nvarchar(20)
    SELECT @RadeType = DefultSetting.Def_Rade FROM DefultSetting;

    SELECT Document.DocNo ,Document.DocType,Title.Title,Header.WriterName + ' '+
           Header.WriterName AS 'Padid' ,Publisher.PublisherName,Publisher.PublishedDate
           ,Rade.MainRange + ',' + Rade.Num +','+Rade.KaterNO +','+Rade.Date1 AS 'Rade'
    FROM Document LEFT OUTER JOIN Title ON Document.DocNo = Title.DocNO
    LEFT OUTER JOIN Header ON Document.DocNo = Header.DocNo
    LEFT OUTER JOIN rade   ON Document.DocNo = rade.DocNO
    LEFT OUTER JOIN Publisher ON Document.DocNo = Publisher.DocNo WHERE Rade.Type = @RadeType 
    AND Document.DocNo=@DocNo

其中@DocNo是输出proc。我想在proc的结束处有一些这样的东西:

SELECT GetInfo(Document.DocNo) FROM Document INNER JOIN #Results ON #Results.DocNo=Document.DocNo COLLATE DATABASE_DEFAUL

这样getinfo是一个像Code-One一样的函数。 我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用表值函数返回一个类似于您想要的表。 类似的东西:

create function [dbo].[GetInfo] (@DocNo nvarchar(10), @RadeType nvarchar(20))
returns @DocInfo table (
      DocNo     varchar(50)
    , DocType   varchar(50)
    , Title     varchar(50)
    , PadId     varchar(50)
    , PublisherName varchar(50)
    , PublishedDate datetime
    , Rade      varchar(100)
    )

as
begin
    insert into @DocInfo
    SELECT Document.DocNo ,Document.DocType,Title.Title,Header.WriterName + ' '+
               Header.WriterName AS 'Padid' ,Publisher.PublisherName,Publisher.PublishedDate
               ,Rade.MainRange + ',' + Rade.Num +','+Rade.KaterNO +','+Rade.Date1 AS 'Rade'
    FROM Document LEFT OUTER JOIN Title ON Document.DocNo = Title.DocNO
    LEFT OUTER JOIN Header ON Document.DocNo = Header.DocNo
    LEFT OUTER JOIN rade   ON Document.DocNo = rade.DocNO
    LEFT OUTER JOIN Publisher ON Document.DocNo = Publisher.DocNo WHERE Rade.Type = @RadeType 
    AND Document.DocNo=@DocNo
    return
end

当然,您必须修复数据类型。