【问题标题】:multiple errors in using with function in select statement在 select 语句中使用 with 函数时出现多个错误
【发布时间】:2014-07-23 15:01:22
【问题描述】:

我的选择语句有一些问题。当我尝试执行它时,它给出了 3 个错误

必须指定要从中选择的表 SELECT INFO 语句的对象或列名丢失或为空。验证每一列

有名字。对于其他语句,请查找空别名。别名定义...

列名或提供的值的数量与表定义不匹配。

WITH A AS
        (SELECT ROW_NUMBER() OVER (ORDER BY 
                CASE
                    WHEN @pOrderBy = 'SortByName' THEN colPortAgentVendorNameVarchar
                    WHEN @pOrderBy = 'SortByCOuntry' THEN colCountryNameVarchar
                    WHEN @pOrderBy = 'SortByCity' THEN colCityNameVarchar
                END, 
                colPortAgentVendorNameVarchar
            ) xRow, A.*
            FROM
            (
                SELECT DISTINCT 
                    V.colPortAgentVendorIDInt,
                    colPortAgentVendorNameVarchar = RTRIM(LTRIM(V.colPortAgentVendorNameVarchar)),
                    C.colCountryNameVarchar,
                    Y.colCityNameVarchar,
                    V.colContactNoVarchar,
                    V.colFaxNoVarchar,
                    V.colEmailToVarchar,
                    V.colWebsiteVarchar,
                    BR.colBrandIdInt,
                    PR.colPriorityTinyint,
                    colBrandCodeVarchar = RTRIM(LTRIM(BR.colBrandCodeVarchar))

                FROM dbo.TblVendorPortAgent  V
                LEFT JOIN TblCountry C ON C.colCountryIDInt = V.colCountryIDInt
                LEFT JOIN TblCity Y ON Y.colCityIDInt = V.colCityIDInt          
                LEFT JOIN tblBrandAirportPortAgent PR ON PR.colPortAgentVendorIDInt = V.colPortAgentVendorIDInt
                    AND PR.colIsActiveBit = 1
                LEFT JOIN TblBrand BR ON BR.colBrandIdInt = PR.colBrandIdInt
                    AND BR.colIsActiveBit = 1
                WHERE V.colIsActiveBit = 1
                AND V.colPortAgentVendorNameVarchar LIKE '%'+ @pPortAgentVendor +'%'            
                AND (PR.colBrandIdInt = @pBrandID OR @pBrandID = 0)
            ) A

        )
        INSERT INTO #tempPortAgent

        SELECT A.*, IsWithContract = CASE WHEN colContractIdInt IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT)  END FROM A LEFT JOIN TblContractPortAgent B ON A.colPortAgentVendorIDInt = B.colPortAgentVendorIDInt
                AND B.colIsActiveBit = 1
        ;WITH QQ AS
    (
        SELECT ROW_NUMBER() OVER(PARTITION BY Q.colPortAgentVendorIDInt ORDER BY xRow, 
            ContractStatusOrder,
            colDateCreatedDate DESC
            )ContractRow,*
        FROM
        (       
            SELECT DISTINCT GG.*, B.colContractIdInt, B.colContractStatusVarchar, B.colDateCreatedDate , 
                ContractStatusOrder = CASE WHEN B.colContractStatusVarchar = 'Approved' THEN 1 ELSE '2' END
            FROM #tempPortAgent GG LEFT JOIN TblContractPortAgent B ON GG.colPortAgentVendorIDInt = B.colPortAgentVendorIDInt
                AND B.colIsActiveBit = 1
        ) Q
    )SELECT * INTO #tempPortAgentWithContract   

    SELECT * FROM #tempPortAgentWithContract

我真的不知道它在哪里显示,因为错误是说这些在里面的 select 语句中。

【问题讨论】:

    标签: sql sql-server select


    【解决方案1】:

    1- 使用Select Into 而不是Insert into select

    SELECT A.*, IsWithContract = CASE WHEN colContractIdInt IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT)  END 
    Into #tempPortAgent
    FROM A 
    LEFT JOIN TblContractPortAgent B ON A.colPortAgentVendorIDInt = B.colPortAgentVendorIDInt
                    AND B.colIsActiveBit = 1
    

    2- SELECT * INTO #tempPortAgentWithContract 语法不正确。您必须使用以下格式:

    SELECT * INTO #tempPortAgentWithContract From QQ 
    

    【讨论】:

    • 我刚试了一下,它说“数据库中已经有一个名为#tempPortAgent 的对象”,我还需要做些什么来让它工作吗?
    • 因为在您的会话中创建了#tempPortAgent。如果存在,您可以先删除#tempPortAgent,或者可以在新的查询窗口中执行您的查询以获得新的会话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2019-12-20
    • 1970-01-01
    • 2020-09-01
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多