sql mass insert. Need help - sql

Hi I doing a mass insert of records from this text file using the script below. I having a problem here to set the "c:\" to a parameter I pass in from a vb .net application? I do not want to define a fixed path for that. IS that possible?
INSERT INTO tblContacts
SELECT *
FROM [Text;Database=C:\;HDR=Yes].[Import.csv]

The best way would be to use any scripting language (or maybe VBA) to parse the SQL file, change the path, write it again, then batch-run the changed SQL file.

Write the path as %TEMP%\Import.csv. This is a special shortcut to get to the temp folder. You will have access to write to the user's temp folder.

Related

How can I link values in a text file to values in a SQL Server table?

UPDATE 2
Essentially, I'd like to pass the dataTable into SQL Server, but not actually "create it", just like a temporary thing using:
cmd.CommandText = "SELECT *
FROM table1
INNER JOIN dataTable
ON sample.name = dataTable.name"
How can I pass the dataTable from vb.net (.NET 2.0) to SQL Server in a similar fashion?
UPDATE 1
So I'm thinking maybe passing the data from the text file into a datatable and using that to compare against the SQL Server table? How would I go about doing that if at all possible?
ORIGINAL POST
I have a table in SQL Server 2012 (i.e., dbo.sample1) within the table there is a column that contains names (i.e., abc01, abc02, abc03, hijk01, hijk02...)
I ran some vb code to extract certain file names without extension from a directory on my machine (i.e., abc01, abc02...) that met certain conditions, these file names were saved on separate lines within a text file.
Is there any easier convenient way of linking ONLY the names on my text file to the ones on my table so as not to show any rows that are not on the text file? I figured I can sit and plug in a bunch of WHERE name = 'abc01'... but didn't really want to site there and do that for all of the names I have. But I'm not sure is this might even work correctly as I need to do an INNER JOIN on of 2 tables in the DB to the values in the text file.
If this is a long problem, then please point me in the right direction and I can research and move forward with it, but any help is greatly appreciated, thanks!
If the list of filenames is reasonable you can us the IN() clause:
SELECT * from XXX WHERE name IN ('abc01', 'abc02', 'abc03',...)
If the list is too long look into bulk copy up to a temp table to use with a JOIN.

Save PDF-File into BLOB-Column Oracle DB with INSERT statement

I have to save a PDF-File into a BLOB-Column from a Oracle DB.
I can't use Java and have to use an INSERT-Statement.
The only solutions I've found while searching were very complex.
Is there an easy solution like: INSERT INTO (BLOB_COLUMN) VALUES(BLOBPDF("myPDF.pdf") or something like that?
I would suggest that you use a stored procedure in Oracle where you pass the path to your PDF file and calling the stored procedure does the insert.
Look at the last two sample example here.
If the load is "one-shot" you can use SQLDeveloper.
Otherwise you can use sqlloader (http://docs.oracle.com/cd/B19306_01/server.102/b14215/ldr_params.htm) that is designed for this type of operations

Inserting a single row in SQL, but loading the contents from a file

I can INSERT a row into SQL like this:
INSERT INTO MyTable VALUES (0, 'This is some text...');
However, what if I wanted This is some text... to be the contents of C:\SomeFile.txt. Is there a method in Oracle that makes this possible?
I dug through the Docs a bit and found the LOAD FILE method, however it appears to be for bulk loading data. For example, it wants a FIELDS TERMINATED BY parameter and what not. I want to simply INSERT a single row and set a single column to by the contents of a file on the local disk.
You should never be reading files from the DATABASE SERVER's file system to insert into the db.
Why do you want to do this? You really should read the file in your application code, then insert the string or binary data through standard SQL.
If you really must do this you will need to use the oracle UTL_FILE function. and write some PL/SQL to store in a variable, then insert.
My first time answering a question, so forgive me.
If you're using PHP (which you may well not be, but this is what I know), then you could do something like this:
File: "importantfile.php"
$variable 1 = "0";
$variable 2 = "This is some text...";
File that inserts text: "index.php"
require "importantfile.php";
$query = mysql_query("INSERT INTO MyTable VALUES ('$variable1', '$variable2')");
I hope this helps you in some way :)
To do this I think you would have to use PL/SQL. This is technically an "advanced SQL".
search up a bit about PL/SQL. You can copy your SQL directly into PL/SQL and use the same database.

tsql : outputting each record to their own text file

is there a simple way to just output each record in a select statement to write to its own file?
for example, if you have the tsql query in sql server 2005,
select top 10 items, names + ':' + address from book
and you ended up with 10 text files with the individual name and addresses in each file.
is there a way to do this without writing an extensive spWriteStringToFile procedure? I'm hoping there is some kind of output setting or something in the select statement.
thanks in advance
SQL returns the result set first, there's no opportunity in there for writing records to specific files until afterwards.
Being SQL Server 2005, it's possible you could use a SQLCLR (.NET 2.0 code) function in a SQL statement without having to make a separate application.
In SSMS, you can do a results to file, but that wouldnt split each record out into its own file. I pretty sure you cannot do this out of the box, so it sounds like you will be rolling your own solution.
You'd do this in some client, be it Java, VBA or SSIS typically.

Creating a SQL table from a xls (Excel) file

I'm trying to convert an Excel document into a table in SQL 2005. I found the link below and am wondering if it looks like a solution. If so, what would the #excel_full_file_name syntax be and where would the path be relative to?
http://www.siccolo.com/Articles/SQLScripts/how-to-create-sql-to-convert-Excel_to_table.html
You can use the BULK INSERT T-SQL command if you just want a pure sql solution. You have to save the file as csv/text first.
BULK
INSERT YourDestinationTable
FROM 'D:\YourFile.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
Alternatively, you can try OPENROWEST - again , a pure T-SQL solution.
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;DATABASE=D:\YourExcelFile.xls', 'Select * from YourExcelFile')
It really depends on how much control and flexibility you want, the SSIS route will have benefits over these methods.
Glancing over the code, I would expect it to be the full path name of the excel document:
For example: c:\path\to\my\excel\document.xls
I haven't installed the procedure though or run it, so I could be wrong - but that's what it appears to be at first glance.
I would suggest using an SSIS/DTS Package, to convert. It's much easier.
SSIS Excel example
** note that this example is using the wizard. you can schedule the SSIS/DTS package as a job to run, on your SQL box.
This example copies data from SQL to Excel.
But it is just a matter of swapping the OleDb providers to get it to work in the opposite direction.