我需要将数据拉入临时表,以便我可以将其分组为产品类型,但我们目前有某种产品的不同变体,我需要将所有变体作为原始变体插入。我编写的代码如下,但它仍然没有工作
IF EXISTS (SELECT *
FROM sys.tables
WHERE name LIKE '#temp%')
DROP TABLE #temp
CREATE TABLE #temp
(
Product VARCHAR(max) NULL,
failure_code VARCHAR(max) NULL
)
IF Product IN ( 18, 19, 20, 23,
24, 25 )
SELECT Product = 'DSD 4136'
INSERT INTO #temp
(Product,
failure_code)
SELECT product.prod_desc AS Product,
test_failure_code.failure_code
FROM repair
INNER JOIN test_failure_code
ON repair.id_test_failure_code = test_failure_code.id_test_failure_code
INNER JOIN production_event_details
ON repair.id_production_event_details = production_event_details.id_production_event_details
INNER JOIN product
ON production_event_details.prod_id = product.prod_id
INNER JOIN repair_scm_bom_item
ON repair.id_repair = repair_scm_bom_item.id_repair
INNER JOIN repair_fault
ON repair_scm_bom_item.id_repair_fault = repair_fault.id_repair_fault
INNER JOIN scm_bom_item
ON repair_scm_bom_item.id_scm_bom_item = scm_bom_item.id_scm_bom_item
GROUP BY product.prod_desc,
test_failure_code.failure_code
SELECT *
FROM #temp
DROP TABLE #temp
答案 0 :(得分:0)
声明
IF Product IN ( 18, 19, 20, 23, 24, 25 )
SELECT Product = 'DSD 4136'
实际上什么都不做,您需要使用CASE语句在insert语句中使用它,当产品是您列出的选项之一时,这将选择产品DSD 4136:
INSERT INTO #temp
(Product,
failure_code)
SELECT CASE WHEN product.prod_desc IN ( 18, 19, 20, 23, 24, 25 ) THEN 'DSD 4136'
ELSE product.prod_desc
END
AS Product,
test_failure_code.failure_code
FROM repair
INNER JOIN test_failure_code
ON repair.id_test_failure_code = test_failure_code.id_test_failure_code
INNER JOIN production_event_details
ON repair.id_production_event_details = production_event_details.id_production_event_details
INNER JOIN product
ON production_event_details.prod_id = product.prod_id
INNER JOIN repair_scm_bom_item
ON repair.id_repair = repair_scm_bom_item.id_repair
INNER JOIN repair_fault
ON repair_scm_bom_item.id_repair_fault = repair_fault.id_repair_fault
INNER JOIN scm_bom_item
ON repair_scm_bom_item.id_scm_bom_item = scm_bom_item.id_scm_bom_item
GROUP BY product.prod_desc,
test_failure_code.failure_code