Соединение insert into и join в одном запросе

Рейтинг: 0Ответов: 1Опубликовано: 11.01.2023

У меня в SQL Server есть базовая таблица с 10 столбцами:

dt/tm/order_number/order_type/product_category/product/manufacturer/cnt/price/selling_price

К ней создаю 4 справочника с id:

  1. order_type_id
  2. product_category_id
  3. product_id
  4. manufacturer_id

Запрос классический:

create table order_type
(
    order_type_id   int identity (1,1),
    order_type      varchar(200) not null
);

create table product_category
(
    product_category_id int identity (1,1),
    product_category    varchar(200)
);

create table product
(
    product_id  int identity (1,1),
    product     varchar(200)
);

create table manufacturer
(
    manufacturer_id int identity (1,1),
    manufacturer    varchar(200)
);

Затем создаю итоговую таблицу:

dt/tm/order_type_id/product_category_id/product_id/manufacturer_id/cnt/price/selling_price

В итоге в новую таблицу нужно вставить соответствующие данные из базовой таблицы и присоединить из справочников id:

INSERT INTO operations_data (dt, tm, order_type_id, cnt, price, selling_price)
SELECT dt, tm, order_type_id, cnt, price, selling_price
FROM invoice_order_operations AS ioo
INNER JOIN order_type AS ot ON ioo.order_type_id = ot.order_type_id

В итоге выдает ошибку:

The multi-part identifier "operations_data.order_type_id" could not be bound

Сломал голову

Ответы

▲ 1

Похоже, Вы связываете не ту таблицу для выборки

--on order_type.order_type_id=operations_data.order_type_id
on order_type.order_type=invoice_order_operations.order_type

Однако, все записи будут каждый раз добавляться в таблицу)