Wednesday, March 28, 2012

Need to Add Data that is checked on a separate table

Hi all, I hope I can make this understandable. I'm new to SQL and I'm
trying to recreate some tables from an older program. I had to create
new keys, since the old data didn't use keys in the same way and I'm
having problems populating the new keys. I'll give you one abridged
example and see if anyone has any ideas.
The main table is Contracts, with a key of ContractNumber. There is
also a ContractCode. The table is related to DO table by
ContractNumber, but that wasn't used on the old system, so I have to
insert it. ContractCode was, but I can't figure out how to fill in
ContractNumber in DO by comparing the ContractCodes from the two
tables. As I said, I'm new to SQL, and here's the last query I tried,
again to no avail. Any help is appreciated.
INSERT DeliveryOrder (DeliveryOrder_ContractNo)
SELECT Contract_Number
FROM Contracts AS a INNER JOIN DeliveryOrder AS b
ON a.ContractCode = b.DeliveryOrderContractNamehi
you just need to update the DO table DeliveryOrder_ContractNO, So can not
use insert command.
look this ,maybe can get some help
---
create table Contracts(ContractNumber int,ContractCode nvarchar(20))
Create table DeliveryOrder(DeliveryOrder_ContractNO
int,DeliveryOrderContractName nvarchar(20))
insert into Contracts select 1,'CountryA'
union all select 2,'CountryB'
union all select 3,'CountryC'
union all select 4,'CountryD'
insert into DeliveryOrder select NULL,'CountryA'
union all select NULL,'CountryB'
union all select NULL,'CountryC'
union all select NULL,'CountryD'
select * from contracts
select * from DeliveryOrder
contracts table
--
ContractNumber ContractCode
1 CountryA
2 CountryB
3 CountryC
4 CountryD
--
DeliveryOrder table
--
DeliveryOrder_ContractNO DeliveryOrderContractName
NULL CountryA
NULL CountryB
NULL CountryC
NULL CountryD
Update DeliveryOrder set DeliveryOrder_ContractNO=
(
select a.ContractNumber from Contracts a
inner join DeliveryOrder b on b.DeliveryOrderContractName = a.ContractCode
where b.DeliveryOrderContractName = DeliveryOrder.DeliveryOrderContractName
)
select * from DeliveryOrder
drop table contracts
drop table DeliveryOrder
---
DeliveryOrder_ContractNO DeliveryOrderContractName
1 CountryA
2 CountryB
3 CountryC
4 CountryD
"DarkGalahad" wrote:

> Hi all, I hope I can make this understandable. I'm new to SQL and I'm
> trying to recreate some tables from an older program. I had to create
> new keys, since the old data didn't use keys in the same way and I'm
> having problems populating the new keys. I'll give you one abridged
> example and see if anyone has any ideas.
> The main table is Contracts, with a key of ContractNumber. There is
> also a ContractCode. The table is related to DO table by
> ContractNumber, but that wasn't used on the old system, so I have to
> insert it. ContractCode was, but I can't figure out how to fill in
> ContractNumber in DO by comparing the ContractCodes from the two
> tables. As I said, I'm new to SQL, and here's the last query I tried,
> again to no avail. Any help is appreciated.
> INSERT DeliveryOrder (DeliveryOrder_ContractNo)
> SELECT Contract_Number
> FROM Contracts AS a INNER JOIN DeliveryOrder AS b
> ON a.ContractCode = b.DeliveryOrderContractName
>

No comments:

Post a Comment