无效的对象名称 - 本地SQL对象

时间:2017-05-24 19:55:25

标签: sql sql-server

以下存储过程正常工作并返回数据。但是我想在最终选择之前将数据插入到表中以进行调试。插入代码在最终选择之前被注释掉。插入使用完全相同的选择,没有顺序,作为数据源。但是,当我运行插件取消注释时,我得到一个错误:

无效的对象名称BudgetSummaryDetail

我不知道为什么在最终选择中使用对象名称是正常的,但如果我尝试将其用于插入,则会出现错误。我确定有一个原因,但我尝试了一些不同的方法,我似乎无法弄明白。任何帮助或见解都将非常受欢迎。

ALTER PROCEDURE [dbo].[rptBudgetSummaryTest]
@Fy AS CHAR(4),
@Index_No AS VARCHAR(MAX) = NULL,       -- "IN" - NULL for all; otherwise, comma seperated list of values
@PCA AS VARCHAR(MAX) = NULL,            -- "IN" - NULL for all; otherwise, comma seperated list of values
@Appro_Sym AS VARCHAR(MAX) = NULL,      -- "IN" - NULL for all; otherwise, comma seperated list of values
@IncludeNonFundedPositions AS BIT = 1,  
@OnlyNonZeroPYs AS BIT = 0,
@OnlyNonZeroAllotment AS BIT = 0,           
@mrgpcain as dbo.mrgpcain READONLY

AS
IF 1=0 BEGIN
    SET FMTONLY OFF
END
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
-- SET XACT_ABORT ON causes the entire transaction to be
-- rolled back on a run-time error instead of just the sql
-- statement that caused the error.
SET XACT_ABORT ON
BEGIN

-- Tally tables for optional multi-value parameters

SELECT Item     
INTO #Index_No_ParamValues
FROM [Common].dbo.Split(@Index_No, ',')

IF (SELECT COUNT(*) FROM #Index_No_ParamValues WHERE Item IS NOT NULL) < 1
BEGIN
    TRUNCATE TABLE #Index_No_ParamValues
END

SELECT Item
INTO #PCA_ParamValues
FROM [Common].dbo.Split(@PCA, ',')
IF (SELECT COUNT(*) FROM #PCA_ParamValues WHERE Item IS NOT NULL) < 1
BEGIN
    TRUNCATE TABLE #PCA_ParamValues
END

SELECT Item
INTO #Appro_Sym_ParamValues
FROM [Common].dbo.Split(@Appro_Sym, ',')
IF (SELECT COUNT(*) FROM #Appro_Sym_ParamValues WHERE Item IS NOT NULL) < 1
BEGIN
    TRUNCATE TABLE #Appro_Sym_ParamValues
END

;WITH
BudgetSummaryDetail
AS (
    SELECT
        tblPCAIn.FY,
        tblPCAIn.Index_No,
        tblPCAIn.PCA,
        tblPCAIn.Obj_Det,
        tblPCAIn.Agy_Obj,
        tblPCAIn.PYs,
        Calc_PYs = IIF(((tblPCAIn.Obj_Det='427' AND tblPCAIn.Agy_Obj='04') OR tblPCAIn.Obj_Det='439') AND (CAST(tblPCAIn.PCA AS INT) BETWEEN 5000 AND 5999), 
                        IIF(tblPCAFl.Fund_Src = 'F', (tblPCAIn.PYs * tblPCAFl.W_Pnct), 0), 
                        (tblPCAIn.PYs * tblPCAFl.Pnct)),
        Calc_Allot = IIF(((tblPCAIn.Obj_Det='427' AND tblPCAIn.Agy_Obj='04') OR tblPCAIn.Obj_Det='439') AND (CAST(tblPCAIn.PCA AS INT) BETWEEN 5000 AND 5999), 
                        IIF(tblPCAFl.Fund_Src = 'F', (tblPCAIn.Allotment * tblPCAFl.W_Pnct), 0), 
                        (tblPCAIn.Allotment * tblPCAFl.Pnct)),
        tblPCAFl.Appro_Sym,
        tblPCAFl.Fund_Src,
        tblPCAFl.Fund_No,
        tblPCAFl.Method,
        tblPCAFl.Project,
        PCT = ROUND(tblPCAFl.Pnct * 100, 0)
    FROM
        dbo.tblIndex AS tblIndex
        INNER JOIN @mrgpcain AS tblPCAIn ON
            tblPCAIn.FY = tblIndex.FY AND
            tblPCAIn.Index_No = tblIndex.Index_No
        INNER JOIN dbo.tblPCA AS tblPCA ON
            tblPCA.FY = tblPCAIn.FY AND
            tblPCA.PCA = tblPCAIn.PCA
        INNER JOIN dbo.tblPCAFl AS tblPCAFl ON
            tblPCAFl.FY = tblPCA.FY AND
            tblPCAFl.PCA = tblPCA.PCA
        INNER JOIN dbo.tblAppro AS tblAppro ON
            tblAppro.FY = tblPCAFl.FY AND
            tblAppro.Appro_Sym = tblPCAFl.Appro_Sym
        LEFT JOIN #Index_No_ParamValues ON
            tblIndex.Index_No = #Index_No_ParamValues.Item
        LEFT JOIN #PCA_ParamValues ON
            tblPCAIn.PCA = #PCA_ParamValues.Item
        LEFT JOIN #Appro_Sym_ParamValues ON
            tblAppro.Appro_Sym = #Appro_Sym_ParamValues.Item
    WHERE
        (#Index_No_ParamValues.Item IS NOT NULL OR NOT EXISTS (SELECT TOP 1 Item FROM #Index_No_ParamValues)) AND
        (#PCA_ParamValues.Item IS NOT NULL OR NOT EXISTS (SELECT TOP 1 Item FROM #PCA_ParamValues)) AND
        (#Appro_Sym_ParamValues.Item IS NOT NULL OR NOT EXISTS (SELECT TOP 1 Item FROM #Appro_Sym_ParamValues)) AND
        (((@IncludeNonFundedPositions = 0) AND
            (tblPCA.PCA <> '02727')
        ) OR (@IncludeNonFundedPositions = 1)) AND
        (((@OnlyNonZeroPYs = 1) AND
            (tblPCAIn.PYs <> 0.0)
        ) OR (@OnlyNonZeroPYs = 0)) AND
        (((@OnlyNonZeroAllotment = 1) AND
            (tblPCAIn.Allotment <> 0)
        ) OR (@OnlyNonZeroAllotment = 0))
)

--DELETE FROM tblBS_Test
--INSERT INTO tblBS_Test
--SELECT * FROM (
--SELECT 
--  tblIndex.Dept_Id,
--  tblIndex.Dpty_Id,
--  tblIndex.Brch_Id,
--  tblIndex.Div_Id,
--  detail.Index_No,
--  tblIndex.Index_Title,
--  detail.PCA,
--  tblPCA.PCA_Title,
--  PROGLINK = REPLACE(tblPCA.Program+tblPCA.Element+tblPCA.Component+tblPCA.Task, '', '0'),
--  FUNDLINK = tblPCAFl.Appro_Sym+tblPCAFl.Fund_Src+tblPCAFl.Fund_No+tblPCAFl.Method+ IIF(tblPCAFl.Project = SPACE(6), SPACE(8), tblPCAFl.Project+'00'),
--  tblAppro.Ref,
--  detail.Obj_Det,
--  detail.Agy_Obj,
--  PYs,
--  POS = IIF(((detail.Obj_Det = '003') AND (RTRIM(detail.Agy_Obj) = '')), detail.Calc_PYs, 0.0),
--  Calc_PYs = detail.Calc_PYs,
--  detail.Calc_Allot,
--  PCT
--FROM 
--  BudgetSummaryDetail AS detail
--  INNER JOIN dbo.tblIndex AS tblIndex ON
--      tblIndex.FY = detail.FY AND
--      tblIndex.Index_No = detail.Index_No
--  INNER JOIN dbo.tblPCA AS tblPCA ON
--      tblPCA.FY = detail.FY AND
--      tblPCA.PCA = detail.PCA
--  INNER JOIN dbo.tblPCAFl AS tblPCAFl ON
--      tblPCAFl.FY = tblPCA.FY AND
--      tblPCAFl.PCA = tblPCA.PCA AND
--      tblPCAFl.Appro_Sym = detail.Appro_Sym AND
--      tblPCAFl.Fund_Src = detail.Fund_Src AND
--      tblPCAFl.Fund_No = detail.Fund_No AND
--      tblPCAFl.Method = detail.Method AND
--      tblPCAFl.Project = detail.Project
--  INNER JOIN dbo.tblAppro AS tblAppro ON
--      tblAppro.FY = tblPCAFl.FY AND
--      tblAppro.Appro_Sym = tblPCAFl.Appro_Sym
--  INNER JOIN dbo.tblAgyDE AS tblAgyDE ON
--      tblAgyDE.Obj_Det = detail.Obj_Det AND
--      tblAgyDE.Agy_Obj = detail.Agy_Obj
--  INNER JOIN dbo.tblObjDe AS tblObjDe ON
--      tblObjDe.Obj_Det = tblAgyDE.Obj_Det
--  INNER JOIN dbo.tblCat AS tblCat ON
--      tblCat.Category = tblObjDe.Category) as myda

SELECT 
    tblIndex.Dept_Id,
    tblIndex.Dpty_Id,
    tblIndex.Brch_Id,
    tblIndex.Div_Id,
    detail.Index_No,
    tblIndex.Index_Title,
    detail.PCA,
    tblPCA.PCA_Title,
    PROGLINK = REPLACE(tblPCA.Program+tblPCA.Element+tblPCA.Component+tblPCA.Task, '', '0'),
    FUNDLINK = tblPCAFl.Appro_Sym+tblPCAFl.Fund_Src+tblPCAFl.Fund_No+tblPCAFl.Method+ IIF(tblPCAFl.Project = SPACE(6), SPACE(8), tblPCAFl.Project+'00'),
    tblAppro.Ref,
    detail.Obj_Det,
    detail.Agy_Obj,
    PYs,
    POS = IIF(((detail.Obj_Det = '003') AND (RTRIM(detail.Agy_Obj) = '')), detail.Calc_PYs, 0.0),
    Calc_PYs = detail.Calc_PYs,
    detail.Calc_Allot,
    PCT
FROM 
    BudgetSummaryDetail AS detail
    INNER JOIN dbo.tblIndex AS tblIndex ON
        tblIndex.FY = detail.FY AND
        tblIndex.Index_No = detail.Index_No
    INNER JOIN dbo.tblPCA AS tblPCA ON
        tblPCA.FY = detail.FY AND
        tblPCA.PCA = detail.PCA
    INNER JOIN dbo.tblPCAFl AS tblPCAFl ON
        tblPCAFl.FY = tblPCA.FY AND
        tblPCAFl.PCA = tblPCA.PCA AND
        tblPCAFl.Appro_Sym = detail.Appro_Sym AND
        tblPCAFl.Fund_Src = detail.Fund_Src AND
        tblPCAFl.Fund_No = detail.Fund_No AND
        tblPCAFl.Method = detail.Method AND
        tblPCAFl.Project = detail.Project
    INNER JOIN dbo.tblAppro AS tblAppro ON
        tblAppro.FY = tblPCAFl.FY AND
        tblAppro.Appro_Sym = tblPCAFl.Appro_Sym
    INNER JOIN dbo.tblAgyDE AS tblAgyDE ON
        tblAgyDE.Obj_Det = detail.Obj_Det AND
        tblAgyDE.Agy_Obj = detail.Agy_Obj
    INNER JOIN dbo.tblObjDe AS tblObjDe ON
        tblObjDe.Obj_Det = tblAgyDE.Obj_Det
    INNER JOIN dbo.tblCat AS tblCat ON
        tblCat.Category = tblObjDe.Category
ORDER BY 
    tblIndex.Dept_Id,
    tblIndex.Dpty_Id,
    tblIndex.Brch_Id,
    tblIndex.Div_Id,
    detail.Index_No,
    detail.PCA,
    tblPCAFl.Appro_Sym+tblPCAFl.Fund_Src+tblPCAFl.Fund_No+tblPCAFl.Method+ IIF(tblPCAFl.Project = SPACE(6), SPACE(8), tblPCAFl.Project+'00'),
    detail.Obj_Det,
    detail.Agy_Obj

IF Object_id('tempdb..#Index_No_ParamValues') IS NOT NULL 
BEGIN
    DROP TABLE #Index_No_ParamValues;
END

IF Object_id('tempdb..#PCA_ParamValues') IS NOT NULL 
BEGIN
    DROP TABLE #PCA_ParamValues;
END

IF Object_id('tempdb..#Appro_Sym_ParamValues') IS NOT NULL 
BEGIN
    DROP TABLE #Appro_Sym_ParamValues;
END

RETURN 0
END

GO

0 个答案:

没有答案