<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>davedolan.com &#187; SQL Code</title>
	<atom:link href="http://davedolan.com/blog/category/general-programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://davedolan.com/blog</link>
	<description>Website not included.</description>
	<pubDate>Tue, 17 Jun 2008 19:58:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Quick, list the dbs and Users!</title>
		<link>http://davedolan.com/blog/2008/01/21/quick-list-the-dbs-and-users/</link>
		<comments>http://davedolan.com/blog/2008/01/21/quick-list-the-dbs-and-users/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 18:21:37 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
		
		<category><![CDATA[SQL Code]]></category>

		<guid isPermaLink="false">http://davedolan.com/blog/2008/01/21/quick-list-the-dbs-and-users/</guid>
		<description><![CDATA[I’ve been orchestrating a move from an old SQL 2000 database to a SQL 2k5 DB host. Most of the dbs are in use somewhere by some application or other, and I can’t tell by looking at it just what they are. One of the things I’ll need to do is contact all of the [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been orchestrating a move from an old SQL 2000 database to a SQL 2k5 DB host. Most of the dbs are in use somewhere by some application or other, and I can’t tell by looking at it just what they are. One of the things I’ll need to do is contact all of the users of each one and ask them to tell me what the impact is, as well as inform them that they’ll need to swap a hostname out in the connection string. No problem, I can just click through all 50 databases on the host and look at all the users, and write them down, and send an email to them all asking what gives… The trouble is, half the time they have no idea what db’s are on the host, and what if any they have to do with it. I didn’t feel like spending about 4 hours on this, so I quickly tossed off a script that generates some SQL to figure out the real list, without getting too complicated here. In case you find it useful, here’s the script to generate the second script:</p>
<div class="code">
<code><br />
use master</p>
<p>&#45;- below is a query to generate a single query to get all the users for each database<br />
begin<br />
declare dbs cursor for select name from sysdatabases</p>
<p>declare @dbName VARCHAR(25)</p>
<p>open dbs</p>
<p>fetch dbs into @dbName<br />
while @@fetch_status &gt;= 0<br />
begin</p>
<p>&nbsp;&nbsp;if @dbName in (&#039;master&#039;, &#039;msdb&#039;, &#039;tempdb&#039;, &#039;pubs&#039;, &#039;model&#039;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;begin<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#039;&#45;- skipping system db &#039; + @dbName<br />
&nbsp;&nbsp;&nbsp;&nbsp;end<br />
&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;begin<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#039;&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-&#039;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#039;&#45;- &#039; + @dbName + &#039; &#45;-&#039;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#039;&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-&#039;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#039;select &#039;&#039;&#039; + @dbName + &#039;&#039;&#039; as [DB], name as [User] from &#039; + @dbName + &#039;.dbo.sysusers where hasdbaccess = 1&#039;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#039;union&#039;<br />
&nbsp;&nbsp;&nbsp;&nbsp;end</p>
<p>&nbsp;&nbsp;fetch next from dbs into @dbName<br />
end</p>
<p>close dbs<br />
deallocate dbs<br />
end</p>
<p>go</p>
<p></code>
</div>
<p>After that runs it will generate in the output window (or the console if you use OSQL or some other command line tool to run it,) this form of output. I pipe it to another file, and change the last ‘union’ to a ‘go’ and we’re done. I probably should have also excluded the user dbo, but I didn’t you can if you want.</p>
<div class="code">
<code><br />
&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-<br />
&#45;- FirstDB &#45;-<br />
&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-<br />
select &#039;FirstDB&#039; as [DB], name as [User] from FirstDB.dbo.sysusers where hasdbaccess = 1<br />
union<br />
&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-<br />
&#45;- aspnetdb &#45;-<br />
&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-<br />
select &#039;aspnetdb&#039; as [DB], name as [User] from aspnetdb.dbo.sysusers where hasdbaccess = 1<br />
union<br />
&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-<br />
&#45;- OtherDB&#45;-<br />
&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-<br />
select &#039;OtherDB&#039; as [DB], name as [User] from OtherDB.dbo.sysusers where hasdbaccess = 1<br />
go &#45;- changed from union as generated by the script<br />
&#45;- this continues until all dbs are enumerated, excludes the system dbs.<br />
&nbsp;&nbsp;</code>
</div>
]]></content:encoded>
			<wfw:commentRss>http://davedolan.com/blog/2008/01/21/quick-list-the-dbs-and-users/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
