I have a sql query (used with MS Access and C#) that in the where clause references table name with the column, that causes an error. When I remove the table name, it works fine, but it would seem like if you are more specific, it would work better?
Anyone have any ideas why that is?
SQL query in question:
SELECT profiles.profile_id,
profiles.full_name,
shifts.start,
shifts.stop,
shifts.start_log,
shifts.stop_log,
shifts.start_notes,
shifts.stop_notes
FROM shifts,
profiles
WHERE profile.full_name=#full_name AND
shifts.profile_id=profiles.profile_id;
I had to change "profile.full_name=#full_name" in the WHERE clause to "full_name=#full_name" i.e. remove the table name "profile".
This question came from another SO question found here
Your query pulls information from a table named profiles, so profile.full_name is not defined because there is no table (or alias) named profile in the query. profiles.full_name would probably work better.
Related
I'm working on some current, and archived SQLite databases.
In current versions, a column is named message, but in the archived versions it's named message_id.
The query I'm running is pretty lengthy/complex, and it's just this one column that's changed. Is there any way I can do some kind of CASE EXISTS style query to do this, or am I just going to have to write a separate query?
I would suggest writing a view to access the historical data:
create view v_message_history
select message as message_id, . . .
from message_archive;
Then you can use the view and the two columns have the same name.
You could also use alter table to rename the column in either the history or current table. I am guessing, though, that you don't want to do that because it might break existing code.
FORE NOTE: This question regards the IN clause that belongs in the FROM statement and lets you reference to an external database. Do not confuse this with the IN statements that might live in a WHERE clause, please.
Version: MS Access 2016
External table is on the local network
The crux of what I am trying to do is grab an [Employee] table from an external ACCDB database and LEFT JOIN it to a local [Employees] (note the 's') table. I am trying to generate a list of (non-terminated) employees that are not yet added to my local [Employees] table. As in:
SELECT Employee.Last_Name, Employee.First_Name, Employee.Job_Title
FROM Employee IN "\\{full path}\Time Clock 1.0_be.accdb"
LEFT JOIN Employees
ON Employee.Last_Name = Employees.LastName
AND Employee.First_Name = Employees.FirstName
WHERE Employees.FirstName IS NULL
AND Employee.Termination_Date = ""
ORDER BY Employee.Last_Name, Employee.First_Name;
Only the above SQL doesn't work. Access gives me the ever-so-not-very helpful Syntax error in FROM clause to brighten my neurotic insanity.
Does the IN clause have to go last and does it effect both tables? At:
https://msdn.microsoft.com/en-us/library/bb177907(v=office.12).aspx
they say it can be combined with a LEFT JOIN but they don't specify if both tables must be external.
Can you even LEFT JOIN a table from an external DB to an local table? I don't really want to link the table formally as this query will only run occasionally and I don't want any more traffic pinging the Time Clock back end DB than I have to. It's slow enough as it is.
In answer to my original question:
#cha was right to suggest I use nested queries. This solves the problem of an internal table being joined to an external table.
#Gord Thompson had a much more specific way of referencing to an external DB that seems clearer to me than the IN clause in this simple case. Programmers may want to use the IN clause when connecting to different types of external databases as it gives you the ability to specify all that in the IN clause.
In the end none of this helped me because the train-wreck-of-a-database I lovingly caress uses multi-value fields and Access will not link an internal table with multi-valued fields to an external table.
Those who come after you (and probably you yourself) will thank you for observing 1st 2nd and 3rd normal forms except in the most unusual and carefully considered cases and for never ever ever ever using multi-valued fields instead of linking tables for many-to-many relationships.
Aloha!
Firstly, apologies if this is in the wrong section, or the wrong style. Hunted for this answer for a while, to no avail.
Imagine you have a (sample) SQL query in SQL 2014 -
SELECT
dbo.Users.Surname,
dbo.Accounts.Type
FROM
dbo.Users
INNER JOIN
dbo.Accounts
ON (dbo.Users.Id = dbo.Accounts.Id)
Up until now, this is the format I've been using - fully qualifying the table objects with [schema].[tablename].[column].
However, looking at the SQL 2014 Deprecated Database Engine Features, it says that this style is no longer standard -
Two-part names is the standard-compliant behavior.
After digging around for a while, I found the Transact-SQL Syntax Conventions, where it says -
To avoid name resolution errors, we recommend specifying the schema name whenever you specify a schema-scoped object.
So I'm a little confused as to how my little code snippet should be written. Should I only use the schema when referencing the tables, but when referring to columns, skip the schema and just use the table names? Or is it assuming all table objects should have an alias?
Again, apologies for the potential subjectivity of this question. But essentially I'm asking about how to write SQL that does not use a deprecated feature of SQL 2014, but still reads well when joining multiple tables.
It says that the deprecated feature applies to referencing columns, not tables.
To clarify imagine two statements:
SELECT dbo.Orders.ID FROM dbo.Orders
and
SELECT Orders.ID FROM dbo.Orders
The first is deprecated, not the last.
To avoid name resolution errors, we recommend specifying the schema
name whenever you specify a schema-scoped object.
This applies to default schema of user. If user has default schema say 'Person' and two tables with the same name 'dbo.Persons', 'Person.Persons' exist in the database, then if that user executes:
SELECT * FROM Persons
he will get results from table in Person schema, even if he did want data from dbo.
So the actual answer is:
Use
SELECT Orders.ID FROM dbo.Orders
I have a question about creating views in general.
If I have one table "table1" that has two columns, "code" and "value" is it possible to create a view "view1" that has the same data as table1 but just renames the columns to something different like "table1.code" and "table1.value" ? I kno this is a pretty simple question but I havent found much on it. Thanks!
You can alias the columns in the query behind your view. For example, your query might be like below...
SELECT code as table1.code, value as table1.value FROM table1
The exact syntax for creating a view is different depending on the vendor, which you didn't include in your question. But you can alias the columns in a query in almost any implementation, including SQL Server, Oracle, MySql, etc.
Using an alias is as simple as typing the AS keyword, followed by whatever you want to alias the column name as.
In sqlite3, I can force two columns to alias to the same name, as in the following query:
SELECT field_one AS overloaded_name,
field_two AS overloaded_name
FROM my_table;
It returns the following:
overloaded_name overloaded_name
--------------- ---------------
1 2
3 4
... ...
... and so on.
However, if I create a named table using the same syntax, it appends one of the aliases with a :1:
sqlite> CREATE TABLE temp AS
SELECT field_one AS overloaded_name,
field_two AS overloaded_name
FROM my_table;
sqlite> .schema temp
CREATE TABLE temp(
overloaded_name TEXT,
"overloaded_name:1" TEXT
);
I ran the original query just to see if this was possible, and I was surprised that it was allowed. Is there any good reason to do this? Assuming there isn't, why is this allowed at all?
EDIT:
I should clarify: the question is twofold: why is the table creation allowed to succeed, and (more importantly) why is the original select allowed in the first place?
Also, see my clarification above with respect to table creation.
I can force two columns to alias to the same name...
why is [this] allowed in the first place?
This can be attributed to the shackles of compatibility. In the SQL Standards, nothing is ever deprecated. An early version of the Standard allowed the result of a table expression to include columns with duplicate names, probably because an influential vendor had allowed it, possibly due to the inclusion of a bug or the omission of a design feature, and weren't prepared to take the risk of breaking their customers' code (the shackles of compatibility again).
Is there any use to duplicate column names in a table?
In the relational model, every attribute of every relation has a name that is unique within the relevant relation. Just because SQL allows duplicate column names that doesn't mean that as a SQL coder you should utilise such as feature; in fact I'd say you have to vigilant not to invoke this feature in error. I can't think of any good reason to have duplicate column names in a table but I can think of many obvious bad ones. Such a table would not be a relation and that can't be a good thing!
why is the [base] table creation allowed to succeed
Undoubtedly an 'extension' to (a.k.a purposeful violation of) the SQL Standards, I suppose it could be perceived as a reasonable feature: if I attempt to create columns with duplicate names the system automatically disambigutes them by suffixing an ordinal number. In fact, the SQL Standard specifies that there be an implementation dependent way to ensure the result of a table expression does not implicitly have duplicate column names (but as you point out in the question this does not perclude the user from explicitly using duplicate AS clauses). However, I personally think the Standard behaviour of disallowing the duplicate name and raising an error is the correct one. Aside from the above reasons (i.e. that duplicate columns in the same table are of no good use), a SQL script that creates an object without knowing if the system has honoured that name will be error prone.
The table itself can't have duplicate column names because inserting and updating would be messed up. Which column gets the data?
During selects the "duplicates" are just column labels so do not hurt anything.
I assume you're talking about the CREATE TABLE ... AS SELECT command. This looks like an SQL extension to me.
Standard SQL does not allow you to use the same column name for different columns, and SQLite appears to be allowing that in its extension, but working around it. While a simple, naked select statement simply uses as to set the column name, create table ... as select uses it to create a brand new table with those column names.
As an aside, it would be interesting to see what the naked select does when you try to use the duplicated column, such as in an order by clause.
If you were allowed to have multiple columns with the same name, it would be a little difficult for the execution engine to figure out what you meant with:
select overloaded_name from table;
The reason why you can do it in the select is to allow things like:
select id, surname as name from users where surname is not null
union all
select id, firstname as name from users where surname is null
so that you end up with a single name column.
As to whether there's a good reason, SQLite is probably assuming you know what you're doing when you specify the same column name for two different columns. Its essence seems to be to allow a great deal of latitude to the user (as evidenced by the fact that the columns are dynamically typed, for example).
The alternative would be to simply refuse your request, which is what I'd prefer, but the developers of SQLite are probably more liberal (or less anal-retentive) than I :-)