I have some programming experience but am brand new to SQL.
Basically I have about 300 terms that I want to search for in a single search.
What is the best way to store those terms in a way that I can iterate through them in a query? They're currently in an excel column and I'd prefer not to have to manually write each one in
SELECT * INTO EXCEL_IMPORT
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\Excel\Spreadsheet.xls; HDR=YES; IMEX=1',
'SELECT * FROM [Sheet1$]');
Will create a temporary table that you can run your queries against.
SELECT * FROM
SEARCHABLEDATABASE
WHERE column_of interest IN
( SELECT search_terms FROM EXCEL_IMPORT )
Or use the SQL Server import wizard
OR Simply run the select queries directly against the sheet.
EDIT: These two queries will match entries in a column of database to terms from a excel spreadsheet, providing they are exactly the same) you could TRIM them both to prevent differences in whitespace causing issue.
Related
I have a some data in an excel sheet in the form of a table. I want to run an sql query on that data. I am okay to use the data raw from the csv.
Is it possible to create a temporary view or table using that data within the query itself. I don't want to create a separate table for this excel data.
There are a bunch of answers for this already... Have you searched?
You want to do something like this;
Select *
into [temp_table$]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\spreadsheet.xls;HDR=YES;IMEX=1',
'SELECT * FROM [SHEET1$]')
Like in this question: Get Excel sheet into temp table using a script
For SQL Server 2008 and above you can use OPENROWSET
In simplest form it will look like this:
SELECT * FROM OPENROWSET(BULK 'data.csv', SINGLE_CLOB) AS DATA;
Just remember to specify full file path.
i had a problem .... i have and old excels files and i want to save them into SQL database ... MY Q is : if i want to do for example in .xls from (C16:C28) want to take it and put it into table1 in field NAMES .. how i can write it in query ?
note am using Microsoft SQL Server Management .. and try to get all my xls and import it into data base via sql query take from those files data and put it into my database ..
INSERT INTO [table1] (Names) VALUES ('&C16&') to ('&C28&')
is this true ? idk how to write it .. need help
When you use openrecordset you can specify a range [Sheet1$C16:C28] on the table in the query parameter
INSERT INTO [table1] (Names)
SELECT *
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\Foo.xls;HDR=NO',
'select * from [Sheet1$C16:C28]') AS t
I'm trying to do (basically) an automated row copy from one backend to another as the backend is being used but is not fully developed (some tables are done, others not; adding the completed data into the more advanced db at the end of the day).
I'd like to know what the syntax is for an SQL statement which:
INSERT INTO tblMyBetterTable * IN "C:\\path_to_db\db.accdb"
FROM tblMyTable IN "C:\\path_to_in_use_db\in-use-db.accdb"
The syntax is not correct but I've searched for a while and can't find out how.. I've seen how to import from one table to another etc, but not across backends. If it's not clear, I'd like to basically do a table copy of all rows in in-use-db.accdb from tblMyTable to the latest version of the backend db.accdb's table tblMyBetterTable.
I suggest you try DoCmd.TransferDatabase
For a query, you need something on the lines of:
SELECT * INTO NewTable
FROM [;DATABASE=Z:\Docs\Test.accdb].Table1
Or the other way round:
SELECT * INTO [;DATABASE=Z:\Docs\Test.accdb].NewTable
FROM Table1
To insert into an existing table:
INSERT INTO table1
SELECT *
FROM [;DATABASE=Z:\Docs\Test.accdb].Table1
Platform: SQL Server 2008
Language: TSQL
I have a number of queries that currently take the general form of (for simplicity sake)
-- Sample begin results
SELECT * from DB01.dbo.table UNION ALL
SELECT * from DB02.dbo.table UNION ALL --many other databases follow with same syntax
How can I modify these queries such that, when a new database comes into existence (named, say DB39C), I ensure that my queries already includes those new records?
--Sample end results
SELECT * from DB01.dbo.table UNION ALL
SELECT * from DB02.dbo.table UNION ALL
SELECT * from DB39C.dbo.table -- this was created as soon as a new database came into existence
I am looking to make sure programmatically, that this happens without my awareness as new databases are added quite regularly and I need the queries I rely on to keep pace.
You might want to have a look at using something like
SELECT name AS DATABASENAME
FROM master.dbo.sysdatabases
and creating dynamic queries
sys.databases (Transact-SQL)
I am trying to import a large CSV file into a MySQL database. I have loaded the entire file into one flat table. i can select the data that needs to go into separate tables using select statements, my question is how do i copy the results of those select queries to different tables. i would prefer to do it completely in SQL and not have to worry about using a scripting language.
INSERT
INTO new_table_1
SELECT *
FROM existing_table
WHERE condition_for_table_1;
INSERT
INTO new_table_2
SELECT *
FROM existing_table
WHERE condition_for_table_2;
INSERT INTO anothertable (list, of , column, names, to, give, values, for)
SELECT list, of, column, names, of, compatible, column, types
FROM bigimportedtable
WHERE possibly you want a predicate or maybe not;
The answer from Quassnoi was the one I was looking for. Please observe that if new_table_1 doesn't exist yet the "INSERT INTO" statement has to be replaced with a "CREATE TABLE" statement.