简体   繁体   中英

SQL Stored Procedure - Invalid Column Name

I'm building a stored procedure within SSMS and I've run into a brick wall.

USE [VINTCMPA]

go

/****** Object:  StoredProcedure [dbo].[OpenOrderReport]    Script Date: 12/05/2021 12:20:35 ******/
SET ansi_nulls ON

go

SET quoted_identifier ON

go

-- =============================================
-- Author:    Bancroft Wines
-- Create date: 12/05/2021
-- Description:  
-- =============================================
ALTER PROCEDURE [dbo].[Openorderreport]
  -- Add the parameters for the stored procedure here
  @startdate DATETIME = NULL,
  @enddate   DATETIME = NULL
AS
  BEGIN
      SET @startdate = CONVERT(DATETIME, '2021-04-30 23:59:59', 102)
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET nocount ON;

      -- Insert statements for procedure here
      SELECT Customers.cust_acc                       AS CUSTCODE,
             [Sales Order Details].vop_ordn           AS ORDERNO,
             'Invoiced SOP'                           AS TYPE,
             [Sales Order Header].ordidate            AS DATE,
             Round([Sales Order Details].vol_qtty /
                   [Sales Order Details].volcsize
                   *
                         [Sales Order Details].volcprcn /
                   [Sales Order Header].ordexch1, 2)
                                                      AS VALUE,
             Round(Round([Sales Order Details].vol_qtty /
                         [Sales Order Details].volcsize *
                                     [Sales Order Details].volcprcn /
                   [Sales Order Header].ordexch1, 2) -
                   [Sales Order Details].casecost *
                   [Sales Order Details].vol_qtty
                   /
                   [Sales Order Details].volcsize, 2) AS MARGIN
      FROM   vopoline AS [Sales Order Details]
             INNER JOIN voporder AS [Sales Order Header]
                     ON [Sales Order Details].vop_ordn =
                        [Sales Order Header].vop_ordn
             INNER JOIN vslname1 AS Customers
                     ON [Sales Order Header].cust_acc = Customers.cust_acc
                        AND [Sales Order Header].cust_acc = Customers.custaccs
             INNER JOIN vstmain1 AS Products
                     ON [Sales Order Details].prd_code = Products.prd_code
      WHERE  ( [Sales Order Header].ordidate >=
               CONVERT(DATETIME, '2021-04-30 23:59:59', 102)
             )
             AND ( [Sales Order Header].ordidate <=
                   CONVERT(DATETIME, '2021-05-12 23:59:59',
                   102)
                 )
             AND ( [Sales Order Header].ordacode <> 'S' )
             AND ( [Sales Order Header].ord_stat = 5 )
             AND ( Products.grp_code >= '001' )
             AND ( Customers.custanl1 = 'NO' )
              OR ( [Sales Order Header].ordidate >=
                   @startdate
                 )
                 AND ( [Sales Order Header].ordidate <=
                       @enddate
                     )
                 AND ( [Sales Order Header].ordacode = 'S' )
                 AND ( [Sales Order Header].ord_stat = 5 )
                 AND ( Products.grp_code >= '001' )
                 AND ( Customers.custanl1 = 'NO' )
                 AND ( [Sales Order Header].credstyl = 1 )
      ORDER  BY custcode,
                orderno
  END

go 

Which throws the error:

Invalid column name 'VALUE'.

I've defined VALUE, so I'm not sure why it's not detecting it.

EDIT:

I've reformatted the code, and it now does not show the invalid column error, however it doesn't seem to be inputting anything to the table.

The select cannot refer to other calculated columns, this happens at a different stage in the query process and is unavailable. you need to either repeat the criteria or use the [value] in an outer select that wraps the first one.

You would find it easier to read if you separated your selected columns onto separate lines or at least in logical groups, and removed the unecessary parantheses.

Try the following in your select

Round(Round((([Vol_Qtty]/[VolCsize])*[VolCprcN])/[OrdExch1],2)-([CaseCost]*[Vol_Qtty]/[VolCsize]),2) AS MARGIN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM