MS SQL 2008 Studio创建语法错误

时间:2013-10-09 05:33:00

标签: sql sql-server-2008

我有一个动态创建查询字符串的项目。它不是太复杂,只是复杂。我特意在向混音中添加一个选项时遇到问题。保存视图时,Studio会添加“as”。

SELECT     TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState, 
                      (CASE WHEN (dbo.Users_Info.Address1 = '' OR
                      dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
                          (SELECT     UserID
                            FROM          Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
                          (SELECT     individual_number
                            FROM          qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
                          (SELECT     individual_number
                            FROM          qUsersPlaced_ES
                            WHERE      isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
                          (SELECT     individual_number
                            FROM          qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
                          (SELECT     individual_number
                            FROM          qUsersPlaced_CS
                            WHERE      isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
                          (SELECT     individual_number
                            FROM          QEliteAdCandidatesPanel
                            WHERE      isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
                          (SELECT     individual_number
                            FROM          QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
                          (SELECT     ID
                            FROM          qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
                          (SELECT     ID
                            FROM          qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
                          (SELECT     ID
                            FROM          qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per, 
                      dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
                      dbo.Users_Info.Custom_JobAd = 1 OR
                      dbo.Users_Info.Elite_JobAd = 1 OR
                      dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                          (SELECT     company_id
                            FROM          Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
                          (SELECT     MAX(date_purchased) AS EJA_Date_Purchased
                            FROM          dbo.QEliteContacts
                            WHERE      (contact_id = dbo.Users.ID)) AS Date_Purchased_EJA, 
                          dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
                          dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,

视图QEliteContacts如下:

SELECT     bd.date_purchased, bd.ID, csc.contact_id
FROM         dbo.Board_Details AS bd LEFT OUTER JOIN
                      dbo.Client_Services_Contacts AS csc ON csc.service_id = bd.ID
WHERE     (bd.ad_type LIKE '%Elite%')

在第一个代码块的底部附近是语句“(SELECT MAX(date_purchased)AS EJA_Date_Purchased”。这是我添加到视图中的新代码。我得到并错误“无效语法附近'为'”如果我从max(date_purchased)工作室中删除别名,则在保存视图时会添加'as expr1'。如果我从select中删除别名,则在保存视图时,studio将添加'as expr1'。

说实话,我不确定我是否正在解决正确获取最大日期的问题。

1 个答案:

答案 0 :(得分:1)

删除第二个'as'.ie,删除'AS Date_Purchased_EJA'部分,然后给它代替'AS EJA_Date_Purchased'。实际上应该只有第一个'AS'语句。然后在Query Designer窗口中执行查询而不是在View Designer Window中。代码如下所示。

SELECT     TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState, 
                          (CASE WHEN (dbo.Users_Info.Address1 = '' OR
                          dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
                              (SELECT     UserID
                                FROM          Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
                              (SELECT     individual_number
                                FROM          qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
                              (SELECT     individual_number
                                FROM          qUsersPlaced_ES
                                WHERE      isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
                              (SELECT     individual_number
                                FROM          qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
                              (SELECT     individual_number
                                FROM          qUsersPlaced_CS
                                WHERE      isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
                              (SELECT     individual_number
                                FROM          QEliteAdCandidatesPanel
                                WHERE      isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
                              (SELECT     individual_number
                                FROM          QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
                              (SELECT     ID
                                FROM          qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
                              (SELECT     ID
                                FROM          qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
                              (SELECT     ID
                                FROM          qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per, 
                          dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
                          dbo.Users_Info.Custom_JobAd = 1 OR
                          dbo.Users_Info.Elite_JobAd = 1 OR
                          dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
                              (SELECT     company_id
                                FROM          Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
                              (SELECT MAX([EJA_Date_Purchased]) from
                               (SELECT date_purchased AS [EJA_Date_Purchased]
                                FROM          dbo.QEliteContacts
                                WHERE      (contact_id = dbo.Users.ID))t), 
                              dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
                              dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,