SQL command for reading all columns in the first row - sql

I have an Excel sheet in which the first row contains title for all the columns. I want all the names in the first row.
What is the SQL command for reading all the entries in the first row?
If possible, I want to define the max-limit.
In addition: I want to enumerate all the column names.

You need to mention "HDR=No;" in your connection string. Then you can use the following query:
Select Top 1 * from [SheetName$]
"HDR=No" will specify that the first row DOES NOT contains the column names.
Being said that, I don't know if there is a SQL statement to get the column names from a table. If there is, then you can have HDR=Yes in the connection string and use that SQL statement.

I hope I'm understanding this right....but I think you're saying that you want to select the column names from a table:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = [YourTableName]

This works for me in Excel using a saved workbook, and it enumerates the column (field) names.
Sub ListFieldADO()
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
''You can also use the name of a named range
Set rs = cn.OpenSchema(adSchemaColumns, _
Array(Empty, Empty, "Sheet1$"))
While Not rs.EOF
Debug.Print " " & rs!Column_Name
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
End Sub

Select Top 1 * into #temp from [SheetName$]
use tempdb
sp_help #temp.
By this, you can get the column names of the #temp table. Again, you have to change the database to:
use yourDBName
Put the data in a temporary table and read the properties of the temporary table and from that you can get the list of column names.

Related

Access VBA Putting name of a Field as a Record

I am with a little bit of a coding problem that I don't know how to solve. I want to put in the table as a record a field name of another table in the same Database.
I will give you an example:
Table1
What I have
Table 2
What I want
The table 1 is update weekly from an external source so I need to record the field name as a record in the second table using VBA language. Does anybody knows if it's possible?
Thank You in advance
You could do something like this
Dim db As Database
Dim fld As Field
Dim sql As String
Set db = CurrentDb
For Each fld In db.TableDefs("YourTable").Fields
sql = "Insert into YourSummaryTable([Date], Hours) select '" & fld.Name & "', sum([" & fld.Name & "]) as s from YourTable"
DoCmd.RunSQL sql
Next fld
Note that you've used a reserved word Date as a fieldname, which isn't best practice and requires the use of the square brackets in the query.

Excel not selected the range specified in query

i am bulk inserting the worksheet to a temp table in SQL, after that i am calling a stored procedure to move data from temp to main table.
for insert i am using the below statement
s = "select * into [" & ThisWorkbook.Sheets("Master Control").Range("F2") & "].[" & Environ("username") & "] FROM [ABC$A13:IU5000] "
cn.Execute s
the problem is that, even after putting the range in the query, it is only picking upto the last column which have data, which is creating problem.
How can i make it to select exactly the range i specified?
I just posted my code to Transfer an Excel Range INTO a Database
This works for me.
SELECT * INTO [TagetTable] FROM [Excel 8.0;HDR=YES;DATABASE=C:\stackoverflow\test-stub.xlsx].[ABC$A13:IU5000]
This is your Query string:
select * into [F2].[best buy] FROM [ABC$A13:IU5000]
Is F2 the name of the database your are trying to query? Your missing the filepath in the FROM Clause

Saving a query into a Table

I have searched for a question that I am having, though I find parts of the answer I am not able to understand it fully. I am using access2010.
In simple Terms, I want to filter a table [newsearch] to show the results based on my WHERE condition.
I can use SELECT and a WHERE condition and I get a result through a query, but I want this result to be saved into [newsearch], that means I want this [newsearch] to contain only the results of this query.
I tried using SELECT INTO but since my source and destination are [newsearch], it does not work.
the query I run now is:
strSQL = "SELECT * FROM [newsearch] WHERE [newsearch].[" & Me.Combo17 & "] = '" & Me.Text18 & "'"
Set qdef = db.CreateQueryDef("User query results", strSQL)
qdef.close
Set qdef = Nothing
Set db = Nothing
DoCmd.OpenQuery "User query results", acViewNormal
This gives me the result in a query table but I want it saved to [newsearch].
How can I do it?
select into query creates a table, but obviously you cannot create a table that already exists. You have 2 options:
Instead of selecting what you need, delete those records from your table that you do not need: delete from [newsearch] where field3<>xyz
You can use select into to create a new table, then drop [newsearch] and then rename the new table to [newsearch].

Can I use a subquery to choose the column I want to select in SQL in MS Access?

I have a query written that will return a single record (a class number) from table1. This class number is the title of a column that I want to select in another table (table2). I want to use this subquery as a mechanism to select this column. Can this be done?
I know this may be bad design but I am just wondering if this sort of thing is possible in MS Access SQL. I know it is not as powerful as MySQL.
The only way you're ever going to do this is using VBA to write the SQL, which really isn't a bad thing. I'd do it sort of like this:
Dim db as Database
Dim rec as Recordset
Dim MyVar = String '(or whatever the datatype is for ClassNumber in Table1)
Dim sSQL as String
Dim qdfNew As DAO.QueryDef
Dim qryLoop As QueryDef
Set db = CurrentDB
Set rec = db.OpenRecordset("SELECT ClassNumber from Table1")
MyVar = rec(0)
'First check to see if the query already exists. If so, delete it.
For Each qryLoop In CurrentDb.QueryDefs
If qryLoop.Name = MyVar Then
DoCmd.DeleteObject acQuery, MyVar
End If
Next
'This will select only the field that relates to the ClassNumber above
sSQL = "SELECT " & MyVar & " FROM Table2"
Set qdfNew = db.CreateQueryDef("MyNewQuery", sSQL)
Then, MyNewQuery is a permanent query in your database which can be used in other queries.
There might be a better option, but I can only think of:
Heinous case statement to choose the right column.
Using VBA.
Correcting the table design, turning the field name into a value
that can be joined upon.

sql statement to return all fields from all tables

select * from *
yes this is what I want I want to return all fields from all table in my ACCESS database regardless the fields names or the tables names!!!
for example if I have table1 and table2 as tables in my database access file
what I want is to generate this statement
select * from table1,table2
from sql query which run fine in access query
but again I don't know the name of the tables which in the access file.
is there a way for that?
This query will list all the table names
SELECT MSysObjects.Name
FROM MSysObjects
WHERE (((MSysObjects.Type)=6))
You can also use this bit of code to go through every table and list every field
Public Sub List_fields_in_tables()
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
For Each tdf In CurrentDb.TableDefs
For Each fld In tdf.Fields
Debug.Print "Table='" & tdf.name & "' Field='" & fld.name & "'"
Next fld
Next tdf
End Sub
Hope this helps
To achieve this you should use from Master database in SQL Server
Master database contains information about all columns of all of your databases. also there is catalog view to gain information about an exact database.
Just use Master stored procedures