|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 4:34 AM
Points: 92,
Visits: 282
|
|
If any one is having troubkle with this (on SQL 2005) make sure that the INSERT statement reads as follows...
INSERT into ##TempSync EXEC sp_msforeachdb @SQL
For some reason the ##TempSync & EXEC are run together if you just copy and paste.
Mark
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 1:32 PM
Points: 6,
Visits: 78
|
|
| THANKS!!!! That did it. You're a genious.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:07 AM
Points: 1,221,
Visits: 2,614
|
|
It looks like the formatting gets messed up when you cut and paste. This is what I got when I cut and pasted.
INSERT into ##TempSyncEXEC sp_msforeachdb @SQL (incorrect)
Make sure the line reads...
INSERT into ##TempSync EXEC sp_msforeachdb @SQL
Ken Simmons http://twitter.com/KenSimmons
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:40 PM
Points: 29,
Visits: 519
|
|
This script reminds me of a song and goes a little something like this:
"I've been through the desert on a horse named Dewayne"
What's up, Ken.
Rodney
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:26 PM
Points: 1,258,
Visits: 341
|
|
Same issue on both SQL Server 2000 and 2005: syntax errors. The copy is jumbling the code and I was careful in separating it but no luck.
update -- I managed to find the errors. Somehow the NL were lost on some of the lines and I had missed fixing a couple. But I got both scripts running. Thank you for the script.
-- Mark D Powell --
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 03, 2008 7:05 PM
Points: 1,
Visits: 8
|
|
Hi Ken,
Great scripts!
Do you have anything that works with Windows Users\Logins?
I wrote a script awhile ago that removes database access for each user and reassigns it. It's a pain if the user owns any objects as it has to swap ownership during the process, but it works. It also takes into account permissions.
Your script is much more simple. The only problem is with Windows Users\Logins.
Any ideas?
Thanks!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:07 AM
Points: 1,221,
Visits: 2,614
|
|
Since the copy is not working from the article, I am posting it here. Maybe everyone will have better luck with this one.
--Query 1 DECLARE @Collation varchar(100) DECLARE @SQL VARCHAR(2000)
CREATE TABLE ##TempSync ( DB_NME Varchar(50), DBUserName varchar(50), SysLoginName varchar(50) )
SELECT @Collation = CONVERT(SYSNAME,DatabasePropertyEx('master','Collation'))
SET @SQL = 'USE [?] SELECT ''?'' DB_NME, A.name DBUserName, B.loginname SysLoginName FROM sysusers A JOIN master.dbo.syslogins B ON A.name Collate ' + @Collation + ' = B.Name JOIN master.dbo.sysdatabases C ON C.Name = ''?'' WHERE issqluser = 1 AND (A.sid IS NOT NULL AND A.sid <> 0x0) AND suser_sname(A.sid) IS NULL AND (C.status & 32) =0 --loading AND (C.status & 64) =0 --pre recovery AND (C.status & 128) =0 --recovering AND (C.status & 256) =0 --not recovered AND (C.status & 512) =0 --offline AND (C.status & 1024) =0 --read only ORDER BY A.name'
INSERT into ##TempSync EXEC sp_msforeachdb @SQL
SELECT * FROM ##TempSync
DROP TABLE ##TempSync
--Query 2
DECLARE @Collation VARCHAR (100) DECLARE @SQL VARCHAR(2000)
SELECT @Collation = CONVERT(SYSNAME,DatabasePropertyEx('master','Collation'))
SET @SQL = 'USE [?] DECLARE @DBUserName varchar(50) DECLARE @SysLoginName varchar(50) DECLARE SyncDBLogins CURSOR FOR SELECT A.name DBUserName, B.loginname SysLoginName FROM sysusers A JOIN master.dbo.syslogins B ON A.name Collate ' + @Collation + ' = B.Name JOIN master.dbo.sysdatabases C ON C.Name = ''?'' WHERE issqluser = 1 AND (A.sid IS NOT NULL AND A.sid <> 0x0) AND suser_sname(A.sid) IS NULL AND (C.status & 32) =0 --Loading AND (C.status & 64) =0 --pre recovery AND (C.status & 128) =0 --recovering AND (C.status & 256) =0 --not recovered AND (C.status & 512) =0 --offline AND (C.status & 1024) =0 --read only ORDER BY A.name
OPEN SyncDBLogins FETCH NEXT FROM SyncDBLogins INTO @DBUserName, @SysLoginName
WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_change_users_login ''update_one'', @DBUserName, @SysLoginName FETCH NEXT FROM SyncDBLogins INTO @DBUserName, @SysLoginName END CLOSE SyncDBLogins DEALLOCATE SyncDBLogins ' EXEC sp_msforeachdb @SQL
Ken Simmons http://twitter.com/KenSimmons
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, August 01, 2010 4:44 PM
Points: 1,
Visits: 8
|
|
I am getting the following error on SQL 2005 Sp2 on the query below
SELECT @Collation = CONVERT(SYSNAME,DatabasePropertyEx('master','Collation'))
Msg 137, Level 15, State 1, Line 1 Must declare the scalar variable "@Collation".
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:07 AM
Points: 1,221,
Visits: 2,614
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
Nicely done, Ken... gonna add this one to my toolbelt... Heh, gotta find some way to get rid of the cursor, though (it's just me :D )
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|