|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
php5 - CSV import to MSSQL Issues with php script
Hi All,
i have been working on a website which has a products database which imports csv files on a night by deleting all db entries and reimporting the products from the csv file. i want to change this as we are planning on updating products when the system is in use and obviously we cant delete everything from the db so i have been trying to create a script bit by bit that will do something similar to this: PHP Code:
this is my current code but i know that it isnt going to work the way i want it to. as i want it to look row by row and if the row exists in the database to update it, if the row doesnt exist in the database insert it and if the row is no longer in the csv file to delete it from the database. does anyone know how to do this? PHP Code:
|
|
#2
|
||||
|
||||
|
Check out the mysql keywords "update into" this will update a record if it exists (based on an primary key match) or insert it if it does not exist.
__________________
There is no spoon. |
|
#3
|
|||
|
|||
|
Quote:
this is ok but we don't have a unique key in the csv file, all i could do is a where statement e.g. update into ... WHERE manf_part_no = "$manf_part_no" AND supp_id = "$supp_id" but this wouldnt delete a line if it wasnt in the csv file would it? |
|
#4
|
|||
|
|||
|
I don't believe MSsql has "update into" anyways. What I would personally do, which is probably a long way around, but what I think is the easiest is make an empty copy of the main table, even a temp table would work, add the csv file to that and then run something like this (probably in a sproc, but can be done by running each query in php):
Code:
--add new rows from csv file.
INSERT INTO mainTable
SELECT *
FROM copyTable
WHERE
manf_part_no NOT IN (SELECT manf_part_no FROM mainTable)
AND supp_id NOT IN (SELECT supp_id FROM mainTable)
--remove rows not in csv file.
DELETE FROM mainTable
WHERE
manf_part_no NOT IN (SELECT manf_part_no FROM copyTable)
AND supp_id NOT IN (SELECT supp_id FROM copyTable)
--clear everything from the copyTable to prepare for next night.
TRUNCATE TABLE copyTable
|
|
#5
|
|||
|
|||
|
Quote:
Hi, yes you can do an update function in MSSQL, the idea of putting the CSV into SQL first seems like the best way to go so i have created a table called tempproducts which the csv is imported into but then i have the issue of doing the update. using your method with the insert into wont update fields which i require for stock level changes etc. yours will however allow me to insert new and delete old rows which is great. i need a syntax that will allow me to update specific columns e.g. UPDATE products SET products.avail_qty = tempproducts.avail_qty WHERE products.manf_part_no = tempproducts.manf_part_no AND products.supp_id = tempproducts.supp_id the above command doesnt work i get this error: Msg 4104, Level 16, State 1, Line 4 The multi-part identifier "dbo.tempproduct.manf_part_no" could not be bound. Msg 4104, Level 16, State 1, Line 5 The multi-part identifier "dbo.tampproduct.supp_id" could not be bound. |
|
#6
|
|||
|
|||
|
You CAN do UPDATE, but not UPDATE INTO
|
|
#7
|
|||
|
|||
|
I believe something like this should work for updating
Code:
UPDATE p1
SET p1.avail_qty = t1.avail_qty
FROM Products p1
INNER JOIN tempproducts t1 ON
p1.manf_part_no = t1.manf_part_no
AND p.supp_id = t.supp_id
|
|
#8
|
|||
|
|||
|
Quote:
no this doesnt work just tried it it just reimports the same data again |
|
#9
|
|||
|
|||
|
Hi,
ok i have managed to get the script working with the queries but i notice that this query never selects anything then i thought of course it doesnt as this is wrong. Code:
INSERT INTO mainTable
SELECT *
FROM copyTable
WHERE
manf_part_no NOT IN (SELECT manf_part_no FROM mainTable)
AND supp_id NOT IN (SELECT supp_id FROM mainTable)
not sure what it should be but it should do the followig insert data from the temptable that isnt in the maintable. as there are e.g. 4 suppliers each one could have the same manf_part_no so we need to select the supp_id also? does the above query do this or not i changed a line in the temp table and id didnt seem to work as 0 rows were affected Last edited by marksie1988 : October 20th, 2009 at 12:02 PM. |
|
#10
|
|||
|
|||
|
you are right that if the same part id from different manufacturers was present in the main table it wouldn't insert the new row. you could probably do something like this, however I don't know how well it would perform because it has to look up the data for each row.
Code:
INSERT INTO mainTable
SELECT *
FROM copyTable
WHERE
(SELECT manf_part_no FROM mainTable WHERE manf_part_no=copyTable.manf_part_no AND supp_id=copyTable.supp_id) IS NULL
tries to select a column (really doesn't matter which column) from the main table where the manf_part_no and supp_id match the current row in the copy table and if nothing is brought back (is null) then it will add that row. or something like this with an outter join (which probably would be faster if you have indexes setup on the id columns). Code:
INSERT INTO mainTable
SELECT CT.*
FROM copyTable CT
LEFT JOIN mainTable MT ON CT.manf_part_no=MT.manf_part_no AND CT.supp_id=MT.supp_id
WHERE
MT.manf_part_no IS NULL
AND MT.supp_id IS NULL
this joins the main table with an outter join on both the id columns and only inserts the data if the ids are null meaning they don't exist in the main table. |
|
#11
|
||||
|
||||
|
yea, this is a case of my poor attention to detail. I just assumed MySQL.
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > php5 - CSV import to MSSQL Issues with php script |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|