Select names of columns that have no data in Access? - sql

I'm working on a C# application that imports data from Access into SQL Server. They select an Access file, then a table in the file.
I then perform checks on the data to see if it's valid to import. I want to display a list of columns from the table that have no data in them, so the user has to confirm they want to import regardless of certain empty columns.
Is there an approach to this in Access besides looping through SELECT ... WHERE (field) IS NULL queries?

Is there an approach to this in Access besides looping through SELECT ... WHERE (field) IS NULL queries?
I don't know of an alternative to looping, but I will suggest a different strategy for the queries you run in the loop. Seems to me you would want to know whether any rows include non-Null values for the given field.
SELECT Count(*) AS row_count
FROM Table_Name
WHERE field_name Is Not Null;

No looping necessary
Use Count(*) to get the number of total records
Use Count with WHERE (field) IS NULL and compare the counts
If the counts are equal all of the rows are null for that column.

Related

What does SELECT Function is SQL actually produce? Does it produce a new table by default?

I am struggling to understand what the output of SELECT is meant to be in SQL (I am using MS ACCESS), and what sort of criteria this output needs to specify, if any. As a result, I don't understand why some queries work and others don't. So I know it retrieves data from a table, does calculations with it and displays it. But I don't understand the "inner" working of SELECT function. For instance, what is the name of data structure / entity it displays? Is it a "new" table?
And for example, suppose I have a table called "table_name", with 5 columns. One of the columns called "column_3", and there are 20 records.
SELECT column_3, COUNT(*) AS Count
FROM table_name;
Why does this query fail to run? By logic, I would expect it to display two columns: first column will be "column_3", containing 20 rows with relevant data, and second column will be "Count", containing just one non-empty row (displaying 20), and other 19 rows will be empty (or NULL maybe)?
Is it because SELECT is meant to produce equal number of rows for each column?
Your questions involve a basic understanding of SQL. SELECT statements do not create tables, but instead return virtual result sets. Nothing is persisted unless you change it to an INSERT.
In your example question, you will need to "tell" the SQL engine what you want a count "of". Because you added column_3, you need to write:
SELECT column_3, COUNT(*) AS Count
FROM table_name
GROUP BY column_3
If you wanted a count of all the rows, simply:
SELECT COUNT(*) FROM table_name

How can I add blank columns to the results of a SQL query?

I'm working on a query that pulls demographic information for people who have visited a location. However, the fields required for this report aren't all available in the DB, and some will need to be added manually from a separate Excel file after I've exported the results of my query. The person who will be responsible for merging the two files asked if it would be possible to create blank columns so they can more easily see where the missing data needs to go, and I wasn't sure how to go about that. Obviously those blank columns could just be created in the exported spreadsheet, but I wondered if there was a way to add them in the SQL query itself.
My SELECT statement currently looks something like this—I've just added comments to mark the missing fields so I can keep track of what order all the fields for this report need to be in.
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
--StaffFName,
--StaffLName,
FacilityPhone,
FacilityAddress,
...and so on
Since those two staff name fields don't exist in my DB, I obviously can't actually include them in the SELECT list. But is there a way to still include those two fields as blank columns in my query results? Something along the lines of "select [nothing] as StaffFName"?
Just add literal nulls to the select clause:
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
null as StaffFName,
null as StaffLName,
FacilityPhone,
FacilityAddress,
...
Or, if you prefer, you can use empty strings instead:
...
'' as StaffFName,
'' as StaffLName,
...
But null is the canonical way to represent the absence of data.

VB 2010 Using DISTINCT in SQL

using vb 2010 on an access database
INSERT INTO UniqueTable
SELECT DISTINCT
1,2,3,4,5
FROM DataTable
This will get only unique rows in all fields and if I only specify the one field I want to be distinct it only inserts the data in that field
How can I import all data from every field where field 5 is unique?
If I set the database field properties to not allow duplicates all import fails.
Thanks
Don't use distinct in this case, you can't specify which field need to be distinct, it works for entire columns selected. Use group by instead, like: ..GROUP BY 5 .. HAVING COUNT(*) = 1. That will return all rows having field 5 value appear only once in the table, in other word distinct.

Delete rows conditionally

I have a dataset with over 100,000 rows, over 100 columns and where some values are NULL. Now I want to remove all the rows which contain NULL values.
Can anybody suggest the sql command for it?
With the little information you've provided:
DELETE FROM table WHERE colA IS NULL OR colB is NULL
Add further conditions for each column that you want to check.
Change OR to AND if you only want to delete rows where all of the columns are NULL.
It's fairly easy to generate the SQL for this using a query on user_tab_columns if you don't want to type it out by hand.
use a scripting language like PHP to retreive all column names and then construct your SQL query.
Using pure SQL could get tricky.

How to only insert unique data into an access database

I have an access database that I need to update only if my information is unique. Is there a simple sql statement to accomplish this? Will 'insert ignore' work with access?
Example: I have the info stored in an array ('bob','34','hair'). If my database contains a record that matches on those three columns I would not want it to be inserted. If it was found to be unique I would like it to be inserted.
I am writing this in cold fusion but just cant seem to get the sql right.
Before doing an insert, do a select for those values. If you don't get a record back then you know it is safe to insert. Just use two separate queries, one for the check, and if no record found, then the insert.
A unique index is always a good idea if a field or set of fields should be unique. If you have a unique index in Access on the three fields, an insert will fail.
It is possible is to create a single query that only inserts a record where a matched record is not found, for example:
INSERT INTO Shows (ForeName,Reviews,Musical)
SELECT "bob" As ForeName,"34" As Reviews,"hair" As Musical
FROM (SELECT Count(*) As ExistsCount
FROM Shows
WHERE ForeName = "bob",Reviews = "34",Musical = "hair") AS e
WHERE e.ExistsCount=0