I have a doubt about how to retrieve data from a third table and import it into the first table via a second table. The flow is as follows:
I must fill the column 'descrip' in table 'Resul' (the 1st table) with a field called 'proveedor' taken from table 'Prov_datos' (the 3rd table)
'Resul' contains a column called 'codigo interno' whose field is filled with a field called 'idPedido' imported from a table called 'Pedidos_prov_cabecera'(the 2nd table)
'Pedidos_prov_cabecera' contains a column called 'idProveedor' which links to 'idProveedor' in the table 'Prov_datos'.
Finally, 'Prov_Datos' contains another column called 'proveedor'. This is the value which I must import into the first table 'resul' assigned to the field 'descrip'. I must do this with an SQL UPDATE command.
Any help would be greatly appreciated.
Kind Regards,
P.S Other similar UPDATES I did in the Resul table look something like this for reference:
UPDATE @Resul
SET CodigoInterno = CP.IdPedido
FROM @Resul R INNER JOIN
SERVIDORKARNACA.FSEGURA_ES.dbo.Conf_Pedidos_Prov CP ON REPLACE(R.Valor, 'K#', '') = CAST(CP.KumoCode AS VARCHAR)
WHERE Campo = 'providerOrderId
UPDATE @Resul
SET Descrip = CPP.IdPedidoProv
FROM @Resul R INNER JOIN
SERVIDORKARNACA.FSEGURA_ES.dbo.Pedidos_Prov_Cabecera CPP ON (R.CodigoInterno) = CAST (CPP.IdPedido AS VARCHAR)
WHERE Campo = 'providerOrderId'
Based on the description provided, the SQL UPDATE command to fill the 'descrip' column in the 'Resul' table with the 'proveedor' field from the 'Prov_datos' table can be written as follows:
UPDATE Resul
SET descrip = Prov_datos.proveedor
FROM Resul
INNER JOIN Pedidos_prov_cabecera ON Resul.CodigoInterno = Pedidos_prov_cabecera.idPedido
INNER JOIN Prov_datos ON Pedidos_prov_cabecera.idProveedor = Prov_datos.idProveedor
This query joins the 'Resul' table with the 'Pedidos_prov_cabecera' table on the 'CodigoInterno' and 'idPedido' fields respectively, and then joins the resulting table with the 'Prov_datos' table on the 'idProveedor' field. Finally, it updates the 'descrip' column in the 'Resul' table with the 'proveedor' field from the 'Prov_datos' table.
Note that the table and column names used in the query may need to be adjusted based on the actual names used in the database.