Update SQL tables (1 Viewer)

sdbsadmin

Registered User.
Local time
Today, 07:08
Joined
Jun 6, 2013
Messages
14
I have some update queries that are running in Access, but they're consuming an incredible amount of CPU. I'd love to run them directly on the SQL Server, but don't know how. Is anyone able to point me in the right direction? I don't know if I need to set something up with transact SQL, a stored procedure, a function, or something else. The queries are pretty straightforward.

Thanks in advance for your help!
 

mdlueck

Sr. Application Developer
Local time
Today, 07:08
Joined
Jun 23, 2011
Messages
2,631
Yes, if you have a SQL back-end available, making use of it will make a performance improvement.

You will need to dig into the wonderful world of Pass-Through queries in Access. You do not necessarily need to code the queries as Stored Procedures (SP's). For my applications I prefer to use SP's. When query strings get very long, you will run into string handling issues sending the entire SQL to the SQL BE DB to have it execute it. If you use a SP instead, all that must go up is the SP name and parameter list... the rest is already on the server inside the SP.

I will share a few favorite posts links:

Client/Server Architecture
http://www.access-programmers.co.uk/forums/showpost.php?p=1110794&postcount=5

Example Stored Procedure / Naming Standard
http://www.access-programmers.co.uk/forums/showthread.php?t=237387

ADO Constants for use with Late Binding ActiveX Data Objects 2.8 Library
http://www.access-programmers.co.uk/forums/showthread.php?t=243088

ADO.Connection object to SQL BE DB
http://www.access-programmers.co.uk/forums/showthread.php?t=231923#post1184259

Way to detect when ADODB.Connection has lost its connection?
http://www.access-programmers.co.uk/forums/showthread.php?t=235630

Using VBA ADO objects to execute a Stored Procedure (INSERT)
http://www.access-programmers.co.uk/forums/showthread.php?t=216860#post1104120

Options to execute Stored Procedures - DAO / ADO - Including an example of SQL SELECT Stored Procedure using ADODB.Command and ADODB.Parameters objects
http://www.access-programmers.co.uk/forums/showthread.php?p=1160494&posted=1#post1160494

I have resolved the trouble storing dates to SQL Server via ADO objects and Stored Procedures:
http://www.access-programmers.co.uk/forums/showthread.php?p=1104450#post1104450

Example of SQL INSERT / UPDATE using ADODB.Command and ADODB.Parameters objects to Access tables
http://www.access-programmers.co.uk/forums/showthread.php?t=219149

Example of SQL SELECT using ADODB.Command and ADODB.Parameters objects to Access tables
http://www.access-programmers.co.uk/forums/showthread.php?t=230610#post1176746

Example of SQL SELECT using ADODB.Recordset object to Access FE temp table to scan the FE temp table and perform operations
http://www.access-programmers.co.uk/forums/showthread.php?p=1214730 #post1214730

Retrieve Autonumber ID after adoCMD INSERT via adoRS query to Access tables
http://www.access-programmers.co.uk/forums/showthread.php?t=225703

ADO way to capture IDENTITY value with OUTPUT in an INSERT SQL
http://www.access-programmers.co.uk/forums/showthread.php?t=214872#post1094341

Example of DAO.QueryDef objects downloading records from a SQL BE DB via Pass-Through query and populating a FE temp table with them
http://www.access-programmers.co.uk/forums/showthread.php?p=1119605#post1119605

Ability to store NULL via VBA ADO Stored Procedure
http://www.access-programmers.co.uk/forums/showthread.php?t=219562&page=2#post1120572

Linked Table Saved Password ADOX code
http://www.access-programmers.co.uk/forums/showthread.php?t=226468#post1155700

Replace pass-through DAO.QueryDef with DAO execution of Stored Procedure Q's - NoCount and SQL EXEC method
http://www.access-programmers.co.uk/forums/showthread.php?t=223414#post1140657


Now, my preference is to use ADO.Command / ADO.Parameter objects where ever possible. Using ADO objects, you end up with the query results in VBA variables, no Access tables. ADO.Parameter objects encapsulate data being passed to the query so that you do not need to pass all data into the SQL BE DB bare in the SQL. Using ADO.Parameter objects is one big step against SQL injection attacks.... separation of SQL from the data it is dealing with.

To get data downloaded directly into Access tables, see the "nested DAO.QueryDef" post above.

Also it is necessary to use ADO.RecordSet objects for some things... that is my second choice when using ADO.Command / ADO.Parameter objects is not possible.

And you will have to deal with ODBC connection strings in some way shape or form. That depends on your deployment configuration environment. Perhaps you have a shared ODBC ID. Perhaps you have ODBC access tied to the workstation user ID. This is a topic all on its own.
 

spikepl

Eledittingent Beliped
Local time
Today, 13:08
Joined
Nov 3, 2010
Messages
6,142
"they're consuming an incredible amount of CPU " And why, specifically, is this a problem?

Also, have the queries been checked for optimization, and the tables are indexed properly?
 

boblarson

Smeghead
Local time
Today, 04:08
Joined
Jan 12, 2001
Messages
32,059
And are you using functions within the queries?
 

sdbsadmin

Registered User.
Local time
Today, 07:08
Joined
Jun 6, 2013
Messages
14
Thanks for your responses - they're much appreciated!

Spikepl - CPU usage matters as we're running a companywide, heavily used SQL server/Access based program, and we don't want to slow everyone else's speed when they're connecting to the server. The queries I have running are repetitive - I have them set to repeat every minute or so to update orders as billable, process orders that came in, send results to clients etc. So that's why I need them to run often, and why CPU usage matters.

Fields are indexed properly. I have a tbl_orders joining on a tbl_tests, and if any record in tbl_test is not filled in and the order was previously marked as completed, it marks it as incomplete. Another query does the opposite - marks any orders that don't have incomplete tests as complete.

boblarson - The only functions I may be using are dlookups/dmax/dmin, but that's only in one of the queries.

mdlueck - Thanks for all the links, but I'm still not sure how I'd run an efficient update query on the SQL tables.
 

mdlueck

Sr. Application Developer
Local time
Today, 07:08
Joined
Jun 23, 2011
Messages
2,631
but I'm still not sure how I'd run an efficient update query on the SQL tables.

If it is an UPDATE query, then definitely use the ADO.Command / ADO.Parameter style query VBA code. I have a concise example of such - though specifically postured towards Access tables - in the links above.

To that, you would need to supplement with an appropriate ADO.Connection object able to connect to the SQL BE DB... then swap out the LOC that specifies which connection to connect the new ADO.Command object to.
 

sdbsadmin

Registered User.
Local time
Today, 07:08
Joined
Jun 6, 2013
Messages
14
I'm unfamiliar with how to use ADO objects to run a query - I'm used to just using the query designer. Which link was it to use an ADO object? And what exactly is an ADO object?
 

mdlueck

Sr. Application Developer
Local time
Today, 07:08
Joined
Jun 23, 2011
Messages
2,631
Which link was it to use an ADO object?

Here it is again...

Example of SQL INSERT / UPDATE using ADODB.Command and ADODB.Parameters objects to Access tables
http://www.access-programmers.co.uk/forums/showthread.php?t=219149

And what exactly is an ADO object?

ADO is one of many standards MS has developed over the years. ActiveX Data Object I believe is what it stands for. This is able to be developed in VBA code.

I'm unfamiliar with how to use ADO objects to run a query - I'm used to just using the query designer.

I am recommending you move out of your norm of building queries with the query designer UI and into building queries in raw SQL. I would recommend obtaining what ever UI comes with the SQL software. Example: If SQL Server then SQL Server Management Studio (SSMS) in that case. Use that to assist you with building the query. You will not be able to execute the query verbatum. You would need to develop a "driver script" which would define the parameter variables, populate the values to the parameters, then finally execute the query. You can do all of that in SQL SSMS. When you actually run the query from VBA, then VBA itself creates and supplies the parameters, and just the SQL statement you test drove in SSMS gets copied into the VBA code.

Let me see if I can find an example of a DAO style pass-through query. In DAO mode, there the pass-through query looks more similar to a SQL driver script you would be developing in SSMS...

"Replace pass-through DAO.QueryDef with DAO execution of Stored Procedure Q's"
http://www.access-programmers.co.uk/...14#post1140657

That is a concise SQL script declaring / setting a parameter. You would then go onto your UPDATE and use the parameter within the UPDATE query.

Once you have that much working in SSMS, then like I said, copy/paste the SQL into ADO VBA code, and also in ADO VBA code have it send the query the correct parameter list. Then try sending the query to the SQL BE DB from VBA.
 

sdbsadmin

Registered User.
Local time
Today, 07:08
Joined
Jun 6, 2013
Messages
14
I removed the dmins and maxes I was using - performance increased instantly. I still want to have everything on the server, so I'm going to try learning the ADO methods. Thanks!
 

mdlueck

Sr. Application Developer
Local time
Today, 07:08
Joined
Jun 23, 2011
Messages
2,631
I removed the dmins and maxes I was using - performance increased instantly.

I believe those functions are actually a part of DAO, thus the query was downloading EVERY row to the client machine and performing those DAO functions against the data.

Such queries could not be passed directly to the SQL BE DB to be executed by them on behalf of Access.

I still want to have everything on the server, so I'm going to try learning the ADO methods. Thanks!

And there is in the real SQL various functions which are available to you that Access SQL did not have available.

I have some very large SQL queries - many pages long when printed - and for that reason I prefer to have queries already on the server in the form of a Stored Procedure. It was getting very VERY hard to transport complicated SQL to the server and not get bit by quoting problems, or max string length in VBA and other similar challenges. Now with using Stored Procedures I may leave "source code comments" in my SP's, retain proper indentation / formatting.... aaaahhhh.... :D
 

mdlueck

Sr. Application Developer
Local time
Today, 07:08
Joined
Jun 23, 2011
Messages
2,631
Then stored procedures it will be. Thank you!

In that case, I offer a sample UPDATE SP for your reference...

Code:
/************************************************************************************/
/* FILENAME       :  products.clsObjProductsTbl_Update.sql                          */
/* TYPE           :  SQL                                                            */
/* DESCRIPTION    :  Stored Procedure to update a record into the products table    */
/*                                                                                  */
/* AUTHOR         :  Michael D Lueck                                                */
/*                   mlueck@lueckdatasystems.com                                    */
/*                                                                                  */
/* NEEDS          :                                                                 */
/*                                                                                  */
/* USAGE          :  id                                                             */
/*                   authid                                                         */
/*                   logtimestamp                                                   */
/*                   title                                                          */
/*                                                                                  */
/* RETURNS        :                                                                 */
/*                                                                                  */
/* REVISION HISTORY                                                                 */
/*                                                                                  */
/* DATE       REVISED BY DESCRIPTION OF CHANGE                                      */
/* ---------- ---------- -------------------------------------------------------    */
/* 09/08/2011 MDL        Initial Creation                                           */
/* 09/13/2011 MDL        Added missed productnumber column                          */
/* 09/19/2011 MDL        Adjusted length of productnumber up to 25 length           */
/* 12/19/2011 MDL        Set default values for non-required parameters             */
/* 06/06/2012 MDL        Added prodteamid/eau cols                                  */
/************************************************************************************/
-- Define name of STORED PROCEDURE for this script
:setvar SPNAME clsObjProductsTbl_Update
USE [Fandango]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Check if the STORED PROCEDURE exists already, DROP if found
IF EXISTS(SELECT * FROM [sys].[objects] WHERE [type_desc] = 'SQL_STORED_PROCEDURE' AND [name] = '$(SPNAME)')
  DROP PROCEDURE [dbo].[$(SPNAME)]
GO
CREATE PROCEDURE [dbo].[$(SPNAME)] (
  -- Add the parameters for the stored procedure here
  @id AS smallint,
  @authid AS smallint,
  @logtimestamp AS datetime2(0),
  @projectid AS smallint,
  @prodteamid AS smallint,
  @title AS varchar(100),
  @productnumber AS varchar(25) = NULL,
  @eau AS int,
  @bomcad AS varchar(MAX) = NULL
  )
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
--SET NOCOUNT ON;
-- Insert statements for procedure here
BEGIN TRAN
UPDATE [dbo].[products]
SET [authid] = @authid,
    [logtimestamp] = CURRENT_TIMESTAMP,
    [projectid] = @projectid,
    [prodteamid] = @prodteamid,
    [title] = @title,
    [productnumber] = @productnumber,
    [eau] = @eau,
    [bomcad] = @bomcad
WHERE [id] = @id
AND [logtimestamp] = @logtimestamp;
COMMIT TRAN

--SET NOCOUNT OFF
END

You are most welcome, sdbsadmin. Welcome aboard!
 

Users who are viewing this thread

Top Bottom