Visual Studio中的存储过程和新查询

时间:2011-10-17 19:01:17

标签: .net sql visual-studio

我有一个查询并尝试在数据库的Visual Studio / New Query对话框中执行它:

SELECT * FROM 
    (SELECT TOP 1 
     PT.NameAn as NameAn, 
     PT.Text as TaleText, 
     PT.RouteNameAn as TaleRouteNameAn,
     '' as SbornikRouteNameAn, 
     PTC.RouteNameAn as CategoryRouteNameAn
     FROM PeopleTales PT
     LEFT JOIN PeopleTalesCategories PTC ON PT.CategoryAn = PTC.Analit
     WHERE LEN(PT.Text) > 900
     ORDER BY NEWID()
    ) t1

UNION

SELECT * FROM 
    (SELECT TOP 1 
     AT.NameAn as NameAn, 
     AT.Text as TaleText, 
     AT.RouteNameAn as TaleRouteNameAn, 
     '' as SbornikRouteNameAn, 
     ATC.RouteNameAn as CategoryRouteNameAn
     FROM AuthorTales AT
     LEFT JOIN AuthorTalesCategories ATC ON AT.CategoryAn = ATC.Analit
     WHERE LEN(AT.Text) > 900
     ORDER BY NEWID()
    ) t2

UNION

SELECT * FROM 
   (SELECT TOP 1 
    PST.NameAn as NameAn,
    PST.Text as TaleText, 
    PST.RouteNameAn as TaleRouteNameAn, 
    PS.RouteNameAn as SbornikRouteNameAn, 
    PSC.RouteNameAn as CategoryRouteNameAn
    FROM PeopleSbornikiTales PST
    LEFT JOIN PeopleSborniki PS ON PS.Analit = PST.SbornikAn
    LEFT JOIN PeopleSbornikiCategories PSC ON PSC.Analit = PS.CategoryAn
    WHERE PST.Analit = (
        SELECT Analit FROM (
            SELECT TOP 1 PS.Analit, COUNT(PST.Analit)
            FROM PeopleSborniki PS 
            LEFT JOIN PeopleSbornikITales PST ON PST.SbornikAn = PS.Analit
            GROUP BY PS.Analit
            HAVING COUNT(PST.Analit) > 0
            ORDER BY NEWID())
        ) 
) t3

当我将其复制/粘贴到Visual Studio 2010中的新查询对话框中时,我的数据库工作正常,但是当我想将其保存到存储过程中时

ALTER PROCEDURE dbo.Index_GetRandomTales
AS
BEGIN
    SELECT * FROM 
        (SELECT TOP 1 
         PT.NameAn as NameAn, 
         PT.Text as TaleText, 
         PT.RouteNameAn as TaleRouteNameAn,
         '' as SbornikRouteNameAn, 
         PTC.RouteNameAn as CategoryRouteNameAn
         FROM PeopleTales PT
         LEFT JOIN PeopleTalesCategories PTC ON PT.CategoryAn = PTC.Analit
         WHERE LEN(PT.Text) > 900
         ORDER BY NEWID()
        ) t1

    UNION

    SELECT * FROM 
        (SELECT TOP 1 
         AT.NameAn as NameAn, 
         AT.Text as TaleText, 
         AT.RouteNameAn as TaleRouteNameAn, 
         '' as SbornikRouteNameAn, 
         ATC.RouteNameAn as CategoryRouteNameAn
         FROM AuthorTales AT
         LEFT JOIN AuthorTalesCategories ATC ON AT.CategoryAn = ATC.Analit
         WHERE LEN(AT.Text) > 900
         ORDER BY NEWID()
        ) t2

    UNION

    SELECT * FROM 
       (SELECT TOP 1 
        PST.NameAn as NameAn,
        PST.Text as TaleText, 
        PST.RouteNameAn as TaleRouteNameAn, 
        PS.RouteNameAn as SbornikRouteNameAn, 
        PSC.RouteNameAn as CategoryRouteNameAn
        FROM PeopleSbornikiTales PST
        LEFT JOIN PeopleSborniki PS ON PS.Analit = PST.SbornikAn
        LEFT JOIN PeopleSbornikiCategories PSC ON PSC.Analit = PS.CategoryAn
        WHERE PST.Analit = (
            SELECT Analit FROM (
                SELECT TOP 1 PS.Analit, COUNT(PST.Analit)
                FROM PeopleSborniki PS 
                LEFT JOIN PeopleSbornikITales PST ON PST.SbornikAn = PS.Analit
                GROUP BY PS.Analit
                HAVING COUNT(PST.Analit) > 0
                ORDER BY NEWID())
            ) 
    ) t3
END
我有一个错误: 在')'附近的Incorect语法。 有什么想法我可以更好地调试它吗? = \

0 个答案:

没有答案