在select语句中使用函数时出现多个错误

时间:2014-07-23 06:57:39

标签: sql sql-server select

我的select语句出了点问题。当我尝试执行它时,它会产生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语句中。

1 个答案:

答案 0 :(得分: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