• UPDATE to one table based on select from another

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » UPDATE to one table based on select from another

    Author
    Topic
    #464633

    I am trying to update rows in one table (in SQL Server) based on selected rows in another table. not sure how to do this, and this SQL executes but updates all rows:

    Code:
    UPDATE cr_SampleDataSet_HeaderInfo SET LoginID = '', NetworkLogin= '', ReturnedFromField='Yes' SELECT * FROM qryOrigRUGItems;

    doing WHERE (Select * from qryOrigRUGItems); spawns an error.

    so, is this a syntax issue or am i pressing my luck?

    Viewing 8 reply threads
    Author
    Replies
    • #1190110

      WHERE (Select * from qryOrigRUGItems) makes no sense. What exactly do you want to do?

    • #1190111

      that should be obvious! i am trying to expand on ANSII SQL!

      Well, what i am trying to do is run an UPDATE query that only writes to rows in one table that are identified in a separate query. i tried WHERE EXISTS and that got me a new error.

      • #1190114

        The WHERE clause has to specify some kind of condition on field(s) in the table you’re trying to update. Please try to provide specific information.

        • #1190118

          The WHERE clause has to specify some kind of condition on field(s) in the table you’re trying to update. Please try to provide specific information.

          I am trying to update rows with specific info in Table A only if the rows in question are also in Query B. Several rows in Table A should *not* be updated.

          Here’s an example from some website:

          UPDATE suppliers
          SET supplier_name = ( SELECT customers.name
          FROM customers
          WHERE customers.customer_id = suppliers.supplier_id)
          WHERE EXISTS
          ( SELECT customers.name
          FROM customers
          WHERE customers.customer_id = suppliers.supplier_id);

          on looking at it, it seems I need to declare the values in the update inside the select on the Query – I’ll try that next.

          • #1190119

            “if the rows in question are also in Query B” is very vague. We need specific information – in what sense are the rows also in Query B?

      • #1190116

        that should be obvious! i am trying to expand on ANSII SQL!

        Well, what i am trying to do is run an UPDATE query that only writes to rows in one table that are identified in a separate query. i tried WHERE EXISTS and that got me a new error.

        NOT OBVIOUS TO ME! I have no idea what qryOrigRUGItems is for a start.

        [/size]

        BUT anyway maybe something like[/size]

        [/size]

        WHERE (COUNT(*) from qryOrigRUGItems) 0[/size]

        [/size]

        Despite the fact the SQL and Access are written by the same company, they are as Heinz and Tesco’s Value Brand[/size]

        [/size]


        [/size]

    • #1190113

      Also, are you running this query from Access or in SQL Server? The syntax is different.

    • #1190115

      it’s dynamic SQL from Access to SQL Server using ADODB. I can get a straight UPDATE to work but it hits all rows, and i don’t want to do that.

    • #1190120

      I’m sorry if this isn’t clear. Table A has all the records, Query B is some subset of Table A. I want to update Table A for only those records that are in Query B with some predetermined values to certain columns in Table A.

      • #1190121

        If you can’t or won’t provide details I can’t help you.

        • #1190132

          If you can’t or won’t provide details I can’t help you.

          I don’t know how I can state it much plainer. basically, instead of

          INSERT INTO dbo_cr_OrigRUGItems SELECT * FROM qryOrigRUGItems

          I want to UPDATE specific rows.

          However, on thinking about it, I think I need to link the SQL table into Access first.

          • #1190134

            I’ll leave you in the able hands of Francois Caron…

    • #1190124

      Would’nt it be something like this :

      UPDATE cr_SampleDataSet_HeaderInfo SET LoginID = ”, NetworkLogin= ”, ReturnedFromField=’Yes’ WHERE someIDfield IN (Select someIDfield from qryOrigRUGItems)

    • #1190128

      that’s what I just tried:

      Code:
      UPDATE dbo.cr_SampleDataSet_HeaderInfo SET LoginID ='', NetworkLogin='', ReturnedFromField='Yes' WHERE RecordID IN (SELECT RecordID FROM qryEditRUGItems);
      

      I added the column RecordID and made sure the query has the same column (and data) – on run I get

      Invalid object name ‘qryEditRUGitems’.

      Now, the query definately exists, so not sure what the issue is. Perhaps ADODB is confused and things the query should be in SQL Server… Hmmm.

    • #1190131

      Are you sure about the query names ?
      In your first post you mention qryOrigRUGItems and now qryEditRUGitems

      • #1190133

        Are you sure about the query names ?
        In your first post you mention qryOrigRUGItems and now qryEditRUGitems

        yes I’m sure. the correct query is the edit one. What i think i need to do is, dynamically link the table in question into Access rather than do something fancy like run UPDATE directly out of Access into SQL Server.

    • #1190145

      Problem mostly solved. I dynamically link the SQL Server table in and run UPDATE with the Access query providing the set of records to update. Much like Francois’s code. The issue was trying to combine data from SQL Server and Access in one command.

    Viewing 8 reply threads
    Reply To: UPDATE to one table based on select from another

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: