Cannot use a variable after initializing - multi-part identifier could not be bound

  • I'm wondering why i cannot grab a column value and use it down the line in my sql statement/stored procedure

    I have something like this:

    select o.orderid, o.salesproduct , i.saledate from orders as o,ordersinfo as i where o.ordernum = i.archivednum group by o.orderid, o.salesproduct,i.salesdate

    Declare @oid uniqueidentifier

    set @oid = o.orderid

    select v.vendortype from vendorsorders as v where v.orderid = @oid

    whereas @oid is not legit, renders "multipart identifier could not be bound"

    ? 🙁

    thanks for all help

    Zo

  • set @oid = o.orderid

    What is "o.orderid"?

    Did you forget about FROM part?

    _____________
    Code for TallyGenerator

  • Hypothetically the orderid would also be copied from orders table to vendororders table sometime after the process of the order history

    HTH, thanks

  • Firstly, there probably is a logic error with the code you included. The first SELECT statement would return records for many orders. The scalar variable you are using (@oid) can store a single value only (i.e. it would only store one ordered but the first SELECT statement could return thousands of different orders. Which order do you want to use to lookup the vendor type ?

    Perhaps a single query such as the one below might give you what you are after using a single query

    select v.vendortype, o.orderid, o.salesproduct , i.saledate

    from orders as o

    INNER JOIN ordersinfo as i

    ON o.ordernum = i.archivednum

    INNER JOIN vendorsorders as v

    ON v.orderid = o.orderid

    group by v.vendortype, o.orderid, o.salesproduct,i.salesdate

  • happycat59 - Wednesday, January 11, 2017 8:15 PM

    Firstly, there probably is a logic error with the code you included. The first SELECT statement would return records for many orders. The scalar variable you are using (@oid) can store a single value only (i.e. it would only store one ordered but the first SELECT statement could return thousands of different orders. Which order do you want to use to lookup the vendor type ?Perhaps a single query such as the one below might give you what you are after using a single queryselect v.vendortype, o.orderid, o.salesproduct , i.saledate from orders as oINNER JOIN ordersinfo as i ON o.ordernum = i.archivednum INNER JOIN vendorsorders as vON v.orderid = o.orderidgroup by v.vendortype, o.orderid, o.salesproduct,i.salesdate

    Thanks for assist, yes the requirement changed somewhat and are going to parse the information at the application level, but I needed to see if I can retain each row and then extend more data correctly correlating to the given row, but the logic needed more filtering out of the scope of what I perceived could happen.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply