Is there anything wrong with this statement?
SELECT *
FROM Movies INNER JOIN
Sessions
ON Movies.MovieID=Sessions.MovieID INNER JOIN
Tickets
ON Sessions.SessionID=Tickets.SessionID;
When ever I run it on Access I get a Syntax error 'Missing Operator'.
Also are there any alternatives to Access that I can import data from an excel spread sheet?
In general, no. In MS Access, yes. It likes extra parentheses, probably because the database developers don't believe in readability:
SELECT *
FROM (Movies INNER JOIN
Sessions
ON Movies.MovieID = Sessions.MovieID
) INNER JOIN
Tickets
ON Sessions.SessionID = Tickets.SessionID;
You could enable OPENROWSET if you have a local instance of SQL, and install MDACs (I would install both x86 and x64 if you have a 64 bit pc). Below is a link to an article that will help you get setup. Also, be sure to run the management studio with elevated privileges.
How to enable Ad Hoc Distributed Queries
Below is how the query would look. In my example I use Excel 8.0 instead of 12 because the column names are addressable in my select statements for 8.
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=C:\Temp\MyExcelDoc.xlsx;',
'SELECT * FROM [Sheet1$]')
You can export data from an excel spreadsheet into a number of formats. I find two of the best to be;
Comma Separated Values (CSV)
XML
In many cases, you can deal with the data directly from the Excell spreadsheet.
It really depends on what you want to do.
Your SQL query looks fine - but with Access you need to do the extra joins in brackets;
SELECT * FROM ((Movies
INNER JOIN Sessions ON Movies.MovieID=Sessions.MovieID)
INNER JOIN Tickets ON Sessions.SessionID=Tickets.SessionID)
;
Related
We have an item cull that retrieves items and their appropriate data from one server. Their Primary Keys need to be INNER JOINed with another table. However, the second table is on another server. We can retrieve that data using a separate select statement and accompanying VB objects.
How would one go about this programmatically in VB? Is a loop the best practice available? Although we are in a Microsoft Ecosystem, Server Linking is not possible.
Use openrowset statement.
Syntax:
SELECT *
FROM OPENROWSET('SQLNCLI10',
'server=REMOTEDBSERVERNAME;database=DBNAME;uid=USERID;pwd=PASSWORD',
'SELECT * FROM table')
Try it.
Tiz
I have a database with many tables that get used, and many tables that are no longer used. While I could sort through each table manually to see if they are still in use, that would be a cumbersome task. Is there any software/hidden feature that can be used on a SQL Server/Oracle database that would return information like "Tables x,y,z have not been used in the past month" "Tables a,b,c have been used 17 times today"? Or possibly a way to sort tables by "Date Last Modified/Selected From"?
Or is there a better way to go about doing this? Thanks
edit: I found a "modify_date" column when executing "SELECT * FROM sys.tables ORDER BY modify_date desc", but this seems to only keep track of modifications to the table's structure, not its contents.
replace spt_values with the tablename you are interested in, the query will give the the last time it was used and what it was used by
From here: Finding Out How Many Times A Table Is Being Used In Ad Hoc Or Procedure Calls In SQL Server 2005 And 2008
SELECT * FROM(SELECT COALESCE(OBJECT_NAME(s2.objectid),'Ad-Hoc') AS ProcName,execution_count,
(SELECT TOP 1 SUBSTRING(s2.TEXT,statement_start_offset / 2+1 ,
( (CASE WHEN statement_end_offset = -1
THEN (LEN(CONVERT(NVARCHAR(MAX),s2.TEXT)) * 2)
ELSE statement_end_offset END) - statement_start_offset) / 2+1)) AS sql_statement,
last_execution_time
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 ) x
WHERE sql_statement like '%spt_values%' -- replace here
AND sql_statement NOT like 'SELECT * FROM(SELECT coalesce(object_name(s2.objectid)%'
ORDER BY execution_count DESC
Keep in mind that if you restart the box, this will be cleared out
In Oracle you can use the ASH (Active Session History) to find info about SQL that was used. You can also perform code coverage tests with the Hierarchical profiler, where you can find which parts of the stored procedures is used or not used.
If you wonder about the updates on table data, you can also use DBA_TAB_MODIFICATIONS. This shows how many inserts, updates, deletes are done on a table or table partition. As soon as new object statistics are generated, the row for the specified table is removed from DBA_TAB_MODIFICATIONS. You still have help here, since you could also have a peek in the table statistics history. This does not show anything about tables that are queried only. If you really need to know about this, you are to use the ASH.
Note, for both ASH and statistics history access, you do need the diagnostics or tuning pack license. (normally you would want this anyway).
If you use trigger you can detect update insert or delete on table.
Access is problably more difficult.
I use a combination of static analysis in the metadata to determine tables/columns which have no dependencies and runtime traces in SQL Server to see what activity is happening.
Some more queries that might be useful for you.
select * from sys.dm_db_index_usage_stats
select * from sys.dm_db_index_operational_stats(db_id(),NULL,NULL,NULL)
select * from sys.sql_expression_dependencies /*SQL Server 2008 only*/
The difference betweeen what the first 2 DMVs report is explained well in this blog post.
Ed Elliott's open source tool, SQL Cover, is a good bet and has built-in support for the popular unit testing tool, tSQLt.
I've got two databases on the same server. The Google gave me some hints but there wasn't anything "official" that I could find. Could someone point me to the documentation that explains how to do this? An explanation using PHP would be useful as well. Thanks!
I've got two databases on the same server. ...How do I construct a cross database query in MySQL?
You access other databases on the same MySQL instance by prefixing the table with the appropriate database name. IE:
SELECT *
FROM this_database.table_1 t1
JOIN that_database.table_2 t2 ON t2.column = t1.column
Keep in mind
A query executes with the credentials of the authentication used to set up the
connection. If you want to query two tables simultaneously across two (or more)
databases, the user used to run the query will need SELECT access to all
databases involved.
Reference:
Identity Qualifiers
SELECT * FROM DB1.myTable1 AS db1, DB2.myTable2 AS db2
http://www.dottedidesign.com/node/14 provides the following example:
SELECT
arbogast.node.nid as anid,
mcguffin.node.nid as mnid,
arbogast.node.title as atitle,
mcguffin.node.title as mtitle
FROM arbogast.node, mcguffin.node
WHERE arbogast.node.nid = 1
AND mcguffin.node.nid = arbogast.node.nid;
Where arbogast and mcguffin are different databases.
Can someone please help me out with a query? In Microsoft SQL Server, I have the following query, which executes fine:
SELECT * FROM ControlPoint
INNER JOIN Project ON ControlPoint.ProjectID = Project.ProjectID
INNER JOIN Site ON Site.SiteID = Project.SiteID
WHERE Project.ProjectName LIKE '%Flood%'
My problem is, when I try to execute this on Microsoft Access, it gives me some kind of syntax error. It's been forever since I've used Access, but if I remember right, I think the joins need to be in parenthesis or something. Any help would be useful!
You will need some parentheses in addition to changing the wild cards:
SELECT * FROM (ControlPoint
INNER JOIN Project ON ControlPoint.ProjectID = Project.ProjectID)
INNER JOIN Site ON Site.SiteID = Project.SiteID
WHERE Project.ProjectName LIKE '*Flood*'
Note that the asterisk is used in the Access query window and with DAO, percent is used with ADO.
Access uses different wildcard patterns.
In your case, it will be - LIKE '?Flood?' (replace question mark with asterisk).
I don't know formatting codes to apply here so that it shows it correctly.
See the link for details - http://www.techonthenet.com/access/queries/like.php
The syntax error is caused because access uses " instead of ' as the string delimiter. As previously mentioned you will also need to change the % wildcards to *
Use this sintax in access never fails
(this is an example)
select * from customer, adress
where customer.id = adress.customerId
In MySQL, you can use the keyword USING in a join when you join on columns from different tables with the same name. For example, these queries yield the same result:
SELECT * FROM user INNER JOIN perm USING (uid)
SELECT * FROM user INNER JOIN perm ON user.uid = perm.uid
Is there an equivalent shortcut in SQL Server?
nope, have to use:
SELECT * FROM user INNER JOIN perm ON user.uid = perm.uid
No, SQL Server does not support this kind of shortcut.
I would like to point out that even if it did, shortcuts like these are NOT A GOOD IDEA. I recently worked on a database where the developers thought it would be a good idea to use the *= and =* shortcuts for RIGHT JOIN and LEFT JOIN. Good idea until someone upgraded the SQL Compatibility level to 90. Then it became an extremely Bad Idea.
So, learn from us. Shortcuts are bad. A little extra typing never killed anyone.
Also, I would add not to use wild characters, "*" in your select statement - explicitly specify the column name.