Access has the ability to create Queries, the nearest equivalent in SQL Server is a View. Access Queries are simpler to use than Views, although a lot of progress has been made in extending the power of Views and making them easier to use and SQL Server has a graphical interface in some ways more powerful than Access for designing Views.
One of the main uses for Views is in giving users restricted access to data, it would be possible to only allow a user to have access to a set of views and not the underlying tables; restricting both the rows and columns that a user can see.
Before you dive into converting all your Access queries into Views you should fully understand the implications of this, even when using .NET you may still wish to consider other options.
Here are a list of things you should consider when deciding to convert a Query to a View.
On the plus side, views offer good performance and are excellent for giving users an updateable data set which reflects security constraints.
If you need to re-write a query then you could if using Access consider changing the query type to a Saved Pass-Through Query. Pass-Through Queries return Read-Only sets of data and the SQL must be converted from Access-SQL to SQL Server-SQL (this also applies to Views, Stored Procedures and Functions). You can also construct an Action Query (Insert,Update,Delete) using Pass-Though, these queries are distinguished by the fact that they do NOT return a recordset (queries have a property that gets set to reflect this). Pass-Through Queries can also be used in VBA program code with DAO.
The Access SQL for an unrestricted (no where clause) delete query “DELETE * FROM tablename”, needs converting to “DELETE FROM tablename”; dropping the “*”. A better choice is “TRUNCATE tablename”, this is much faster because the before image of the data does not get written to the SQL Server Transaction Log File. You can only use this command on a table which is NOT participating in any Relationships.
The general syntax of the query needs changing here, but more importantly there also needs to be a slight change to the structure of the query, when as in this case the update is based upon data in another table.
If your Access query had used [CustomersToUpdate]![ContactName], which is quite common, then the ! needs to be changed to a “.”
This example shows a simple example of where the syntax in Access differs from SQL Server. The important point is the need to change the double-quotes around the string to single-quotes. The ; at the end can optionaly be dropped.
Access uses -1 and 0 for True/False or Yes/No, where as SQL Server uses 1 and 0, and does not understand True/False or Yes/No. This can be quite confusing, because when you link to a SQL Server table you will see Yes shown as -1, but if you look at the same data on SQL Server it will be 1. So what is happening is that Access performs a clever translation back and forth with SQL Server.
If you change the SQL from Access to SQL Server, you need to change the syntax.
Now just watch because in a pass through query when displaying a result set if you select the Yes/No field from SQL Server, it will show the values -1, because Pass-Through queries do a quick conversion to the world of Access before showing the results; this is desireable although it can be confusing.
One final word, in Access Yes/No fields can have two values -1 and 0, but in SQL Server the equivalent BIT field can have three values 1, 0 and NULL. This is why when upsizing tables we must add defaults to any Yes/No fields before creating the tables on SQL Server, as compared to Access which automatically defaults Yes/No to False SQL Server defaults to null if no explicit default is specified.
The sample code can be downloaded from here Access 2000 Database.
by Andrew Couch Office Access MVP, Director ASC Associates
andy@ascassociates.biz