So I need to update an existing Table with data from a text file each time the scripts runs. My current code drops the table and creates a new one each time it is ran which cause about a 10 second delay which I need avoid. I would like to instead Update the data without re-creating the table (hopefully alleviating the delay?). I understand the UPDATE
or MERGE
command can be used for this but haven't been able to figure out the syntax.
Function CreateAnalystTable($location, $file, $extension, $server, $database)
{
$full = $location + $file + $extension
$all = Get-Content $full
$columns = $all[0]
$columns = $columns.Replace(";","] VARCHAR(255), [")
$table = "CREATE TABLE " + $file + "([" + $columns + "] VARCHAR(255))"
$connection = New-Object System.Data.SqlClient.SqlConnection
$buildTable = New-Object System.Data.SqlClient.SqlCommand
$insertData = New-Object System.Data.SqlClient.SqlCommand
$connection.ConnectionString = "Data Source=" + $server + ";Database=" + $database + ";integrated security=true"
$buildTable.CommandText = $table
$buildTable.Connection = $connection
## Added to function
$x = 0
$insertData.CommandText = "EXECUTE stp_CommaBulkInsert @1,@2"
$insertData.Parameters.Add("@1", $full)
$insertData.Parameters.Add("@2", $file)
$insertData.Connection = $connection
$connection.Open()
$buildTable.ExecuteNonQuery()
$connection.Close()
## Added to function
$x = 1
if ($x = 1)
{
$connection.Open()
$insertData.ExecuteNonQuery()
$connection.Close()
}
}
CreateAnalystTable -location "PATH" -file "FILE" -extension ".txt" -server "SERVER" -database "DB"
SQL:
CREATE PROCEDURE stp_CommaBulkInsert
@file NVARCHAR(250), @table NVARCHAR(250)
AS
BEGIN
DECLARE @f NVARCHAR(250), @t NVARCHAR(250), @s NVARCHAR(MAX)
SET @f = @file
SET @t = @table
SET @s = N'BULK INSERT ' + @t + '
FROM ''' + @f + '''
WITH (
FIELDTERMINATOR = '';''
,ROWTERMINATOR = ''0x0a''
,FIRSTROW=2
)'
EXEC sp_executesql @s
END
Any help is appreciated