Showing posts with label SQLCMD. Show all posts
Showing posts with label SQLCMD. Show all posts

Thursday, June 25, 2015

Can you use a Here-Strings query to retrieve data using PowerShell?

Yes you can.
You need to understand a few items. Let me go over them.

What is meant by here-strings?
http://hodentekhelp.blogspot.com/2015/06/what-is-here-string-in-windows.html

Using a variable in a here-strings:
http://hodentekhelp.blogspot.com/2015/06/can-you-have-variable-in-powershell.html

How to query a SQL Server with SQLCMD?
http://hodentekmsss.blogspot.com/2015/06/querying-sql-server-database-with-sqlcmd.html

How do you query a database using PowerShell?
http://hodentekmsss.blogspot.com/2015/06/how-do-you-query-database-using.html
If you review the above articles then you can put together all the information in running a query as shown where the query to the database is in here-strings.

That is all folks!

Here-strings come in very handy when the query contains single quote, double quotes and other such ugly characters which a mere string representation cannot handle.

Monday, June 22, 2015

Can you run queries on multiple SQL Server Instances at the same time?

It is possible to query multiple SQL Servers using the SQLCMD mode in SQL Server Management Studio. You start getting connected to atleast on instance.

In the following example, there are two instances of SQL Server:
1. SQL Server 2012 with named instance 'RegencyPark'
2. SQL Server 2016 CTP2 with named instance 'May2015'

I start with SSMS 2012 and run the following statements after enabling SQLCMD Mode in SSMS 2012.
You will get connected to the two named instances when you query them as shown here (in a query window in SSMS 2012):

:Connect .\May2015
Select @@version
Use MSDB
go
Select * from dbo.msdb_version
GO

:Connect .\RegencyPark
Select @@version
Use AdventureWorks2012
go
Select * from Person.person
go


You get the following response from the server.