Ruby Rails challenge

I overheard a conversation the other day in the office about some of my colleagues all challenging themselves to do an Iron Man race as they always wanted to do it and they thought it would be fun to do it as well.

This got me thinking about my own dreams and challenges I could set for myself. Whilst an Iron Man competition sounds fun it doesn’t appeal to me and I have thought about what I could do in a short period of time to both experience something new and to learn and grow.

I have always had a niggling desire to create apps and my existing knowledge of  C#, Python, SQL and PowerShell has been primarily geared towards solving administrative tasks rather than creating programs. After a short deliberation in my head I have decided that I will start knocking out a few apps over the next few months and hopefully share any knowledge I have picked up  over that time.

A quick Google around showed that there were two ways to go to fulfil my goal and those were Ruby & Rails or Python & Django. I know there are pros and cons to both options and didn’t want to get stuck in that malaise so I have chosen the Ruby & Rails route.

The two reasons for choosing Ruby & Rails is because its shiny and new for me and there appears to be a growing community of users which I would like to be a part of and the other reason is there is a meetup local to myself for Ruby programmers which gives me less than a month to learn the basics of Ruby programming and to produce at least one rudimentary app.

I have signed up to onemonth.com for their Rails program and will do the Ruby on Rails Tutorial by Michael Hartl on SafaribookOnline.com which should give me a firm foundation and hopefully I will be able to contribute in that meetup.

I will post regular updates about my progress and hope this public announcement will keep me accountable. Also, I probably be posting a whole load of Ruby & Rails related blogs and hope that everyone finds them enjoyable and informative.

Now I want to turn the tables and ask all my readers what they will be doing over the next few months to learn, grow and most importantly have fun.

 

 

Generate random numbers in PowerShell with Get-Random

Generate random numbers in PowerShell with Get-Random

Have you ever wanted to generate random numbers for passwords, test data…. and wondered how you could easily generate them?

Well I was confronted by the same situation on one of our servers where I needed some random numbers for inputting wait times to test an application but didn’t have my usual tool of choice ( MS Excel ) available to me.

I remembered the Get-Random command from memory but used the Get-Help command to allow me to use the correct syntax.

Executing Get-Random will produce a random number.

get-random
get-random

 

This was quite a good start but then I needed only 4 digit figures and I then had to use the minimum and maximum parameters.

Get-Random -Maximum 9999 -Minimum 1000

2

The above command solved that problem for me and I think it reflects how easily readable the PowerShell language is.

The last scenario I had was producing random numbers for some temporary passwords. I looked at the help for Get-Command and noticed the inputobject and count parameters which I thought would fulfil my need.

Get-Random -InputObject (10000..99999) -Count 3

3

The above command shows the range of values which I gave and the number of random numbers I needed in the Count parameter.

Hopefully the above will help you with your random number needs. Please add your feedback or other solutions in the comments below.

 

Statistical Aggregate functions in SQL Server

Have you ever wondered how to do program or perform simple statistical function within SQL Server? Well the kind people at Microsoft have enabled us to all to save time by giving us a few inbuilt functions which save us from having to solve them programmatically.
I have used the unit Price column in the SalesOrderDetail table within the Adventureworks database to illustrate the in built functions

 

SELECT --UnitPrice,
COUNT(UnitPrice) AS 'Count of UnitPrice',
AVG(UnitPrice) AS 'Average UnitPrice',
MIN(UnitPrice) AS 'Minimum UnitPrice',
MAX(UnitPrice) AS 'Maximum UnitPrice',
SUM(UnitPrice) AS 'Total UnitPrice',
VAR(UnitPrice) AS 'Variance of UnitPrice',
STDEV(UnitPrice) AS 'Standard deviation of UnitPrice'
FROM [Sales].[SalesOrderDetail] 

1
The COUNT function counts the number of values within the column.
The AVG function returns the average unit price column.
The MIN function returns the smallest value within the column.
The MAX function returns the largest value within the column.
The SUM function returns the sum total of all the values within the column.
The VAR function returns the variance of all the values within the column.
The STDEV function returns the standard of all the values within the column.

 

I am sure that I have probably missed out a few functions so please let me know via the comments below.

SQL Cursor to Kill all connections to a database

Have you ever tried to restore over a database but found that all attempts are being blocked by an annoying SPID? Or had hundreds of orphaned SPIDs running crazy on your instance?

Well I have the solution for you with the below script which uses a cursor to kill all connections to a database.

DECLARE @spid varchar(10)</pre>
DECLARE kill_spid CURSOR fast_forward FOR
SELECT SPID FROM sys.sysprocesses WHERE DB_NAME(dbid) = 'AdventureWorks2012' AND spid > 50
OPEN kill_spid
FETCH NEXT FROM kill_spid INTO @spid
while @@FETCH_STATUS = 0
BEGIN EXEC ('Kill ' + @Spid)
FETCH NEXT
FROM kill_spid INTO @spid
END
CLOSE kill_spid
DEALLOCATE kill_spid

Make sure that you change AdventureWorks2012 for your database name and double check that you have the correct database name as I have seen it when people put the wrong database name in and its never a pretty sight.

 

SQL Cursors

Cursors are a way of manipulating data and interacting with them one at a time. They have a bad reputation within the SQL world as they go against the SET based logic and they can have a very high performance cost. Where possible you should ask yourself whether you could avoid using a Cursor. This is because of the performance advantages a SET based solution has and that Cursor problems only increase when the tasks are scaled up.

The five general steps of a cursor are:

  1. Declaration of the cursor
  2. Opening the cursor
  3. Fetching and manipulating the data
  4. Closing the cursor
  5. Deallocating the cursor

A simple example of a Cursor is below.

Declare @Databases varchar(50)</pre>
Declare DatabasesOnIntance CURSOR READ_ONLY FOR SELECT name FROM sys.databases order by name
Open DatabasesOnIntance
Fetch next from DatabasesOnIntance into @Databases
While @@FETCH_STATUS = 0
Begin
Print @Databases
Fetch next from DatabasesOnIntance into @Databases
End
Close DatabasesOnIntance
Deallocate DatabasesOnIntance
Cursor Results
Cursor Results

As you can see the Cursor I have created has printed each database on the instance

WHILE, BREAK, and CONTINUE Statements

Have you ever wondered how to create a loop in SQL? Or wondered how to break and escape a loop?

Well I am going to briefly introduce you to the WHILE, BREAK, and CONTINUE Statements which will satisfy your curiosity

The first command I will introduce you to is WHILE

DECLARE @i int = 1;
WHILE @i < = 5

BEGIN

PRINT @i;

SET @i = @i + 1;

END

Loop

Loop

As you can see the WHILE statement will force the loop to continue until we reach 5. This is very useful if you need to batch process tasks and limit the amount done within each batch.

The next command to learn is CONTINUE. This command forces you to go back to the beginning of the loop.

 

DECLARE @i int = 1;
WHILE @i < = 5

BEGIN

PRINT @i;

SET @i = @i + 1;

CONTINUE; -- This will cause the WHILE to loop back

PRINT 'You wont see this due to the CONTINUE commands cleverness.';

END
Loop
Loop

Once the WHILE command is satisfied the CONTINUE command will allow the loop to complete.

The final command related to loops is the BREAK command.

 

DECLARE @i int = 1;
WHILE @i < = 5

BEGIN

PRINT @i;

SET @i = @i + 1;

BREAK; -- Force the WHILE loop to terminate

PRINT 'You wont see this due to the BREAK commands cleverness..';

END

 

Loop
Loop

As you can see from the example when the BREAK is encountered the loop is broken and it only ever prints 1.

In the real world many developers try not to use the BREAK and CONTINUE commands as they can be easily avoided in code and many people find it makes code less readable and unnecessarily complex.

 

 

Learn about the WAITFOR TIME & WAITFOR DELAY

Have you ever wanted to pause a command for a short period or wanted to run a transaction at a specific time.

 

Well I am going to quickly show you how to do them both using the WAITFOR command.

SELECT GETDATE()
WAITFOR DELAY '00:00:10';
BEGIN
SELECT GETDATE()
END;
Date time
Date time

The above code shows you how to delay two print current date and time commands by ten seconds. It can be easily modified for any time such as 43 minutes, 43 hours….

SELECT GETDATE()
WAITFOR TIME '12:22:00';

BEGIN

SELECT GETDATE()

END;
Date time
Date time

The above command will cause the transaction to wait until that time before executing the command. The time can be modified at your pleasure for whatever time you require.

The two processes do carry a processor overhead as the transaction will be running until it’s completed. Also, these commands can usually be replaced by an appropriately timed SQL Agent job which reduces the process overhead and makes the administration of it far easier.

Declaring Variables and Retrieving Variables

A variable can be best described as being a place holder for information which you then fill in with relevant information which you will want to retrieve later.

The below query shows the result from the CURRENT_TIMESTAMP which returns the current date and time.

PRINT CURRENT_TIMESTAMP

Jan 15 2015 11:44AM

1

The CURRENT_TIMESTAMP result can be made into a variable which can be retrieved later.

DECLARE @ThisIsTheCurrentDateandTime Datetime = CURRENT_TIMESTAMP

PRINT @ThisIsTheCurrentDateandTime

2

The DECLARE command creates the variable, assigns the datatype and the information I would like to put into the variable.

After I have created the variable I then retrieve it using the PRINT command to show the value stored.

TSQL Challenge on BeyondRelational.COM

I was asked by one of my junior colleagues to help him with a puzzle he had seen on BeyondRelational.COM which I thought would be a nice challenge. I liked the premise of the scenario and I always liked a challenge decided to show my colleague how I would approach and resolve this problem.

I was a little annoyed that the example code to create the test data didn’t work so did a quick fix which I have posted below.

</pre>
CREATE TABLE Firstchallenge(
EmployeeID INT IDENTITY,
EmployeeName VARCHAR(15),
Department VARCHAR(15),
Salary NUMERIC(16,2)
)

INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('T Cook','Finance', 40000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('D Michael','Finance', 25000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('A Smith','Finance', 25000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('D Adams','Finance', 15000)

INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('M Williams','IT', 80000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('D Jones','IT', 40000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('J Miller','IT', 50000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('L Lewis','IT', 50000)

INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('A Anderson','Back-Office', 25000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('S Martin','Back-Office', 15000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('J Garcia','Back-Office', 15000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('T Clerk','Back-Office', 10000)
<pre>

I told him that it would be quite simple to get the Ranking by using the RANK command and Partitioning the data by Department. However, I know you can’t filter by rankings from past experience but I knew if I put it into a subquery I could then filter the data which I have done below.


SELECT [EmployeeID]
 ,[EmployeeName]
 ,[Department]
 ,[Salary]
 FROM
(
SELECT [EmployeeID]
 ,[EmployeeName]
 ,[Department]
 ,[Salary]
 ,RANK() OVER (PARTITION BY [Department]ORDER BY [Salary] DESC) AS 'Salary Rank'
 FROM [TESTDB].[dbo].[Firstchallenge]
 ) A
WHERE [Salary Rank] = 2

I know that I could improve the query by using a CTE but I was happy that I was able to to get the result required and will probably put the CTE query in a future update of this article.

1

IF ELSE Control of flow statements

The IF ELSE statements are one of the most frequently used statement within SQL and control of

flow statements are one of the core statements within any programing language. Once you have

understood the concept you can easily make powerful scripts.

An IF statement is a check to see whether a condition is TRUE and is used in hundreds of different

scenarios such as checking whether an object exists, checking data for a value…… I always liken the

IF statement to checking your fridge for your favourite meal and if its there you eat otherwise you

check out all your other options.

The below is a simple script which tests to see whether I have the AdventureWorks2012 database.


IF EXISTS (SELECT * FROM sys.databases

WHERE name = 'AdventureWorks2012' )

PRINT 'AdventureWorks2012 is installed'

The result you will get is below.

AdventureWorks2012 is installed

1

The query checks whether the AdventureWorks2012 returns a value or is TRUE from my query and

then runs the PRINT command to confirm the value was returned as I expected.

You can make the IF statement more powerful by adding the ELSE statement. The ELSE Statement is

used as we often don’t just want to check if a statement is TRUE but also want to produce an action if its false.


DECLARE @TestValue int

SET @TestValue = 1

IF @TestValue >1

BEGIN

PRINT 'The Test value is greater than 1'

END

ELSE

PRINT 'The Test value is less than or equal to 1'

The result you will get is below.

The Test value is less than or equal to 1

2