SQL Update Statement in Powershell Issue with Website Escape Charactor - sql

I am trying to Iterate through a CSV file to do an Update Query in SQL. The CSV has Websites, a nickname for a site, and a resource group. It is having issues reading the website because it contains / which I think is an escape charactor. Is there a way to format WHERE SITES = $($c.sites) so it doesnt throw the following error? Invoke-SQLcmd : Incorrect syntax near 'https:'. The sites look like https://fakesite.com/something
foreach($c in $csv){
$updatequery="
UPDATE [dbo].[Table]
SET SiteName = $($c.sitename), RSG_Name= $($c.Resource_Group)
WHERE SITES = $($c.sites)
GO"}
Invoke-SQLcmd -ServerInstance 'serverinstance' -query $updatequery -Database db```

Related

Invoke-Sqlcmd : Could not find stored procedure

I have a dump of a SQL database table which contains only data. It is one long list of INSERT statements. The file is about 10GB and when I try to import with Invoke-Sqlcmd or the SQL Server management studio it fails with the message "Not enough memory". Therefore I split the file into several smaller files of 250MB. All the lines are complete, so no half INSERT statements at the end or beginning of each file because of splitting the files.
When I use Powershell to import the data the first file imports without problems.
Invoke-Sqlcmd -ServerInstance myserver\instance -Database mydatabase -InputFile "C:\temp\files\dbo.Data.00.sql"
Whenever I try to import the next file I get the following error message.
Invoke-Sqlcmd : Could not find stored procedure 'I'.
At line:1 char:1
+ Invoke-Sqlcmd -ServerInstance myserver\instance -Database mydatabase -I ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionExceptio
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
It mentions the stored procedure could not be found but it are only INSERT statements. I also tried to specify the database name before the first INSERT statement but that does not change the result.
USE [mydatabase]
Any ideas what is going wrong here.
I managed to work around this issue by copying the dump file to a Linux host and use the following command to split the file into files of 250.000 lines each.
split -l 250000 dbo.Data.sql
There still was a problem with the split files. All files except for the first one contained NUL characters between each character.
I used the following solution to remove the NUL characters.
Removing "NUL" characters
By executing the following command for all split files except the first one.
tr < xab -d '\000' > xab.dbo.Data.sql
tr < xac -d '\000' > xac.dbo.Data.sql
etc...

How to query a remote database with a powershell command?

I've done some research and I found the command "Invoke-Sqlcmd". I believe this is the command I want to use, but not which arguments. I've tried multiple things, but keep getting "invoke-sqlcmd : Login failed for user 'Hello'". Here is some practice data:
"Forehead" is the name of the server I am executing the PowerShell query from
"Elbow" is the name of the server that is running the SQL database
"Kitten" is the name of the database on "Elbow"
"Fluid" is the name of the table I wish to query
"Hello" is the username to log into "Elbow"
"World" is the password to log into "Elbow"
"Timmy" is the username to log into "Kitten"
"Sticky" is the password to log into "Kitten"
"SELECT * FROM Fluid" is the query I wish to run
With this information, from "Forehead" how can I execute the above query on Kitten? Is there more information I need?
Thank you for any help!
You need to pass the username, password, sql server and database to Invoke-Sqlcmd. Then your query (nice namings... :-)) should work.
$ServerInstance = 'Elbow'
$Database = 'Kitten'
$Username = 'Timmy'
$Password = 'Sticky'
$Query = 'SELECT * FROM Fluid'
Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Username $Username -Password $Password -Query $Query

Unable to cast object of type 'System.DBNull' to type 'System.Type'

I have a PowerShell script trying to run a sql command using Invoke-Sqlcmd but I get the following exception:
Invoke-Sqlcmd : The pipeline has been stopped.
At D:\Scripts\powersehelscript.ps1:57 char:9
+ $rows = Invoke-Sqlcmd -Query "SELECT * from TableName where location is null;" ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], PipelineStoppedException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptComman
Invoke-Sqlcmd : Unable to cast object of type 'System.DBNull' to type
'System.Type'.
At D:\Scripts\MyScript.ps1:57 char:9
+ $rows = Invoke-Sqlcmd -Query "SELECT * from TableName where location is null;" ...
my sql command looks like this:
$rows = Invoke-Sqlcmd -Query "SELECT * from TableName where location is null;" -Database $Database -ServerInstance $SqlServer -Username $U -Password $P
It seems to be connecting fine to the Database and if I run the same query directly in the DB i get the results back.
From the exception I see "Unable to cast object of type 'System.DBNull' to type" but I have no clue why this casting issue is occurring
In case anyone reading this is looking for other conclusions, I had exact same issue of PowerShell same error and it grinding down to a single geo column. I was simply writing some code to grab the table and export once a week for easy user reference. The Add-Type solution didn't change my outcome. What worked for me was, in the SQL cmd, using CAST(mygeocol AS varchar(100)). Conveniently, in SQL & PS, that brought it back to a format like "POINT (-90.1234 30.1234)" and worked great. Hope it helps someone!

Replace Single Quotes in PowerShell Or Excel CSV

I'm wrapping up a script which gets software versions and puts them into a CSV, then back to powershell then finally to SQL. Some of the software names have single quotes in them which is not allowed for import to SQL. I'm trying to find and replace these single quotes with back ticks at some point before the final SQL export. My first instinct is to operate the find and replace function in excel via powershell to do the replace, but I'm not sure how to go about this or if this is the best way.
Any advice is greatly appreciated. I'm very new to all this.
Thanks
edit: Thanks for advice so far alroc. I'm working with the following for this piece:
##SQL PART##
$CSV = Import-CSV -path $UpdatePath\$($todaydate).csv
import-module sqlps
ForEach ($item in $CSV){
$CSVAppID = $($item.AppID)
$CSVAppName = $($item.AppName)
$CSVVersion = $($item.Version)
$SqlServer = "redacted"
$SqlDB = "redacted"
$SoftwareTable = "redacted"
Invoke-Sqlcmd -ServerInstance "$SQLServer" -Database "$SqlDB" -query "INSERT INTO dbo.ecca_sw_standard_2 (name, version, product_id) VALUES (N'$CSVAppName', N'$CSVVersion' , N'$CSVAppID')"
}
A few of the variable values contain single quotes in the strings, and powershell throws me an error when it gets to these few. "Invoke-Sqlcmd : Incorrect syntax near 'S'."
The answer is to create sub expressions in your string to replace ' with ''.
Invoke-Sqlcmd -ServerInstance "$SQLServer" -Database "$SqlDB" -query "INSERT INTO dbo.ecca_sw_standard_2 (name, version, product_id) VALUES (N'$($CSVAppName -replace "'", "''")', N'$($CSVVersion -replace "'", "''")' , N'$($CSVAppID -replace "'", "''")')"
This way when the string is expanded it will be appropriately escaped for SQL insertion.

Calling sproc on SQL Azure from Invoke-Sqlcmd

The form to hit a regular sql server:
$server = "192.168.1.1"
$myDatabase = "Adventures"
$myUser = "Joe"
$myPass = "BadPass"
Invoke-Sqlcmd -ServerInstance $server -Database $myDatabase -Username $myUser -Password $myPass
For Azure SQL http://www.connectionstrings.com/sql-azure/ tells me to bracket the servername as:
[plkahglkhjl].database.windows.net
but that gets me a error message that the server can not be found or was not accessible. It's behaving as if it were a firewall blocking but I have a open connection in SSMS without problem.
Is there another set of validation/authentication that PowerShell scripts have to deal with?
thx
ConnectionStrings.com is just indicating that you need to replace those fields with your own, not that you actually need brackets.
1) You do not need to bracket your server name
plkahglkhjl.database.windows.net
2) You do need to put the server name in the UserName
Joe#plkahglkhjl
More details here: Error connecting to SQL Azure Database