Соединение insert into и join в одном запросе
У меня в SQL Server есть базовая таблица с 10 столбцами:
dt/tm/order_number/order_type/product_category/product/manufacturer/cnt/price/selling_price
К ней создаю 4 справочника с id:
- order_type_id
- product_category_id
- product_id
- 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
Сломал голову
Источник: Stack Overflow на русском