1

请查看这些 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 是输出过程。即我想在过程的最后一行有一些这样的东西:

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

这样 getinfo 是一个像 Code-One 一样工作的函数。我怎样才能做到这一点?

4

1 回答 1

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

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

于 2013-11-05T12:19:23.250 回答