Wednesday, June 16, 2010

Performing Queries With T-SQL DBs With Illegal Characters In Their Name

I've been trying to connect to a DB on a completely different server, using Microsoft SQL Server Management Studio, and discovering a big problem. The SQL Server I'm trying to query has a '-' character in it's name.

This is overcome using two methods:
  1. Add the SQL Server to the local (on my development machine) SQL Server linked servers list.

    This is done by:

    Using the Object Explorer and opening "Server Objects -> Linked Servers", right clicking and selecting "New Linked Server".

    The important next step is that the "Linked server" field is filled out with the SQL Server name only, eg: SOMELIVEBOX-SQL5 and then the "SQL Server" radio button is selected.

    Next, on the left, select "Security" and choose "Be made using this security context:" and fill out the SQL Server login details.

  2. Then the query, and this is only relevant if the SQL Server name has an illegal character (like '-') in it.

    Lets say the table is called "MyTable", is in a database called "MyDB", is in the server mentioned in point 1 and we want all the records from it...

    Open a new query window from inside the local database and type:

    SELECT * FROM [SOMELIVEBOX-SQL5].MyDB.dbo.MyTable

Tuesday, June 15, 2010

My Favourite Chrome Extensions

Yep, cos I keep forgetting, each time set up a new PC....

Thursday, June 10, 2010

Can't Connect To Local SQLEXPRESS

This started occurring the other day and I was confused as to why the local database would not let me connect via standard Windows Authentication.

The answer turned out to be simply that the database had not been started as a Windows Service. This was because the Log On account it was trying to use did not have appropriate permissions. Not having another machine to check the correct settings, I decided that in the Services snap-in (Start -> Run -> services.msc -> SQL Server (MSSQLSERVER) & SQL Server (SQLEXPRESS)) I went to the Log On tab, selected "Local System account", hit "Apply" and then "Start" under the General tab.

For those two services, on my machine at least, this got everything working again. I was only able to discover this solution after googling and ending up here:

I encountered the exact same error. My work around to get the server back up and running is as follows. This applies to both the engine and the agent. The bug in this patch didn't affect the other SQL related services.

I changed the service account from the normal one to one that is local administrator to the server (me).

I was then able to start the services.

I went into the SQL Server 2005 Surface Area Configuration tool and changed Database Engine -> Remote Connections to use Local connections only.

I restarted the engine service.

I then changed it back to using Local and remote connections Using TCP/IP only.

I restarted the engine service.

I then changed both services back to their normal service account and all is well.

I could not get the patch to apply under any circumstances or configuration that I tried and have given up on it in hopes that MS releases a new one for this real quick.

Tuesday, June 08, 2010

Google Proxy Setting

If you are having trouble accessing the internet via Google's Chrome, try the solution suggested here:
Basically, (for Windows) make your Chrome shortcut look like this:
  • Target: "C:\Documents and Settings\\Local Settings\Application Data\Google\Chrome\Application\chrome.exe" --proxy-server=

Sunday, June 06, 2010

Load Balanced Session State Partitioning

Recently doing some research into getting session state management faster and more stable in an enterprise, load balanced environment. Not claiming to be any more knowledgeable than I was before, but definately more read...

Tutorials:
Discussions:
Of course, once you have your caching up and running you'll want to test it before sticking it in the wild:
And for those who want to know, my own comparison (please be aware that I have not exhaustively tested all options, this is based on research and some usage):

NCache
  • Distributed
  • Partitioaned
  • Replicated
  • Remote clients available
  • Expensive
  • Failover
  • Clustered
  • Free developer edition not appropriate for live environments
Memcached
  • Free
  • Open source
  • Distributed
  • Not partitioned
  • Not replicated
  • Servers unaware of each other
  • Not clustered
  • Least recently used model
Velocity
  • Sparsely documented
ScaleOut

Friday, June 04, 2010

Rediscovering A Blog Entry On The Button Element

Just seen this and though it warranted some attention - the Button HTML element looks rather more powerful than the input submit element, but "requires a little love":

Thursday, June 03, 2010

Get Notified About Public Holidays

So, using Outlook (in this case, 2007) you can add lots of public holidays to your calendar very easily:
  • Tools -> Options -> Calendar Options -> Add Holidays
Easy...

T-SQL IsNullOrEmpty

Just needed to do the T-SQL equivalent of String.IsNullOrEmpty(), so went looking and found these:

Wednesday, June 02, 2010

How To List The Columns In Your Table

I wanted to inspect, in T-SQL code, the list of columns in a table I had created. This is how I did it:
select c.* from sysobjects o inner join syscolumns c on o.id = c.id
where o.xtype = 'U' and o.name = 'myTableName'
And for reference, a handy page on the T-SQL system tables:

Friday, May 28, 2010

Concatenating String Fields In T-SQL

Lets say you've got a table called 'metaproperty' with an NVARCHAR(50) field called 'names'. How do you get all the values of the 'names' field concatenated into one string, separated by commas?

Like this:

select STUFF(
(SELECT N',' + names
FROM (SELECT * FROM metaproperty) AS Y
--ORDER BY names
FOR XML PATH('')),
1, 1, N'')

The ORDER BY is optional, as are any DISTINCTs you might want to shove in...

Thursday, May 27, 2010

PIVOT

Found these pages quite interesting but not managed to make use of it yet:
It's about how to use PIVOT. Have a look round his site and look for the UNPIVOT pages, much geeky fun to be had within a T-SQL server (2005 onwards.)

[UPDATE] The key thing I've found (as highlighted in the first link) is that the PIVOT statement operates upon the fields returned in the FROM clause, NOT the SELECT clause. This is important because it means, if you have a JOIN or two, you probably have a lot more fields to quote in your clause than you think. The solution, I found, is to nest the original query inside the FROM( ) AS virtualtable thereby reducing the pivot to only the fields you're concerned with.

eg:

select *
from
(
select
distinct top 100
v.pointid, v.doublevalue, p.displayname
from [property] p
inner join pointvalue v on p.propertyid = v.propertyid
inner join point pt on v.pointid = pt.pointid
where v.pointid in (select top 5 p.pointid from point p where p.instanceid = 36132)
) virtualtable
pivot
(
sum(doublevalue)
for [displayname] in ([Low Price], [High Price])
) as alias

Friday, May 21, 2010

Virtual Machines

My foray into the Mac world continues, though this post isn't specifically a Mac oriented thing. I have been looking for virtual machine applications...

Firstly, what Wikipedia has to say on the subject:
And the virtual PC applications I have used, or rather, had closer experience of than the vast array of virtualisation software out there:
And finally, my personal opine on the subject:
  • VirtualPC - Quite good, does what it says on the tin but hogs a lot of system resources. Will run non-Windows OS's, but probably is better running Windows. Discontinued, I understand, in deference to Hyper-V.
  • Parallels - Not the same kind of virtual machine as VirtualPC, in that it's expressly designed to run Windows on a Mac, but it does this beautifully. It can startup Windows which has been installed into Parallels or BootCamp and run them side by side.
  • BootCamp - Runs Windows on a Mac as a stand-alone OS, in that Mac OS X is not running at the same time. Allows the Mac to operate as a Windows PC and does it pretty much better than a regular PC can IMHO.
  • VMWare - Basically just like VirtualPC, but many would have it as being more stable and not Microsoft.

Web.config Element Positions

One thing that bugs me from time to time is that I cannot remember where the hell a web.config element is supposed to go. Some (ok, all) of them are extremely location sensitive. Some don't really care as long as certain elements appear before, but not necessarily immediately before, them.

Here are some which often (in the grand scheme of things, but not every day) annoy me and links to find others which might cause problems:

Thursday, May 20, 2010

I Don't Know Nout About Fonts, But I Know This

This is awesome - I've seen it tweeted recently, a lot, but it really is impressive:

Friday, April 30, 2010

Stuff You (I) Should Know

The following is a list of APIs and tools which I consider the current state of the art in .NET development (though not exclusively .NET) and that I really should know a lot more about. This should pretty much be complete right now, though I have a nagging feeling I missed one.
The following list is not complete, won't be for some time and may require time travel to keep reasonably accurate - list of best practice advice/docs:
And some videos which I hope will be helpful:
Footnote: This needs reorganising into a list of tech, each with a list of links and FAQ/Quickstarts.

Monday, April 26, 2010

Wednesday, April 21, 2010

Error 107 ERR_SSL_PROTOCOL_ERROR

Needing to apply SSL to an existing website (on my Windows Server 2003 development machine) I recently opened the properties on the website (under IIS, Web Sites), clicked Directory Security and Server Certificate and chose to apply/replace the certificate from an existing site/certificate.

This did not have the required effect, when requesting the page in the browser:
  • Using HTTP://
    The page must be viewed over a secure channel
  • Using HTTPS://
    Error 107 (net::ERR_SSL_PROTOCOL_ERROR): Unknown error IIS
  • And occasionally
    Bad Request (Invalid Hostname)
Essentially, this is because I was trying to reuse an existing certificate on a web site that had a different name. The solution is basically to replace the certificate with a valid certificate either from a trusted Certificate Authority (CA) or to use the SelfSSL.exe.

All of this is documented here, the first link being how to use the SelfSSL to get the job done on a development box:
It is important to note that when applying SSL certificates, especially via SelfSSL, that you open the site's Properties dialog:
  • Click Web Site tab
  • Click Advanced button
  • Under Multiple SSL identifies for this Web site select the site's IP row
  • Click Edit button
  • From the IP address drop down menu select the IP address of that site
  • Click Ok, Ok, Ok
This associates the SSL certificate with the site's IP and ensures that there are no conflicts. Click the Help button in this window for more information.

Wednesday, April 14, 2010

Tuesday, April 06, 2010

Debugging SQL And The Fun Attached To That

Having come across this problem in Commerce Server 2002 recently:
Server Error in '/' Application.

Source:Microsoft OLE DB Provider for SQL ServerDescription:Incorrect syntax near '('.Source:Microsoft OLE DB Provider for SQL ServerDescription:Invalid object name 'MSCS_CatalogScratch.dbo.Catalog__Query__Results__for_spid__304'.Source:Microsoft OLE DB Provider for SQL ServerDescription:Invalid object name 'MSCS_CatalogScratch.dbo.Catalog__Query__Results__for_spid__304'.
I discovered that the stored procedure failing was called "ctlg_GetResults_for_SingleCatalog" but how to work out where?

This page came in useful: http://support.microsoft.com/kb/316549

Basically, it's stepping through SQL Stored Procedures in Visual Studio. The account you're accessing the DB requires execute permissions, etc, of course, but from there you should be able to step through the process just like code. Well, almost.

Friday, April 02, 2010

Always Check IIS

This is a lesson we all learn over and over again, I believe.

Recently trying to get the files in the MVC web app's directory '/Scripts/*' to be accessed I was constantly frustrated in not being able to load simple .js files, etc, in the browser page.

In this case, it was because Default Web Site in IIS already has a Scripts virtual directory setup. This, of course, does not behave as a normal subdirectory would and was stopping access to the files within it by the browser. This is expected, unless you've forgotten that fact because you've been working on WinSrv2k3 for too long...