Pages

Saturday, 20 August 2011

How to draw a Polygon in SQL Server

In my previous post 'Drawing a circle in SQL Server (almost)' I showed how to plot 360 points to draw a circle. {Well} It looked like a circle, but to SQL Server, it was just a series of points.

We can use a LineString to draw a shape, but the most appropriate type is the Polygon.

The polygon is a closed [at least 3 sided] shape, i.e. the first point is the same as the same point. If it’s not, SQL will throw an exception. SQL Server will also throw an exception if you attempt to create a polygon with just two sides..

 

   1:  -- Drawing a polygon
   2:  declare @Sides float = 3
   3:  declare @count int = 0
   4:  declare @size int = 10
   5:  declare @firstpoint varchar(400)
   6:   
   7:  declare @sql varchar(8000);
   8:   
   9:   
  10:  set @sql =  'POLYGON((' 
  11:   
  12:  while(@count < @Sides)
  13:  begin
  14:   
  15:  set @sql = @sql + cast(SIN(RADIANS((CAST(@count AS FLOAT)
  16:              /CAST(@Sides AS FLOAT)) 
  17:              * 360)) * @size as varchar) 
  18:             + ' ' +
  19:              cast(COS(RADIANS((CAST(@count AS FLOAT)
  20:              /CAST(@Sides AS FLOAT))
  21:              * 360)) * @size as varchar) + ','
  22:   
  23:  if @count = 0
  24:  set @firstpoint  = cast(SIN(RADIANS((CAST(@count AS FLOAT)
  25:              /CAST(@Sides AS FLOAT)) 
  26:              * 360)) * @size as varchar) 
  27:             + ' ' +
  28:              cast(COS(RADIANS((CAST(@count AS FLOAT)
  29:              /CAST(@Sides AS FLOAT))
  30:              * 360)) * @size as varchar) 
  31:              
  32:  set @count = @count + 1
  33:   
  34:  end
  35:   
  36:  set @sql = @sql + @firstpoint + '))'
  37:   
  38:  print @sql
  39:  DECLARE @g geometry;
  40:  SET @g = geometry::STGeomFromText(@sql, 0);
  41:  select @g
  42:   



Simply change


   1:  declare @Sides float = 3

to be how ever many sides you need.  Simple.


Here are a few examples.


3456360


All scripts can be found on my SkyDrive using the link below.

SQL Server in the Evening (3rd Event)

Last night I did my first slot as a presenter.  I’d like to thank Gavin Payne, the event organiser for giving me the opportunity to speak.

So how I did go? Okay, I think.  It is very difficult to know how you go down until you get feedback (so if you attended, please feel free you email me your comments). 

The initial feedback was positive and incredibly useful (Thank-you Christian), its given me ideas of how to structure my next presentation… 

But back to the evening…

My presentation was on Geospatial data types in SQL Server 2008 (r2 Express) – to try and make my presentation more memorable I used props, balls for Points, string for LineString and Polygons –> I hoped that it would be different , not in code or PowerPoint would help the concepts stick..   

me_at_sql_in_the_evening If you look really closely you can see the string.. (Thanks again Christian – I took no pictures all evening).

After me, Vilosh (Smart Reporting Solutions) did a demo of his BI product.

Then the buffet,  although I feel the term buffet doesn’t do justice to food.  I was bowled over by the quality and quantity.  The Ramada did a great job.

Then Jen Stirrup presented iPad and PowerPivot- Mobile Business Intelligence in Action.  A really thought provoke presentation on the future of mobile BI and BI in general.  The iPad is a game changer and a great business tool.  Check her previous and next SQLBits sessions out.

Next up was Keith Burns presenting Denali CTP3 - An overview and update.  I was excited about CTP1 of Denali, but it was a bit of a non-starter, yes, it was a new engine and new SSMS, but the cool sexy features, Crescent, Column Store indexes, file table, Juneau etc etc were all missing, but now in CTP3 – they are all there!!!!!!

I feel like it’s time to get enthusiastic about Denali again.  Keith briefly mentioned Feature pack for Denali…Check out the goodies!!

One of the great things about going to User Groups is seeing wonderful presenters talk about subjects they love, but its a fantastic place to meet up with the twitterati – people you only know by cryptic code names are real flesh and blood people.  

I’d like to thank Gavin again for putting on a great evening and I’m really looking forward to the next one.

Friday, 19 August 2011

'Denali' Community Technology Preview 3 (CTP 3) Feature Pack

I was at Keith Burn’s presentation of ‘What’s new in Denali CTP3’ at SQL Server in the Evening (Gavin Payne’s User Group) last night and I heard about Denali Feature pack.

A small about of googling with Bing later, this popped up -  ‘Feature Pack pour SQL Server "Denali" CTP3’

I had a Homer Simpson moment ”Hmmm Feature Pack...”

Wednesday, 17 August 2011

How to write a SQL Server Management Studio Add-in for Denali (CTP3) (Part 1)


Writing a SSMS Add-in is not for the faint hearted or those with weak constitutions, but for those who persevere the rewards are small and fleeting, yet worth while.

This post is for people who know roughly what they are doing, so without any more non-sense, here we go.

You will need;
                       Visual Studio 2010 (I have Premium - I've not tried other editions)
                       SQL Server Denali CPT3 (Should work with CTP1 and CTP2 - but I've not tried)
                      
1) Start up VS2010

2) Create a new project, a Visual Studio Add-in, you should find this under 'Other Project Types \ Extensibility'  (You may not have this project type, do not panic! I will make this source available)

3) Give the project a sensible name - like MyAddin1 :-)

4) The Add-in Wizard should start, 'Click Next'

5) Pick you language of choice, I picked 'Create an Add-in  using C#' (C++/CLR and C++/ ATL are both greyed out for me) - Click Next.

6) Un-Select 'Microsoft Visual Studio 2010 Marcos' - Click Next.

7) Give your Add-in a sensible name and description. - Click Next.

8) Tick 'Yes, create a Tools menu Item...' and 'I would like my Add-in to load...' - Click Next.

9) Its up to you if you want a 'Help About' box. - Click Next.

10) Click Finish.


You are almost ready, we just need to sort out the Add-in registration.

Your new Add-in will be already setup to be a Visual Studio 2010 Add-in.
We just need to customise it a bit, so that SSMS recognises it.

11) VS2010 has created you two .Addin files, delete the 'MyAddin1 - For Testing.AddIn'

12) Locate the 2nd .Addin file, it will be somewhere like; c:\users\XXXX\documents\visual studio 2010\Projects\addin\MyAddin1\MyAddin1\MyAddin1.AddIn
Move it to C:\Users\Public\Documents\microsoft\MSEnvShared\AddIns

This is the folder that SSMS is looking for Add-in files.
[Update 19/08/2011: You may need to create the MSEnvShared\AddIns path.  Thanks to Raphael for spotting this]
[Update 19/05/2012: For the final release of SQL Server 2012, the .Addin file must go in the C:\ProgramData\Microsoft\MSEnvShared\AddIns folder]13) You can now delete the MyAddin1.Addin from your project.

14) Edit the C:\Users\Public\Documents\microsoft\MSEnvShared\AddIns\MyAddin1.Addin' file.
Change the top section from :

<HostApplication>   <Name>Microsoft Visual Studio</Name>
    <Version>10.0</Version>

</HostApplication> 

to be:


<HostApplication>   <Name>Microsoft SQL Server Management Studio</Name>
    <Version>*</Version>

</HostApplication> 


and

Update the Assembly to be the full path to your DLL.
<Assembly>c:\users\XXXXX\documents\visual studio 2010\Projects\addin\MyAddin1\MyAddin1\bin\Debug\MyAddin1.dll </Assembly>



14) Update the Project Properties, in the Debug section, update Start external program to be C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe (Of course, update this if you have installed Denali somewhere else)

15) Turn off P-Invoke warnings. Press CRLT + ALT + E - In Managed Debugging Assistants, find PInvokeStackImbalance, untick it.

16) Add a break point to the Connect() line, Hit F5 and run for the hills - because you should have a SQL Server Management Studio Add-in working.


The Add-in doesn't do very much yet, I will save that for another post. 

But you have an Add-in running, which was easier to write than Add-ins for SQL 2005 and 2008.

The only tricky, bit was the location and contents of the .Addin file.

Have fun writing your own Add-ins!!
[Updated 19/08/2011: I’ve got to say thanks for Mladen Prajdić for the telling me what the version needs to be in the XML]


Sunday, 7 August 2011

Can not connect to SQL Server remotely

Having trouble connecting to a new instance of SQL on a new Windows 2008 install?

Took me longer to work out than I care to admit.

I checked all the basics, allow remote connections, browser service running etc.etc..

Answer: Turn off Windows Firewall!! DOH! (Or at least add the SQL ports to the allowed list!)

SQL Server User Group in Southampton 10th August

The next Southampton SQL Server User Group is on the 10th of August.

Neil Hambly (@Neil_Hambly | web ) and John Martin (@SQLServerMoney) are both presenting - for more details check out the SQLServerFAQ.com website.

See you there!!

Thursday, 28 July 2011

Drawing a circle in SQL Server (almost)

I've just started playing with the Geo spatial data types in SQL Server 2008 R2 Express.
The first thing I wanted to do was draw a circle.  How difficult could it be?

I could not find an example on the interweb - so I put one together.

   1:  DECLARE @Points AS FLOAT = 360;
   2:   
   3:  DECLARE @count AS INT = 0;
   4:   
   5:  DECLARE @size AS INT = 1;
   6:   
   7:  DECLARE @tmpTable TABLE (
   8:      geo geometry);
   9:   
  10:  WHILE (@count < @Points)
  11:      BEGIN
  12:          INSERT INTO @tmpTable
  13:          SELECT geometry::STGeomFromText('POINT(' + CAST (SIN(RADIANS((CAST (@count AS FLOAT) / CAST (@Points AS FLOAT)) * @Points)) * @size AS VARCHAR) + ' ' + CAST (COS(RADIANS((CAST (@count AS FLOAT) / CAST (@Points AS FLOAT)) * @Points)) * @size AS VARCHAR) + ')', 4326) AS Posn;
  14:          SET @count = @count + 1;
  15:      END
  16:   
  17:  SELECT geo
  18:  FROM   @tmpTable;  

Now choose the 'Spatial Results' tab - you might have never seen this before. It only appears if the results set has a geospatial data type in it.




The observant of you might notice, it's not actually a circle. Just 360 different point (there is a different!) But I'll cover that in my next blog post.

[UPDATED: Added link to scripts]
All scripts can be found on my SkyDrive using the link below.

Monday, 18 July 2011

Southampton SQL User Group Next Events

[Updated: For latest news and events at Southampton SQL User Group follow this link]

I've been busy organising the next events for the Southampton SQL User Group and here they are.

10th August, John Q Martin and Neil HamblyDetails
7th September, Jessica Meats and Simon Sabin (MCM/MVP)Details
5th October, Justin Langford (MVP) and TBCDetails
One year Anniversary 
9th November, Allan Mitchell  and James Rowland-Jones

Details

For details of your local SQL Server User Group visit http://www.sqlserverfaq.com

Sunday, 17 July 2011

SQLBits 9 - Session submission

 

I’ve finally done it, I’ve submitted a session for SQLBits 9!!

 

God news knows if it will be picked, but it will only get picked if people vote for it!!!

 

So get on and vote!! If not for me, then someone else! http://sqlbits.com/information/publicsessions.aspx

Saturday, 16 April 2011

SQL Server User Groups in the UK!!

I tried to get this list together before SQLBits 8, but failed.  It contains SQL Server User Groups and the postal code of where it was held.  I've included SQLBits (for fun!).  Its not every single one thats ever happened, buts ones that have happened over the last 7 or 8 months. I apologise if I've missed you off!!



UG Name,City,Postcode
---------------------
Scottish Area User Group,Edinburgh,EH1 3EG
SQL Herts,Hatfield,AL9 5HZ
Leeds User Group,Leeds, LS3 1EP
Manchester User Group, Manchester, M1 3HU
SQLSoton,Southampton,SO17 1XQ
Cardiff User Group,Cardiff,CF10 5BT
Bristol User Group, Bristol,BS1 4QD
Kent SQL Server and .NET User Group,Maidstone,ME14 1ED
Birmingham User Group,Birmingham,B2 5NY
Surrey User Group,Camberley,GU16 7ER
Bracknell User Group,Bracknell, RG12 1AQ
Dundee User Group,Dundee, DD1 4HN
Reading UG,Reading, RG6 1WG
London UG,London, EC4M 7AN
Cambridge UG,Cambridge,CB4 0WZ
BCS,Telford, TF2 9FT
SQL SouthWest,Exetor,EX1 1JJ
SQLBits 8,Brighton, BN1 2FW
SQLBits 7,York,YO10 5DD
SQLBits 6,London,SW1P 3NZ
SQLBits 5,Newport,NP18 1HQ
SQLBits 4,Manchester,M15 6BH
SQLBits 3,Hatfield,AL10 9AB
SQLBits 2,Birmingham,B4 7ET
SQLBits 1,Reading, RG6 1WG


Link to these points on a bing map

*SQL SouthWest postcode is a guess.

Thursday, 14 April 2011

The SQLBits 8 Community Corner Map!

After 2 days of pinning, this is the result....The SQLBits 8 Community Corner Map!

I would like to thank all the people that came to Community Corner and stuck pins in the map and all the very kind non-UK people that took the time to draw their own countries (including Italy, Germany, Holland, Sweden, Norway, Denmark, Austria and Australia ) and pin them on the map.

Here is all your work.......




I have to thank all the people who stayed in Community Corner and represented the SQL Communities in the UK! - You know who you are!!! Finally we have to thank the SQLBits committee who gave us the opportunity to have community corner.

See you at the next SQLBits!!

Red pins = User Groups , Blue Pins = People..

Friday, 4 March 2011

T-SQL Tidy SSMS 2008 R2 released...

Tonight I've finally done it, posted up the R2 version of the SQL Server Management Studio Add-in.

You can download it from here; http://www.tsqltidy.com/downloads/addinR2.zip

It's got some new features!!!
  • Local formatting (no longer uses the web service - so no internet connection required)
  • Lots and lots of formatting options.
  • Format SQL to HTML!
So on give it a go, what have you got to lose?