SQL-Invalid column name - sql

I have 3 temp tables , using 2 temp tables i will join and create a new temp table using "into" command. In the two parent tables i have a column named abc but when i use the same in the join condition i get an error mentioning invalid column name.
Invalid column name 'abc'.
can anyone help with the issue

Perhaps you need to be more specific about which column you mean, since both tables have a column with the same name. Aliases may make this more convenient.
INSERT #mytemptableC (x, y, z)
select a.q, a.p, b.r
from #mytemptableA a inner join #mytemptableB b on a.q=b.q
Also, if your abc column has a complex name with symbols or spaces, such as col #1, you may need to enclose it in square braces [col #1].
Without seeing the existing code, though, it is difficult to guess. Please reply with your existing SQL statement and I'm sure the answer will be provided very quickly.

Related

Syntax for explicitly referencing a column name

I'm trying to make a join to a date table on a column called 'date'.
The issue I have is that the column name is the same as the table and it seems to be making a referencing to the table rather than the column (hence the yellow highlighting). I've found a workaround which is to rename the table - though does anyone know how to explicitly reference the column in the join rather than the table if they have the same name?
You can give aliases to the table/column and use that instead. Otherwise, you can use the table name before the column with a . as a separator, i.e., TableName.date = date.

Oracle SQL "column ambiguously defined" with `FETCH FIRST n ROWS ONLY`

I have a query SELECT A.ID, B.ID FROM A, B that works fine.
As soon as I add FETCH FIRST n ROWS ONLY to it, the query fails with the error message
SQL Error [918] [42000]: ORA-00918: column ambiguously defined
As far as I understand, the error refers to an ambiguous SELECT clause and should not be caused by a FETCH FIRST n ROWS ONLY.
Do I miss something that justifies this behaviour? Or is this a bug?
I know that I can omit this behaviour when I specify an explicit column alias. I want to know, why SELECT A.ID, B.ID FROM A, B works, while SELECT A.ID, B.ID FROM A, B FETCH FIRST 10 ROWS ONLY doesn't.
The Oracle version is 12.1.0.2.0
This is documented in oracle documents:
Restrictions on the row_limiting_clause
If the select list contains columns with identical names and you
specify the row_limiting_clause, then an ORA-00918 error occurs. This
error occurs whether the identically named columns are in the same
table or in different tables. You can work around this issue by
specifying unique column aliases for the identically named columns.
Even though SELECT query works, after using FETCH FIRST|NEXT, it will throw error if two of the column names are same.
You should just assign different alias names for all columns in SELECT clause.
Learn to use proper, explicit, **standard* JOIN syntax. Never use commas in the FROM clause. But that is not your problem.
You have two columns with the same alias. Simply use as to assign new aliases:
SELECT A.ID as a_id, B.ID as b_id
What you are observing may be a bug. The code seems to work on other versions of Oracle.

PostgreSQL match string to dynamically entered string

I have a varchar field in my database table A let's call it store_name, this field gets its value from entity A, now entity B enters store_name into a different database table B now I want to get all records in table A where the store_name matches the values in table B.
How would you recommend me doing the query as I don't control the values of those 2 fields?
What do you think about PostgreSQL fuzzystrmatch?
The tables contain thousands of records.
Thanks
Assuming that both table A and table B are in the same database. And I guess since you don't control insertion of data, you are not sure if the values are of same case or there may be a spelling mismatch.
Case 1: If the problem is only of case-mismatch, you can use ilike:
Select a.store_name
from a, b
Where a.store_name ilike b.store_name
Case 2: If you also want to check for spelling mismatch, but words sound similar, then after installing postgresql-contrib package and creating extension fuzzystrmatch, you can use:
Select a.store_name
from a, b
Where a.store_name ilike b.store_name OR
soundex(a.store_name) = soundex(b.store_name)
If you are dealing with names, which may not always be in English, it may be more appropriate to use metaphone or dmetaphone function instead of soundex.
Documentation: Fuzzystrmatch
If you want matching you can use a straight up join.
Select a.store_name
from a
join b on a.store_name = b.store_name;
If you want to use fuzzy matching just use the various functions available in the join criteria. Documentation here
Note: there are some limitations to Fuzzy string matching so i would advise testing each out on values that you either know match or don't.

Inserting new rows into table-1 based on constraints defined on table-2 and table-3

I want to append new rows to a table-1 d:\dl based on the equality constraint lower(rdl.subdir) = lower(tr.n1), where rdl and tr would be prospective aliases for f:\rdl and f:\tr tables respectively.
I get a function name is missing ). message when running the following command in VFP9:
INSERT INTO d:\dl SELECT * FROM f:\rdl WHERE (select LOWER(subdir)FROM f:\rdl in (select LOWER(n1) FROM f:\tr))
I am using the in syntax, instead of the alias based equality statement lower(rdl.subdir) = lower(tr.n1) because I do not know where to define aliases within this command.
In general, the best way to get something like this working is to first make the query work and give you the results you want, and then use it in INSERT.
In general, in SQL commands you assign aliases by putting them after the table name, with or without the keyword AS. In this case, you don't need aliases because the ones you want are the same as the table names and that's the default.
If what you're showing is your exact code and you're running it in VFP, the first problem is that you're missing the continuation character between lines.
You're definitely doing too much work, too. Try this:
INSERT INTO d:\dl ;
SELECT * ;
FROM f:\rdl ;
JOIN f:\tr ;
ON LOWER(rdl.subdir) = LOWER(tr.n1)

error with a sql query because of ambiguous column name

I'm trying to create a sql query, but there is this error:
Ambiguous column name 'description'.
Its because this column occurs in both tables.
if I remove the description from the query, it works.
I tried to rename the description-field "AS description_pointer", but the error still occurs.
SELECT TOP 1000 [activityid]
,[activitytypecodename]
,[subject]
,[regardingobjectid]
,[contactid]
,[new_crmid]
,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact]
ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'
Both tables coming into play in the query have a column named description. You RDBMS cannot guess which column table you actually want.
You need to prefix the column name with the table name (or table alias) to disambiguate it.
Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.
Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:
SELECT TOP 1000
i.[activityid]
,i.[activitytypecodename]
,i.[subject]
,c.[regardingobjectid]
,c.[contactid]
,c.[new_crmid]
,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact] as c
ON i.[regardingobjectid] = c.[contactid]
WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'