Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 10 of 10

Thread: Need help writing a Query

  1. #1
    vedran is offline Member
    Join Date
    Jun 2012
    Posts
    98

    Default Need help writing a Query

    Hello everyone,

    Just recently I have discovered an error on all of the files that are uploaded.

    We have uploaded product options (kits) as nonpublished products. However, problem in those unpublished products is the fact that COST Price ended up as a sale price, and the cost is now empty.

    I need a query that will check every row and move Sale price to cost price.


    thanks

  2. #2
    BFG 9000 is offline Senior Member
    Join Date
    Oct 2006
    Location
    South UK
    Posts
    882

    Default

    C#/VB.NET Code:
    UPDATE Productvariant SET Cost SalePrice;
    UPDATE ProductVariant SET SalePrice 0.00

    Insert obligatory comment about how you should test this before using on a live server - make half a dozen backups, test the backups & pay someone a fortune to verify the query & guarantee it will work.



    TTFN

    BFG

  3. #3
    vedran is offline Member
    Join Date
    Jun 2012
    Posts
    98

    Default

    Quote Originally Posted by BFG 9000 View Post
    C#/VB.NET Code:
    UPDATE Productvariant SET Cost SalePrice;
    UPDATE ProductVariant SET SalePrice 0.00

    Insert obligatory comment about how you should test this before using on a live server - make half a dozen backups, test the backups & pay someone a fortune to verify the query & guarantee it will work.



    TTFN

    BFG
    Thanks for the query. Now if I dare to ask, how would I add that this should be run only on products that are unpublished?

    Thanks

  4. #4
    vedran is offline Member
    Join Date
    Jun 2012
    Posts
    98

    Default

    This is what I have come up with:

    Code:
    declare @id int
    
    declare cur CURSOR LOCAL for
    select id = ProductID  from Product WHERE (Published = '0')
    
    open cur
    
    fetch next from cur into @id 
    
    while @@FETCH_STATUS = 0 BEGIN
    
    UPDATE Productvariant SET Cost = SalePrice WHERE VariantID = @id;
    UPDATE ProductVariant SET SalePrice = 0.00 WHERE VariantID = @id; 
    
    fetch next from cur into @id 
    END
    
    close cur
    deallocate cur

  5. #5
    BFG 9000 is offline Senior Member
    Join Date
    Oct 2006
    Location
    South UK
    Posts
    882

    Default

    C#/VB.NET Code:
    UPDATE Productvariant SET Cost SalePrice WHERE Published 0;
    UPDATE ProductVariant SET SalePrice 0.00 WHERE Published 0

    TTFN

    BFG

  6. #6
    vedran is offline Member
    Join Date
    Jun 2012
    Posts
    98

    Default

    Quote Originally Posted by BFG 9000 View Post
    C#/VB.NET Code:
    UPDATE Productvariant SET Cost SalePrice WHERE Published 0;
    UPDATE ProductVariant SET SalePrice 0.00 WHERE Published 0

    TTFN

    BFG
    Now I am confused totally... my code is bad?

    What I noticed when I run this code on my test DB instance, it did chagned those values... but DB become slow as hell. I did the same thing three times in a row, and still it is slow as hell

  7. #7
    esedirect is offline Senior Member
    Join Date
    Feb 2010
    Location
    Norfolk, UK
    Posts
    343

    Default

    Your "slow" problem is in using cursors. Absolutely no need in this case. Also, your query is wrong:

    Code:
    declare @id int
    
    declare cur CURSOR LOCAL for
    select id = ProductID  from Product WHERE (Published = '0')
    
    open cur
    
    fetch next from cur into @id 
    
    while @@FETCH_STATUS = 0 BEGIN
    
    UPDATE Productvariant SET Cost = SalePrice WHERE VariantID = @id;
    UPDATE ProductVariant SET SalePrice = 0.00 WHERE VariantID = @id; 
    
    fetch next from cur into @id 
    END
    
    close cur
    deallocate cur
    You are using Product.ProductID in the cursor to update the ProductVariant table based on the VariantID, so all of your records will get mixed up. You will need to revert to your backup before doing it again.

    Code:
    UPDATE pv SET Cost=SalePrice FROM ProductVariant pv JOIN Product p ON p.ProductID=pv.ProductID WHERE p.Published=0
    UPDATE pv SET SalePrice=0 FROM ProductVariant pv JOIN Product p ON p.ProductID=pv.ProductID WHERE p.Published=0
    http://www.esedirect.co.uk
    --------------------------------------------------------------------------
    Using MS 9.2.0.0 with the following customisations:

    Lightbox/Fancybox enlarged images;
    Auto-suggest searchbox;
    Extra product information shown only to our IP Address (such as supplier info, costs, etc.);
    Failed transactions emailed via trigger;
    Custom app to show basket contents when customer online;
    Orders pushed through to accounting systems.

    All the above without source!

  8. #8
    vedran is offline Member
    Join Date
    Jun 2012
    Posts
    98

    Default

    Thank you

    I will test this query ASAP

  9. #9
    SarahGomez is offline Junior Member
    Join Date
    Jan 2013
    Location
    New york
    Posts
    7

    Default

    I lost my recently loaded files. i don't know what is the reason behind this?? please help me so that i get my recently loaded files.

    Visit here.. http://www.prlog.org/12114868-lenswo...o-grab-it.html
    Last edited by SarahGomez; 04-11-2013 at 02:13 AM.

  10. #10
    Skriver is offline Senior Member
    Join Date
    Apr 2012
    Posts
    188

    Default

    What files Sarah?