Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. Invoke-Sqlcmd - Server $Server -Query 'SELECT Column FROM Database'
  2.  
  3. Column
  4. ------
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10.  
  11.  
  12. #Powershell takes the above results applies single quotes around the row value, adds a comma, then puts the next value in single quotes #on the same line and adds a comma, so on until the last value which gets no trailing comma.
  13.  
  14. Invoke-Sqlcmd -Server $Server -Query 'SELECT Column2 , Column3 FROM Database2
  15. WHERE Column3 IN ( '1','2','3','4','5' )
  16.  
  17.  
  18.  
  19. Things i've tried after a bit of reading.
  20.  
  21. $Result = Invoke-Sqlcmd -Server $Server -Query 'SELECT Column FROM Database;'
  22.  
  23. foreach($item in $Result){
  24. $item= "'"+$Result.Name+"'" }
  25.  
  26. Write-Host $item
  27. Results:
  28. '1 2 3 4 5'
  29.  
  30.  
  31. Another I tried:
  32.  
  33. $GetIDs = Invoke-Sqlcmd -Server Server -Query 'select column from database;'
  34. $SeperatedIDs = $GetIDs -join ','
  35. write-host = $SeperatedIDs
  36.  
  37. Results:
  38. ColumnName,ColumnName,ColumnName,etc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement