如何将存储过程转换为视图?

时间:2017-05-13 07:46:01

标签: sql-server stored-procedures

我有这个存储过程从4个表中提取数据并按4列排序,但我需要一个具有相同结果的视图(当然没有参数)。这可能吗?我知道我不能在视图中使用临时表,任何解决方案?!

ALTER PROC pr_GetChildrenByPersonID
    @pePersonIDs varchar(8000)
AS
    SELECT 
        *,
        RANK() OVER (PARTITION BY pbsPersonID ORDER BY psbPersonSiblingID ASC) AS CustomRank
    INTO 
        #TempPersonSibling
    FROM 
        PersonSibling
    WHERE 
        pbsSiblingTypeID = 2
        AND pbsPersonID IN (SELECT item 
                            FROM dbo.fn_ParseStrListIdentity(@pePersonIDs, ','))

    SELECT
        pePersonID,
        ISNULL((SELECT pbsName 
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 1), '') AS ChildrenName1,
        ISNULL((SELECT pbsAge  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 1), '') AS ChildrenAge1,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPlaceOfBirth)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 1), '') AS ChildrenCountryOfBirth1,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPresentStay)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 1), '') AS ChildrenCountryOfResidence1,
        ISNULL((SELECT pbsName 
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 2), '') AS ChildrenName2,
        ISNULL((SELECT pbsAge  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 2), '') AS ChildrenAge2,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPlaceOfBirth)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 2), '') AS ChildrenCountryOfBirth2,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPresentStay)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 2), '') AS ChildrenCountryOfResidence2,
        ISNULL((SELECT pbsName 
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 3), '') AS ChildrenName3,
        ISNULL((SELECT pbsAge  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 3), '') AS ChildrenAge3,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPlaceOfBirth)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 3), '') AS ChildrenCountryOfBirth3,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPresentStay)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 3), '') AS ChildrenCountryOfResidence3,
        ISNULL((SELECT pbsName 
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 4), '') AS ChildrenName4,
        ISNULL((SELECT pbsAge  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 4), '') AS ChildrenAge4,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPlaceOfBirth)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 4), '') AS ChildrenCountryOfBirth4,
        ISNULL((SELECT dbo.fn_GetCountry(pbsPresentStay)  
                FROM #TempPersonSibling   
                WHERE pbsSiblingTypeID = 2  
                  AND pbsPersonID = pepersonID 
                  AND CustomRank = 4), '') AS ChildrenCountryOfResidence4
    FROM 
        Person
    WHERE 
        pePersonID IN (SELECT item 
                       FROM dbo.fn_ParseStrListIdentity(@pePersonIDs, ','))

1 个答案:

答案 0 :(得分:3)

使用Common Table Expression (CTE)而不是临时表,并使用现在使用临时表的CTE。

像这样:

WITH 
    TempPersonSibling AS (
        -- the select statement that creates the temp table here
        -- without the INTO clause
    )
SELECT
    pePersonID,
    ISNULL((SELECT pbsName FROM TempPersonSibling WHERE pbsSiblingTypeID = 2  AND pbsPersonID = pepersonID AND CustomRank = 1),'') AS ChildrenName1,
    -- ..., rest of the query is similar
FROM
    Person
WHERE
    -- ...

这样您就可以在视图中创建单个查询和包。