Write-datatable - does it finish writing before it returns? (SP run after doesn't seem to see the data)

I'm trying to load data then run a stored procedure when it finishes.  So my code looks like:

(using runspaces, fwiw, but this is within the same runspace)

    Write-DataTable -ServerInstance $server_repository -Database $database_repository -TableName myinsert -Data $LogResults

    invoke-sqlcmd2 -serverinstance $server_repository -database $database_repository `
    -query "EXEC myinsert_Blacklist_Removal @ComputerName = '$computername', @EventLog = '$eventlog'"

My blacklist code deletes from the table, inserts remaining records into the master table, then deletes from the staging table.  Further complicating matters is the use of full-text, which speeds up the delete based off the blacklist.

However, I'm seeing where the data isn't touched.  So I added this to the beginning of the stored procedure:

SELECT @max_recordnumber = MAX(RecordNumber)
FROM myinsert
WHERE eventlog = @EventLog
AND ComputerName = @ComputerName

INSERT INTO myinsert_Blacklist_Calls (computername, eventlog, max_recordnumber) VALUES (@ComputerName, @EventLog, @max_recordnumber)

And to my surprise, I have NULLs for the max_recordnumber, even though I can see the record in the staging table.

Which means, I think, that stored procedure is called before the BulkInsert is finished.  Which makes no sense. Maybe it hasn't finished committing from disk before the SP runs?  *shrug*  I'm really stumped and it's torking me off good.

Any help greatly appreciated.

Michael
April 30th, 2015 3:54pm

What bulk insert?

You need to post issues with SQL in the SQLServer forum.  None of this is done by Powershell so it is not a scripting issue.

 

Free Windows Admin Tool Kit Click here and download it now
April 30th, 2015 5:33pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics