Sunday, 5 June 2016


SQLCMD and the power of the little r

I would have to say that one of my favorite new utilities that shipped with SQL Server 2005 has been the SQLCMD utility.  I am going to demonstrate the use of include files. The following is the complete call syntax for SQLCMD.


sqlcmd 
[{ { -U login_id [ -P password ] } | –E trusted connection }] 
[ -z new password ] [ -Z new password and exit]
[ -S server_name [ \ instance_name ] ] [ -H wksta_name ] [ -d db_name ]
[ -l login time_out ] [ -A dedicated admin connection ] 
[ -i input_file ] [ -o output_file ]
[ -f < codepage > | i: < codepage > [ < , o: < codepage > ] ]
[ -u unicode output ] [ -r [ 0 | 1 ] msgs to stderr ] 
[ -R use client regional settings ]
[ -q "cmdline query" ] [ -Q "cmdline query" and exit ] 
[ -e echo input ] [ -t query time_out ] 
[ -I enable Quoted Identifiers ] 
[ -v var = "value"...] [ -x disable variable substitution ]
[ -h headers ][ -s col_separator ] [ -w column_width ] 
[ -W remove trailing spaces ]
[ -k [ 1 | 2 ] remove[replace] control characters ] 
[ -y display_width ] [-Y display_width ]
[ -b on error batch abort ] [ -V severitylevel ] [ -m error_level ] 
[ -a packet_size ][ -c cmd_end ] 
[ -L [ c ] list servers[clean output] ] 
[ -p [ 1 ] print statistics[colon format] ] 
[ -X [ 1 ] ] disable commands, startup script, enviroment variables [and exit] 
[ -? show syntax summary ]
:r is a SQLCMD command that parses additional Transact-SQL statements and sqlcmd commands from the file specified by <filename> into the statement cache.
A simple example:
In SQLQuery1.sql I have the following:
SELECT TOP 5 ProductID,Name,ProductNumber,MakeFlag FROM AdventureWorks.Production.Product
In SQLQuery2.sql I can then reference SQL file 1 as follows:
:r “c:\SQLQuery1.sql”
My results are:
ProductID    Name    ProductNumber    MakeFlag
1    Adjustable Race    AR-5381    0
2    Bearing Ball    BA-8327    0
3    BB Ball Bearing    BE-2349    1
4    Headset Ball Bearings    BE-2908    0
316    Blade    BL-2036    1

This may not seem like a big deal, but consider the following scenario. I have a large bat file of SQL that I process in jobs or in SSIS packages. This bat file is doing and setting the same variables over and over again, so I can create include files that declare my variables, and another include file that sets them.
My first file “c:\DeclareVariables.sql” will always change me to the correct DB, set no count on and declare my variables. This becomes  good anchor file for any pre-processing that I may want to do.
USE [AdventureWorks]
GO
SET NOCOUNT ON
GO
DECLARE @AccountingStartDate smalldatetime;
DECLARE @AccountingEndDate smalldatetime;
DECLARE @DocumentStatusText varchar(400);
DECLARE @Status tinyint;
“c:\SetVariables.sql” is where is set my variables. You notice that the SQL variable @Status is set to a SQLCMD variable of the same name “Status”. This allows me to control that from my batch sql file. I am trying to eliminate any hard coded references in my global files.
SET @Status = $(Status);
SET @AccountingStartDate = (SELECT AdventureWorks.dbo.ufnGetAccountingStartDate());
SET @AccountingEndDate = (SELECT AdventureWorks.dbo.ufnGetAccountingEndDate());
SET @DocumentStatusText = (SELECT AdventureWorks.dbo.ufnGetDocumentStatusText(@Status));
And finally the batch file “c:\SQLBatch.sql”. The first two lines set my include files to variables. This allows for a consistent naming through all my files and allows me to change the files without having to change the batch code. This is good for testing and portability. The next line is where I set the SQLCMD “Status” variable. This variable is then set in the SetVariables.sql file to the TSQL variable @Status that is passed to the function ufnGetDocumentStatusText.
:setvar DeclareVariablesScript "c:\DeclareVariables.sql"
:setvar SetVariablesScript "c:\SetVariables.sql"
 
-- Set Local variable Status (Used in SetVariablesScript)
:setvar Status 1
 
-- Declare Global Script Variables
:r $(DeclareVariablesScript)
-- Set Global Script Variables
:r $(SetVariablesScript)
 
SELECT * FROM AdventureWorks.Sales.SalesOrderHeader soh
WHERE ((soh.OrderDate > = @AccountingStartDate) AND (soh.OrderDate <= @AccountingEndDate));
GO
Now all I have to do it execute the file “c:\SQLBatch.sql”. If you are familiar with ASP include files this should be easy to understand.

Thursday, 17 September 2015

How can I switch themes in Visual Studio 2012 ?

In Visual Studio 2012, open the Options dialog (Tools -> Options). Under Environment -> General, the first setting is "Color theme." You can use this to switch between Light and Dark.
The shell theme is distinct from the editor theme--you can use any editor fonts and colors settings with either shell theme.
O hai!
There is also a Color Theme Editor extension that can be used to create new themes.

Thursday, 12 March 2015

Removing HTTPS binding affects other sites.

You may receive the below error message while removing https binding for domain in IIS.

Removing HTTPS binding may affect the other sites

Solution: If you remove the https binding for the site through IIS then the SSL certificate will also be removed for another domain to which the certificate is assigned.

The only way is to remove the SSL bindings from the applicationhost.config file. 

# Login to the server through RDP.
# Go to path C:/windows/system32/inetsrv/config
# Open the applicationhost.config file
# Search for domain SSL binding which you want to remove.
# Delete that complete line of the SSL binding and save the file.

You will see that the SSL binding for the domain has been removed without affecting other domains.

Note: Make sure you take the backup of the applicationhost.config file before making changes 

Wednesday, 26 November 2014

AWS EC2 VPC - RDS Error: The semaphore timeout period has expired

This issue is caused by TCP offloading on some Windows EC2 instances which connect to SQL Server DB instances. We need to disable TCP Offloading on your EC2 Instance(s) and perform a reboot to resolve the issue. 

The following aligns with Microsoft's guidance as outlined in the KB article: http://support.microsoft.com/kb/942861

  • Bring up the "Network Adapters" by opening "ncpa.cpl" (type at a Run prompt or in the Start Menu's Search box)
  • Right click the "Local Area Connection" and select "Properties"
  • Click the "Configure" button
  • Select the "Advanced Tab"
  • Select the following five "Properties" and set the "Value" to "Disabled" (See attached screenshot highlights)
                * IPv4 Checksum Offload
                * Large Receive Offload (IPv4)
                * Large Send Offload Version 2 (IPv4)
                * TCP Checksum Offload (IPv4)
                * UDP Checksum Offload (IPv4)
  • Click "OK" to close the dialog box, then close the "Network Adapters" window
  • Run the following script from a command prompt to set global TCP offload settings:

                C:\> netsh int ip set global taskoffload=disabled
                C:\> netsh int tcp set global chimney=disabled
                C:\> netsh int tcp set global rss=disabled
                C:\> netsh int tcp set global netdma=disabled

Alternative to .NET Reflector

For those of you who miss .NET Reflector (since Redgate took down the free version in 2011), there is a pretty good open source alternative: ILSpy.

The tool is designed around the original Roy Osherove’s .NET Reflector and supports all its major features including intermediate language disassembly, reflection (browsing), decompilation to C#.

It is a pretty decent IL decompiler.

Tuesday, 16 September 2014

How to backup Amazon RDS MS SQL Server database instance and restore locally


We can't currently create a .bak file out of amazon rds. The way I handle this is to use the azure migration wizard which just happens to also work with amazon rds.
I spin up a EC2 instance with SQL Server and the migration wizard installed. I then use the migration tool to copy the RDS database to the ec2 instance.
Once that is done you can create a .bak file from the SQL Server running on the EC2 instance. its a pain but it works. if you have the bandwidth or your database is small you may be able to use the migration tool directly on your target machine.

Thursday, 13 February 2014

"Launchy"

I don’t know if this product is widely known here but it came up in conversation yesterday so I thought I’d share. In my opinion, it’s one of the most useful tools around…and it’s free. Spend a bit of time with it and see for yourselves.