Getting the table structure in ms access with SQL query? - sql

How to get the table structure in MS Access with a SQL query?
Using the following query:
SELECT name FROM MSysObjects
Results in the following exception:
Exception: [Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on 'MSysObjects'.

To resolve the permissions issue for MSysObjects, see these similar questions:
no read permission on 'MSysObjects', or
Run C# queries against (hidden) system tables in Access?

Use This
SELECT * FROM MSysObjects WHERE Type=1 AND Flags=0
Ms Access has several system tables that are, by default, hidden from tables list. You can show them.
In Ms Access 2007 do a right click on tables list and select Navigation Options. At the bottom of the form you will find Show System Objects check box. Check it and system tables will show up in tables list. They all start with MSys.
Alternatively, options form can be activated from application menu - click button Access options -> select Current Database and there is Navigation Options button.
Now you can examine structure and contents and generate queries of all system tables with MsAccess tools.
Source

Set up an ODBC connection for your postrgres database and then call the transfer database command for each of your tables.

Related

Unable to import/link table from SQL Server into access. Too many indexes error

I am trying to import a table from SQL into access but I am getting too many indexes in the table error. The table in SQL is indexed to several tables and unfortunately I don't have the rights to modify or change the table anyway. just have read access in the DB. I am trying to import/link the table but I am unable to do so due to too many indexes error being thrown.
Is it possible to only import/link the data and not indexes? I know access has a limit of 32 indexes. I have cleared auto indexes in options too, but I still get the error.
Is there a solution as to how I can import/link this table in access?
Thanks in advance
I did some digging on how to avoid this problem. So the problem is we can't directly link to the SQL database if the table has a lot of indexes and Access will throw an error "too many indexes on the table you are trying to import".
one way to beat this is by putting together a "Pass-through query". Lot of people suggested VBA code. I am not a coder and could not get it to work effectively. However, Access gives you the capability to build it with out VBA and the solution I found was in Microsoft website.
The steps are as follows: you might have to work around it for 2010 due to different naming convention when compared to 2007.
On the Create tab, click Query Design in the Other group.
Click Close in the Show Table dialog box without adding any tables or queries.
Save the query. Open the Query in design mode
On the Design tab, click Pass-Through in the Query Type workgroup.
Click Property Sheet in the Show/Hide workgroup to display the property sheet for the query.
In the query property sheet, place the mouse pointer in the ODBC Connect Str property, and then, click the Build (...) button.
With the ODBC Connect Str property, you specify information about the database to which you want to connect. You can type the connection information, or click Build, and then enter information about the server to which you are connecting.
When you are prompted to save the password in the connection string, click Yes if you want the password and logon name to be stored in the connection string information.
If the query is not the type that returns records, set the ReturnsRecords property to No.
In the SQL Pass-Through Query window, type your pass-through query. For example, the following pass-through query uses the Microsoft SQL Server TOP operator in the SELECT statement to return only the first 25 orders in the Orders table from the sample Northwind database:
SELECT TOP 25 orderid from orders
To run the query, click Run in the Results group on the Design tab. For a SQL pass-through query that returns records, click Datasheet view on the status bar.
If necessary, Microsoft Access prompts you for information about your server database.
This worked for me. If some one is having the same problem you can use these steps.

Invalid object name LocalDb Table

I'm trying to run a simple SQL query on a LocalDb database in Visual Studio 2013.
Here's the query in a file GrabWords.sql:
SELECT * FROM Words
where Words is a table in the NextGen.mdf database. When I run this, I get the error:
Invalid object name 'Words'.
For more information, I actually have two databases in the project. In the Solution explorer one is called NextGen.mdf, and the other Cards.mdf. In server explorer, the first (that I am trying to query) is called AutoGenEntities and the other is CardsDBContext.
I just want to run this query to test queries and view the database - is there another tool I should be using to do this?
Try this:
USE [your Database name]
SELECT * FROM Words
This should ensure that you are targeting the correct database.

Importing query results from another database

So I have two Access databases, let's call them myDatabase and anotherDatabase. In anotherDatabase there is a crosstab query. I want to get the results that this query produces into myDatabase without making ANY modifications to anotherDatabase or the query itself. I want myDatabase to be enitrely self-sufficient in the sense that the databases it interacts with will not have to be modified.
Could anyone give me advice on how to approach this?
Linking external tables is limited to external tables and won't allow you to get data from queries in another db.
One solution for external queries is to create a local query using the IN predicate:
SELECT * FROM myQuery IN 'c:\test\otherdb.mdb'
Use the Linked Table Manager to link the tables in anotherDatabase to myDatabase. Then you can have the query for anotherDatabase in myDatabase and just use that.
In the External Data tab (in myDatabase), click the button that says Import From Access. Except, instead of importing the table, click the radio button that says "Link to the data source by creating a linked table". Just follow the wizard and you should be all set.
You only need to link the tables that you require from your query. Another option would be to write some vba code in myDatabase that instantiates a connection to anotherDatabase and queries it, but I think that just linking the tables is a better solution with less hassle

SQL Server Management Studio connection defaults to 'master' when selecting a database-specific object

In SQL Server 2008 R2 Management Studio, if I right-click on an object inside a specific database and choose "Select top 1000 rows ..", the database connection for the query window always opens on 'master' while the table name is fully qualified as [database].[dbo].[table]. This makes it impossible to jump in and tweak out this query and insert joins, etc., to the statement without also fully-qualifying everything I add, or add a USE statement, or select the database from the drop-down menu.
Is there a setting or something that will make query windows open with a database connection of the selected object browser's database rather than connect to 'master', and not fully qualify the object's database in the query text? I realize that I can register my SQL connection to default to my database, but we actually go through multiple new databases every week--in a given month I will have touched tens of databases--so it would be difficult to manage multiple database registrations. I would rather it if SSMS just connected to the specified database. Possible and straightforward?
If you are going in via Win Auth, are in a group, are using SA or some other userid, or are in a situation where changing your login is not really the solution, AND if all you wish to do is default to a database in the query editor:
In an existing open query editor, right-click, select Connection, Change Connection.
Click the Options button to expand the options.
In the Connection Properties tab, select the database you wish to connect to.
SSMS will remember your selection for that server. You may have to repeat for other servers, but it does remedy having a default database other than master.
There is no such setting for the SELECT TOP command, but you may be able to do this by changing the default database for your login. This is tedious if you're doing this often for various databases (much like changing the registrations, as I just noticed you already outlined).
Instead of using SELECT TOP 1000 (which in addition to not putting you in the right database context, also puts a TOP in that I assume you're just going to remove as well), you should right-click the table and choose Script Table as > SELECT to > New Query Window. This puts the context in the right DB, adds a USE command, doesn't have a TOP and doesn't database-prefix the table name.
If you want query window connects to some database by default, in SSMS go to the Security -> Logins, select the login that you use to connect to this server, and loock at the properties window. In page 'general' change the default database from 'master' to database you want to connect.
You could just put a USE [database name] at the top of the query window prior to executing a query. You do not need to fully qualify the database names if you do this. If you generate any scripts and version control them, this is a good practice to put at the top anyway. It at least prevents executing the script erroneously against the wrong database (say creation of a stored procedure).
USE MySpecialDatabase
GO
SELECT * FROM MySpecialTable

Sql Query into excel

I have a sql query that pulls in form multiple tables, how can I run that query to import into an excel sheet?
I presume that's 'from multiple tables'. In Excel got to the Data menu > Get External Data > From Other Sources > From Microsoft Query and open an ODBC connect. If you can't get the query right in Microsoft Query declare it as a View in your Database and of course you need an ODBC connection to the database
Not knowning your toolset, or even DB Platform, here's a guess.
http://www.mssqltips.com/tip.asp?tip=1202