Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Friday, 14 February 2014

Creating the ASP.net Membership Tables In SQL Server

Because I always forget and I rarely have to create the asp.net membership tables (usually backup from exisitng database) I decided to put the command in a blog post. So the command:

aspnet_regsql -S -d -E -A mr 

-S and -d are obvious command line options. -E authenticates using the Windows credentials of the currently logged-on user. - A adds support for one or more ASP.NET application services. Service identifiers can be specified together or separately. The following identifiers are used for ASP.NET application services: m - Membership r - Role Manager

Thursday, 12 July 2012

SQL Server and CTE

I recently posted a function to perform string replacement for T-SQL which we were using with a cursor to run through a some look up table and replace any occurences of the lookup text in the destination with the corresponding replacement text. After initial testing we were finding that the cursor was really killing performance so we needed a way to replace the cursor.  All roads led to CTEs (Common Table Expression)  but apart from a small function to create a list of people from a tree structure (recursive CTE), that a colleague had written, I had not seen many CTEs in action.

I had a few examples of recursive CTEs from around the web to build from and so I could easily get a CTE to recusively look through a table of lookups and replace a destination string with the corresponding string. The added complication was that there was actually several lookup tables combined into one lookup table and  first I had to link the look up table to an id in the destination table.  An example of the two tables are shown below:

ItemIDLookupReplacement
1MXMN
1MYCY
1 SBDG
2HGTY
2HDOK
Table 1: The lookup table

ItemIDStringResult
1MX+MY+SB
2HD/HG
  Table 2: The string table with column for resultant string with replacements
The desired result is to have MN+CY+DG in row 1 column 3 and OK/TY in row 2 column 3.  The first idea was to just let the lookup navigate all the way through table 2 but this may have replaced strings in the result that were not linked to the item (i.e. If row2 of table 2 contained SB) and would be really inefficent as in the real system these tables could potentially contain 1000s of rows.

I Ain't Afraid of No Ghost

I am often "copying" databases from different dev boxes and live boxes by backing up and restoring. However this causes problems with user accounts.

"Ghost" users are created when a database is backed up and restored to a different machine which uses local SQL user accounts. Even if the local SQL user account is recreated, because the underlying ID doesn't match, when the user tries to access the database the message will say the User does not exist. However when you try and add the user you will be told the user already existings. This causes access issues which at first glance seem illogical.

To fix this run the following query against the database:

use <database name>

go 

sp_change_users_login 'auto_fix', '<local login name>'


Tuesday, 1 May 2012

Conditional Joins

I recently came across a problem where I needed to join a table based on whether the a row in another column contained a flag.  I needed to retrive a date field from either a sale table or order table depending on status of the order.  I needed to join the tables as there was certain other data I need to take into account. 

Unfortunately SQL doesn;t support a case join statement so I went off to serch for a solution. Jeffs blog revealed a good solution involving left joining both tables then using COALESCE to get the relevant date.  I use a where statement on the status table so that I only get the data from the order table when the status is not sold and from the sale table when the item has been sold.

select
  C.CustomerName, coalesce(s.date,o.date) as ActivityDate
from
  Customers C
left outer join 
  Sales S on ...
left outer join
  Orders O on ...
where
  O.Dateis not null OR S.Date is not null

The solution worked well and, as in Jeffs blog, I could have use CASE instead of the COALESCE but I went with COALESCE for neatness.

Thursday, 12 April 2012

Arrrgh, I've Deleted my Default Database

I have a local instance of SQL server 2005 on my desktop which hosts a number of databases that I have worked on over the years.  I decided I would have a bit of a clean up and deleted a number of the databases.  Unfortunately, I hadn't updated my main login and deleted the default database.  In SQL server this means that when a login has no default database the login will not be given access to the server object so I could no longer login. 

I couldn't remember my sa password so it was off to google to see what I could do.  Fortunately I found a command to open SSMS using a different DB (master). 


sqlwb -S <login name> -d master -E

Once into SSMS I could then alter the default database of the login to master.

EXEC sp_defaultdb <login name>, 'master'

This altered the login default database and I checked by running

SELECT * FROM master.dbo.syslogins

There it was, default db changed.  Whilst I was there I updated a number of logins that had deleted databases as their master and everything was hunky dory.  I also changed the sa password to something I would remember so that I didn't caught out again.

Thursday, 23 February 2012

Creating ASP.NET tables in SQL server

To create all of the user and membership tables in SQL Server 2010 there is a command line tool within the tools directory (use the link to the command prompt) that can be used.  The command to get the tool to create the tables is:

ASPNET_RegSQL.exe -S (Server) -d (Database) -A m -E

-A m creates the membership tables and -E uses a trusted connection.

The command creates the Applications, Membership, SchemaVersions and Users tables for ASP.NET.