The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘SQL Server 2005’ Category

The ADO.NET Entity Framework and SQL Server 2000: the ProviderManifestToken attribute and selecting it with XPath

Posted by jpluimers on 2012/05/23

Yes. Dorothy. There are people using the ADO .NET Entity Framework with SQL Server 2000  in parallel of moving towards a more modern Microsoft SQL Server version.

Entity Framework is lovely for developing data-centric applications.

By default, Visual Studio 2010 will target SQL Server 2008 as a database. That is fine, but it is kind of invisible it does: there is no property or dialog where you can change this.

What you have to change in order to have the Entity Framework send SQL Server 2000 compatible queries is to:

  1. Right click your .edmx file
  2. Choose “Open with”
  3. Choose the “XML (text) editor”
  4. Find the  ProviderManifestToken attribute
  5. Change the value (usually from “2008”) into “2000”
  6. Save the .edmx file
  7. Build and run your application

A few caveats:

Read the rest of this entry »

Posted in .NET, .NET ORM, C#, C# 2.0, C# 3.0, C# 4.0, Development, EF Entity Framework, Software Development, SQL Server, SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 | Leave a Comment »

Checking which applications have a TCP connection to SQL Server (DTAP)

Posted by jpluimers on 2012/05/15

When in a DTAP environment, you cannot always have complete clean boundaries. Issues in production don’t reproduce in acceptance, you cannot develop in production, etc.

So sometimes you have to simulate or connect to Test or Acceptance Database Servers from a Develop workstation.

There it can get hairy to keep track of which applications connect to which database server.

That’s where the below batch file comes in handy: it scans your systems on connections to common TCP ports used by SQL server, then for each connection give you some process details (or – if you add a commandline parameter – all details that TLINK can get).

The batch file uses the built in tools tasklist, netstat, find and sc (the latter to show information on the local running SQL Services).

It also uses TLIST, which can be a bit awkward to get.

Read the rest of this entry »

Posted in .NET, Batch-Files, CSV, Database Development, Development, Scripting, Software Development, SQL, SQL Server, SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 7 | Leave a Comment »

Migrating SQL Server 2000 ISQLW Utility to SQL Server 2008 R2 SSMS command-line parameters (SQL Query Analyzer; SQL Server Management Studio)

Posted by jpluimers on 2012/04/17

If you followed my blog, you probably already guessed that I’m assisting a client to prepare for a big SQL Server 2000 to SQL Server 2008 R2 migration. When not, you know now :)

I’m a fan of commandline, tools, and preconfigured settings. Which means that I’m in the midst of reconfiguring all my shortcuts to SQL Server 2000 tools to SQL Server 2008 equivalents.

When inspecting and changing LNK shortcut files, two tools are important:

  • dumplnk: dumps a shortcut lnk file from the commandline
  • shurtcut: creates a shortcut lnk file from the commandline

One of tools I many shortcuts for is the ISQLW aka SQL Query Analyzer, which I use far more than the SQL Server Enterprise Manager (more on SSEM in a future post).

Depending on the mode of authentication you use to connect to your SQL Server, there basically are two forms of shortcuts:

  • SQL Server Authentication (using SQL Server username and password):
    "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\isqlw.exe" /S"servername[\instancename]" /U"username" /P"password"
  • Windows Authentication (using the credentials of the currently logged in windows user):
    "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\isqlw.exe" /S"servername[\instancename]" /E

The former is less secure (so better to only store those shortcuts in a place that no other users can access).

From the ISQLW.exe syntax documentation:

Syntax

isqlw
[-?] |
[
[-S server_name[\instance_name]]
[-d database]
[-E] [-U user] [-P password]
[{-i input_file} {-o output_file} [-F {U|A|O}]]
[-f file_list]
[-C configuration_file]
[-D scripts_directory]
[-T template_directory]
]

The parameters /S, /U, /P and /E are very similar to the -S, -U, -P and -E ones from SSMS.exe (SQL Server Management Studio) with one distinction: there MUST be a space between each parameter and the value:

  • SQL Server Management Studio (using SQL Server username and password):
    "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe" -S "servername[instancename]" -U "username" -P "password"
  • SQL ServerManagement Studio (using credentials for the currently logged in windows user):
    "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe" -S "servername[instancename]" -E

Read the rest of this entry »

Posted in Batch-Files, Database Development, Development, Power User, Scripting, Software Development, SQL Server, SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 | 5 Comments »

Migrate/Transfer SQL Server 2008/2005/2000/7 Logins to SQL Server 2008

Posted by jpluimers on 2012/01/12

When moving databases across servers, you face the same problem as when upgrading servers: users are server specific, but permissions are databases specific. They are not bound by UserName, but through a SID (security identifier).

When adding the same UserName entries to a new server that already has the databases restored, you get error messages like these:

Error 21002 [SQL-DMO] User ‘account’ already exists

When adding the same UserName  entries, then restoring the databases, it won’t work, because the matching SIDs don’t exist.

There are many sites explaining the “Error 21002” and pointing to sp_change_users_login.

But sp_change_users_login (and the ALTER USER UserName WITH LOGINI = UserName2 in SQL Server 2005 and higher) is not the actual fix to the problem: it will re-add the user with a new SID, then correct the SID in the database currently in use.

Actually you are after “Orphaned” users: users that are defined in the various databases, but not present in the user list on the SQL Server instance. Running sp_change_users_login with the “Report’ parameter on the “new” server will show a list of orphaned users.

An even better way to show Orphaned Users is by running the ShowOrphanUsers script (thanks Vyas, wish more people republish your stuff with attribution!). In my own version (sourcecode is below), I have added an extra UserSID column of type varbinary(85).

In order to transfer users to a new server, you need a sp_help_revlogin stored procedure. Depending on the version of your SQL Server (7/2000/2005/2008), you need a slightly different version of a script that creates sp_help_revlogin for you. All versions are available at NetNerds.net.

–jeroen

via: Migrate/Transfer SQL Server 2008/2005/2000/7 Logins to SQL Server 2008.

USE master;
GO
IF OBJECT_ID ('dbo.ShowOrphanUsers') IS NOT NULL
  DROP PROCEDURE dbo.ShowOrphanUsers
GO
CREATE PROC dbo.ShowOrphanUsers
AS
BEGIN
	CREATE TABLE #Results
	(
		[Database Name] sysname COLLATE Latin1_General_CI_AS,
		[Orphaned User] sysname COLLATE Latin1_General_CI_AS,
                [UserSID]  varbinary(85)
	)

	SET NOCOUNT ON

	DECLARE @DBName sysname, @Qry nvarchar(4000)

	SET @Qry = ''
	SET @DBName = ''

	WHILE @DBName IS NOT NULL
	BEGIN
		SET @DBName =
				(
					SELECT MIN(name)
					FROM master..sysdatabases
					WHERE 	name NOT IN
						(
						 'master', 'model', 'tempdb', 'msdb',
						 'distribution', 'pubs', 'northwind'
						)
						AND DATABASEPROPERTY(name, 'IsOffline') = 0
						AND DATABASEPROPERTY(name, 'IsSuspect') = 0
						AND name > @DBName
				)

		IF @DBName IS NULL BREAK

		SET @Qry = '	SELECT ''' + @DBName + ''' AS [Database Name],
				CAST(name AS sysname) COLLATE Latin1_General_CI_AS  AS [Orphaned User],
                                SID AS [UserSID]
				FROM ' + QUOTENAME(@DBName) + '..sysusers su
				WHERE su.islogin = 1
				AND su.name <> ''guest''
				AND NOT EXISTS
				(
					SELECT 1
					FROM master..sysxlogins sl
					WHERE su.sid = sl.sid
				)'

		INSERT INTO #Results EXEC (@Qry)
	END

	SELECT *
	FROM #Results
	ORDER BY [Database Name], [Orphaned User]
END
GO

Posted in Database Development, Development, SQL, SQL Server, SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 7 | 1 Comment »