Unknown field name error. Can't figure out SQL statement - sql

I'm sure it's something small, but I can't figure out why this SQL statement isn't working.
INSERT INTO tempTable
SELECT *
FROM
(
SELECT * FROM [table] AS x RIGHT JOIN
(
SELECT DISTINCT a.[Part number], Count(a.[Part number]) AS [Part Count]
FROM [table] AS a
GROUP BY a.[Part number]
)
AS y
ON x.[Part number] = y.[Part number]
);
Every time I run the query in access, I get the error "The INSERT INTO statement contains the following unknown field name: 'Part number'. Make sure you have typed the name correctly, and try the operation again.
When I take out the INSERT INTO wrapping and use just the select statement, it returns the results I want to insert into tempTable.
tempTable was created using this select statement, so I don't think it's an issue of mismatching column names.
Can anybody see an error with my statement?

Are you saying that you created the temp table by using the exact same select query in a Make Table query? Have you looked at tempTable in design view and confirmed the field names? Without seeing all of your fields, my guess is that your select statement outputs multiple fields with the same name.
Additionally, note that 'table' is a reserved word and should not be used as an object name, and the use of spaces in field names is considered poor practice. Just because you CAN do something, doesn't mean that you SHOULD.

Related

Is it possible to avoid specifying a column list in a SQL Server CTE?

Is it possible to avoid specifying a column list in a SQL Server CTE?
I'd like to create a CTE from a table that has many columns so that the structure is identical. There probably is a way to accomplish this without relisting every column name.
I've tried (unsuccessfully):
with pay_cte as
(select * from payments)
select * from pay_cte
I'm encouraged in my quest by this statement in the msdn documentation:
The list of column names is optional only if distinct names for all resulting columns are supplied in the query definition.
https://msdn.microsoft.com/en-us/library/ms175972.aspx
Yes, assuming you mean that you don't have to name every column in the with cte(Col1, Col2) as section.
You can easily try this yourself with a very simple test query along the lines of:
with cte as
(
select *
from sys.tables
)
select *
from cte

SQL using REPLACE on one of many columns

I have the following query:
SELECT * FROM MailingList
There are about 20+ columns in the MailingList table, one which is called Address. This column has some fields which contain commas, which I need to take out. So I updated my query to:
SELECT REPLACE(Address, ',', '') AS Address, * FROM MailingList
But now I have two Address columns. Is there a way to only display one Address column while still using the wildcard (*) for all the other columns?
There is not a way to do this, though listing the columns you want explicitly is a good idea anyway.
You can trick as following query:
Get the data into a temp table
Drop the cloumns that are not needed
Get results and drop temp table
SELECT *, REPLACE(Address, ',', '') AS Address2
INTO #TempTable
FROM MailingList
ALTER TABLE #TempTable
DROP COLUMN [Address]
SELECT * FROM #TempTable
DROP TABLE #TempTable
I agree with Shadow - avoid using the * wild card if you can...
I know listing out ALL of the columns in select statement for big tables is a pain so here is a quick short cut you may not be aware of: In SQL Server Management Studio, browse through the object explorer and find the table you want to select from (MailingList). Right-click it to view the context menu and choose "Script Table as" then "SELECT TO" then "New Query Editor Window". This will create a new select statement with each column spelled out. In the future, use this method to create select statements, queries, procedures, etc. rather then the * wildcard. Performance is better and it just looks nicer :-)
Then you can solve your alias issue with the replace function.

What is the difference in [ ] and ( ) in SQL?

I've seen both used but I can't seem to understand when to use each?
To me is seems like you enter the name of the table you are referring from in the ( ) and the field name in the [ ]?
Could anyone explain?
The square brackets are used In Microsoft products to specify that what's within them is an identifier (the standard quoted identifiers are double quotes " ", which Microsoft SQL Sever also supports). This is used when you have a database name, user name, table name, field name, view name, procedure name (et.c.) that happens to be the same as a keyword, or contains characters that would break the syntax. This is often used in generated code to safeguard against identifiers that can't otherwise be used in the code. A generated query could look like this:
select [Id], [Name], [Password hint]
from [dbo].[MyDataBase].[User]
Here the field name Password hint would break the syntax if used without brackets, and the table name User could conflict with the keyword User.
Parentheses are used to group items, for example as part of the syntax of some clauses, for example an insert:
insert into someTable (field1, field2) values ('value1', 'value2')
They can also be used in expressions:
select Price * (Quantity + FreeItems) from Articles
They can also be used around queries to make subqueries:
select o.Name
from (select Name, Age from Persons where City = 'Oslo') as o
where o.Age > 18
() are used for passing parameters to functions and stored proceedures etc. [] are used to encapsulate field name (etc.) which include punctuation (spaces and special characters as per the comment above). [] are useful sometimes to name fields for display
SELECT FFgg AS [Some field discription] FROM table1;
Hope this helps.

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.
Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId

SQL LIKE query not working

I'm trying to run the following query against an Oracle DB, but the query is returning 0 records:
select * from TABLE
where upper(FIELD) like '%SEE COMMENT%'
I know this field contains many records with 'See Comment" in it. For example, here is one of the records:
=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))
I am guessing that the quotation marks in the field are messing the query up, but im not sure how to get around it. Any suggestions?
This works for me:
create table testLike (aCol varchar2(500) );
INSERT INTO TESTLIKE VALUES('abc');
insert into testLike values('=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))');
SELECT *
FROM TESTLIKE TL
WHERE upper(tl.acol) like '%SEE COMMENT%';
can you recreate?
edit:
in your query try this:
select * from TABLE
WHERE UPPER(FIELD) = '=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))';
see if that comes up with any results
Just realized there were two similarly named fields in this table, and I was choosing the wrong one.