Friday, January 26, 2018

SSMS (SQL Server Management Studio) I ain't got time for all that...

Command-line SQL has always been easy, I have been writing sql in DOS like this from way back in
the day. The following examples are meant to be in DOS or Powershell; and funny, sorry if they aren't funny.

This example is in DOS
sqlcmd -Q "SELECT * FROM [Jokes].[Mom] WHERE type = 'Fat'" -o FatMomJokes.txt


But when you mix Powershell and SQL you can be more productive and creative.

$Jokes = Invoke-Sqlcmd -Query "Select * From [Jokes].[Mom]"
ForEach ($joke in $Jokes){
  if($joke.Name -eq "your mom"){
    Write-Host "$joke.Tell"
    $script = "Update [Jokes].[Mom] Set Status = 'Told' Where id = $dumb.Id"
    Invoke-Sqlcmd -Query $script
  }
}

$script = "INSERT INTO [Jokes].[Mom] ([Description],[InsertDateTime])
VALUES ('All night long', '$(Get-Date -Format "yyyy-MM-dd HH:mm:")')"
Invoke-Sqlcmd -Query $script -ServerInstance "localhost\sqlexpress"

No comments: