Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
SMerrill
Posting Yak Master
206 Posts |
Posted - 2005-08-09 : 14:04:08
|
It is my task to inventory hundreds of SQL databases. I would like to make a meta-database in which to store info and stats about each database. These databases were written by others, and I am a contractor assigned to design such an inventory metadatabase and fill it with data.I concern myself primarily with security issues, such as determining when the programmers have assigned security rights to users instead of roles, or kept the Administrator login, or an SA login with no password, etc. (The standard list of faux-pazzes. )I am embarking on making an ERD that will list the servers, databases, database objects, logins, roles, assigned permissions, blah blah. I have worked on this for many days, and the security model is pretty complex.I have already made a VBA client program that connects to each server and executes a set of commands. Currently, I am just running sp_helpLogins and sp_helpUser on each server, and storing the resulting returned records to temporary tables. I intend to add more commands as necessary.Now I need to normalize the information gathered from the sp_Help* commands into a decently simple table structure.Has anyone already made such a table structure? I'd hate to reinvent the wheel.~ Shaun MerrillSeattle, WA |
|
Thrasymachus
Constraint Violating Yak Guru
483 Posts |
Posted - 2005-08-09 : 15:00:23
|
"I am embarking on making an ERD that will list the servers, databases, database objects, logins, roles, assigned permissions, blah blah. I have worked on this for many days, and the security model is pretty complex."Sounds like you are already reinventing the wheel. All of this data is in system tables and their associated INFROMATION SCHEMA views. Try doing a sp_helptext sp_helplogins and rewriting a new version like sp_helplogins2 and sp_helpprotect2 to get the format you want.And go see your boss and ask them if they need someone to write software to store data. I work for a reasonable fee.====================================================Regards,Sean RoussyGENERAL ADVICE FOR EVERYONE: Please backup all of your databases including master, msdb and model on a regular basis. I am tired of telling people they are screwed. The job you save may be your own. |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-08-09 : 15:15:24
|
Good luckI did that for sp_depends and it was painful. I basically made a copy of it and the re-wrote it.You probably need to do the same thing.BUT! Have you ever looked at sp_help?What a mess. Shows you (if you can figure it out) just what a mess the sql server catalog is. It may be built for speed, but not for usability.That's why they made the information_schema views.How about this...and I'm reaching now....Each section of sp_help is "Labeled" so you know what it is.Execute a cursorSELECT TABLE_NAME FROM INFORMATION_SCHEMA.TablesSELECT 'TABLE: ' + TABLE_NAMETHen execute sp_help for the tableHave query analyzer in text modeSave the text to a file and bcp it in to a table, then add an identity columnCursor through it row by row, inserting the data to the appropriate tables.That's the best I got for this.Or, tear apart sp_help and canabalize what you need.And if you do, let us know. That'd be a neat trickBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2005-08-09 : 15:30:02
|
>>> And go see your boss and ask them if they need someone to write software to store data. I work for a reasonable fee.<<<This is probably just me but I hate it when someone takes a community dedicated to mutaul assistance and exchange of information and turns it into a Marketing and Sales tool.It leads one to the impression that while both of us are helping people, one of us is getting money for it... (Boy, aren't I a chump! Why am I helping people for free?)But it's probably just me... Feel free to disregard.HTH=================================================================A conservative is one who admires radicals centuries after they're dead.-Leo Rosten, author (1908-1997) |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-08-09 : 15:37:03
|
I was a bit put off by it too. Most everyone here in the forums will work for money. Pimping it just isn't right unless you are paying for ad space here.Tara |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-08-09 : 15:38:44
|
kidding, he was kidding.....I've known Sean for a bithttp://www.dbforums.com/member.php?find=lastposter&t=1175312How to navigate the sql server catalog....hop in a Ferarri, put a blind fold on, do 200mph down the freeway...well it'd be easier than interpreting this:create proc sp_help @objname nvarchar(776) = NULL -- object name we're afteras -- PRELIMINARY set nocount on declare @dbname sysname -- OBTAIN DISPLAY STRINGS FROM spt_values UP FRONT -- declare @no varchar(35), @yes varchar(35), @none varchar(35) select @no = name from master.dbo.spt_values where type = 'B' and number = 0 select @yes = name from master.dbo.spt_values where type = 'B' and number = 1 select @none = name from master.dbo.spt_values where type = 'B' and number = 2 -- If no @objname given, give a little info about all objects. if @objname is null begin -- DISPLAY ALL SYSOBJECTS -- select 'Name' = o.name, 'Owner' = user_name(uid), 'Object_type' = substring(v.name,5,31) from sysobjects o, master.dbo.spt_values v where o.xtype = substring(v.name,1,2) collate database_default and v.type = 'O9T' order by Object_type desc, Name asc print ' ' -- DISPLAY ALL USER TYPES select 'User_type' = name, 'Storage_type' = type_name(xtype), 'Length' = length, 'Prec' = TypeProperty(name, 'precision'), 'Scale' = TypeProperty(name, 'scale'), 'Nullable' = case when TypeProperty(name, 'AllowsNull') = 1 then @yes else @no end, 'Default_name' = isnull(object_name(tdefault), @none), 'Rule_name' = isnull(object_name(domain), @none), 'Collation' = collation from systypes where xusertype > 256 order by name return(0) end -- Make sure the @objname is local to the current database. select @dbname = parsename(@objname,3) if @dbname is not null and @dbname <> db_name() begin raiserror(15250,-1,-1) return(1) end -- @objname must be either sysobjects or systypes: first look in sysobjects declare @objid int declare @sysobj_type char(2) select @objid = id, @sysobj_type = xtype from sysobjects where id = object_id(@objname) -- IF NOT IN SYSOBJECTS, TRY SYSTYPES -- if @objid is null begin -- UNDONE: SHOULD CHECK FOR AND DISALLOW MULTI-PART NAME select @objid = xusertype from systypes where name = @objname -- IF NOT IN SYSTYPES, GIVE UP if @objid is null begin select @dbname=db_name() raiserror(15009,-1,-1,@objname,@dbname) return(1) end -- DATA TYPE HELP (prec/scale only valid for numerics) select 'Type_name' = name, 'Storage_type' = type_name(xtype), 'Length' = length, 'Prec' = TypeProperty(name, 'precision'), 'Scale' = TypeProperty(name, 'scale'), 'Nullable' = case when allownulls=1 then @yes else @no end, 'Default_name' = isnull(object_name(tdefault), @none), 'Rule_name' = isnull(object_name(domain), @none), 'Collation' = collation from systypes where xusertype = @objid return(0) end -- FOUND IT IN SYSOBJECT, SO GIVE OBJECT INFO select 'Name' = o.name, 'Owner' = user_name(uid), 'Type' = substring(v.name,5,31), 'Created_datetime' = o.crdate from sysobjects o, master.dbo.spt_values v where o.id = @objid and o.xtype = substring(v.name,1,2) collate database_default and v.type = 'O9T' print ' ' -- DISPLAY COLUMN IF TABLE / VIEW if @sysobj_type in ('S ','U ','V ','TF','IF') begin -- SET UP NUMERIC TYPES: THESE WILL HAVE NON-BLANK PREC/SCALE declare @numtypes nvarchar(80) select @numtypes = N'tinyint,smallint,decimal,int,real,money,float,numeric,smallmoney' -- INFO FOR EACH COLUMN print ' ' select 'Column_name' = name, 'Type' = type_name(xusertype), 'Computed' = case when iscomputed = 0 then @no else @yes end, 'Length' = convert(int, length), 'Prec' = case when charindex(type_name(xtype), @numtypes) > 0 then convert(char(5),ColumnProperty(id, name, 'precision')) else ' ' end, 'Scale' = case when charindex(type_name(xtype), @numtypes) > 0 then convert(char(5),OdbcScale(xtype,xscale)) else ' ' end, 'Nullable' = case when isnullable = 0 then @no else @yes end, 'TrimTrailingBlanks' = case ColumnProperty(@objid, name, 'UsesAnsiTrim') when 1 then @no when 0 then @yes else '(n/a)' end, 'FixedLenNullInSource' = case when type_name(xtype) not in ('varbinary','varchar','binary','char') Then '(n/a)' When status & 0x20 = 0 Then @no Else @yes END, 'Collation' = collation from syscolumns where id = @objid and number = 0 order by colid -- IDENTITY COLUMN? if @sysobj_type in ('S ','U ','V ','TF') begin print ' ' declare @colname sysname select @colname = name from syscolumns where id = @objid and colstat & 1 = 1 select 'Identity' = isnull(@colname,'No identity column defined.'), 'Seed' = ident_seed(@objname), 'Increment' = ident_incr(@objname), 'Not For Replication' = ColumnProperty(@objid, @colname, 'IsIDNotForRepl') -- ROWGUIDCOL? print ' ' select @colname = null select @colname = name from syscolumns where id = @objid and number = 0 and ColumnProperty(@objid, name, 'IsRowGuidCol') = 1 select 'RowGuidCol' = isnull(@colname,'No rowguidcol column defined.') end end -- DISPLAY PROC PARAMS if @sysobj_type in ('P ') --RF too? begin -- ANY PARAMS FOR THIS PROC? if exists (select id from syscolumns where id = @objid) begin -- INFO ON PROC PARAMS print ' ' select 'Parameter_name' = name, 'Type' = type_name(xusertype), 'Length' = length, 'Prec' = case when type_name(xtype) = 'uniqueidentifier' then xprec else OdbcPrec(xtype, length, xprec) end, 'Scale' = OdbcScale(xtype,xscale), 'Param_order' = colid, 'Collation' = collation from syscolumns where id = @objid end end -- DISPLAY TABLE INDEXES & CONSTRAINTS if @sysobj_type in ('S ','U ') begin print ' ' execute sp_objectfilegroup @objid print ' ' execute sp_helpindex @objname print ' ' execute sp_helpconstraint @objname,'nomsg' if (select count(*) from sysdepends where depid = @objid and deptype = 1) = 0 begin raiserror(15647,-1,-1) -- 'No views with schemabinding reference this table.' end else begin select distinct 'Table is referenced by views' = obj.name from sysobjects obj, sysdepends deps where obj.xtype ='V' and obj.id = deps.id and deps.depid = @objid and deps.deptype = 1 group by obj.name end end else if @sysobj_type in ('V ') begin -- VIEWS DONT HAVE CONSTRAINTS, BUT PRINT THESE MESSAGES BECAUSE 6.5 DID print ' ' raiserror(15469,-1,-1) -- No constraints defined print ' ' raiserror(15470,-1,-1) --'No foreign keys reference this table.' execute sp_helpindex @objname end return (0) -- sp_help Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-08-09 : 15:44:56
|
quote: Originally posted by SMerrill I am embarking on making an ERD that will list the servers, databases, database objects, logins, roles, assigned permissions, blah blah. I have worked on this for many days, and the security model is pretty complex.
Thats a pretty broad definition for the type of data you want to store per object...With a bit more description, I'm sure a general direction could be determined...Oh! and I work for biscuits  Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
SMerrill
Posting Yak Master
206 Posts |
Posted - 2005-08-09 : 15:54:09
|
Yeah, don't pimp! Ha. You tell 'em, Tara!I was trying to be a good boy, and use the sp_help* commands instead of munching through proprietary system tables for this information.In the interest of FREE help, here's what I wrote to pump through each server and get sp_helplogins data.Private Sub ReadSecurity(ServerName As String) Dim cnnMe As ADODB.Connection Dim cnnRemote As ADODB.Connection Dim cmdHelp As New ADODB.Command Dim cmdInsert As New ADODB.Command Dim rst As ADODB.Recordset Dim aRec As Variant Dim iRec As Long Dim strConn As String Dim SkipServer As Boolean Const RowDim As Long = 2 Const colLoginName1 As Long = 0 Const colSID As Long = 1 Const colDefDBName As Long = 2 Const colDefLangName As Long = 3 Const colAUser As Long = 4 Const colARemote As Long = 5 Const colLoginName2 As Long = 0 Const colDBName As Long = 1 Const colUserName As Long = 2 Const colUserOrAlias As Long = 3 With CurrentProject Set cnnMe = .Connection End With SkipServer = False Set cnnRemote = New ADODB.Connection On Error Resume Next 'NOTE: *.UDL files are a good way to make up the following string: With cnnRemote strConn = Replace( _ "Provider=SQLOLEDB.1;Integrated Security=SSPI;" _ & "Persist Security Info=False;Data Source=|1;", "|1", ServerName) .ConnectionString = strConn .ConnectionTimeout = 2 .Open If Err.Number <> 0 Then SkipServer = True If SkipServer Then Debug.Print "Unable to connect to " & ServerName Else Debug.Print ServerName End If End With If SkipServer Then GoTo ExitHere On Error GoTo 0 With cmdHelp .ActiveConnection = cnnRemote .CommandText = "sp_helplogins" .CommandType = adCmdStoredProc Set rst = .Execute aRec = rst.GetRows End With With cmdInsert .ActiveConnection = cnnMe .CommandText = "sp_SQLHelpLogins1_Insert" .CommandType = adCmdStoredProc End With For iRec = LBound(aRec, RowDim) To UBound(aRec, RowDim) With cmdInsert .Parameters("@ServerName") = ServerName .Parameters("@LoginName") = aRec(colLoginName1, iRec) .Parameters("@SID") = aRec(colSID, iRec) .Parameters("@DefDBName") = aRec(colDefDBName, iRec) .Parameters("@DefLangName") = aRec(colDefLangName, iRec) .Parameters("@AUser") = aRec(colAUser, iRec) .Parameters("@ARemote") = aRec(colARemote, iRec) .Execute End With DoEvents Next iRec ' Get the second return set of the stored procedure: Set rst = rst.NextRecordset aRec = rst.GetRows With cmdInsert .CommandText = "sp_SQLHelpLogins2_Insert" End With For iRec = LBound(aRec, RowDim) To UBound(aRec, RowDim) With cmdInsert .Parameters("@ServerName") = ServerName .Parameters("@LoginName") = aRec(colLoginName2, iRec) .Parameters("@DBName") = aRec(colDBName, iRec) .Parameters("@UserName") = aRec(colUserName, iRec) .Parameters("@UserOrAlias") = aRec(colUserOrAlias, iRec) .Execute End With DoEvents Next iRecExitHere: Set rst = Nothing Set cmdHelp = Nothing Set cmdInsert = Nothing Set cnnRemote = Nothing Set cnnMe = Nothing Exit SubEnd Sub CREATE procedure dbo.sp_SQLHelpLogins1_Insert( @ServerName SYSNAME, @LoginName SYSNAME, @SID VARBINARY(85), @DefDBName SYSNAME, @DefLangName SYSNAME, @AUser CHAR(5), @ARemote CHAR(7)) AS-- When SP_HelpLogins is run from the client,-- it uses this sproc to load the data into SQL Server.INSERT INTO [DBInventory].[dbo].[temp_sphelplogins1]([ServerName], [LoginName], [SID], [DefDBName], [DefLangName], [AUser], [ARemote])VALUES( @ServerName, @LoginName, @SID, @DefDBName, @DefLangName, @AUser, @ARemote) CREATE TABLE [temp_sphelplogins1] ( [ServerName] [sysname] NOT NULL , [LoginName] [nvarchar] (128) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [SID] [varbinary] (85) NULL , [DefDBName] [nvarchar] (128) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [DefLangName] [nvarchar] (128) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [AUser] [char] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [ARemote] [char] (7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GO CREATE TABLE [temp_sphelplogins2] ( [ServerName] [sysname] NOT NULL , [LoginName] [sysname] NOT NULL , [DBName] [sysname] NOT NULL , [UserName] [sysname] NOT NULL , [UserOrAlias] [char] (8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GO CREATE procedure dbo.sp_SQLHelpLogins2_Insert( @ServerName SYSNAME, @LoginName SYSNAME, @DBName SYSNAME, @UserName SYSNAME, @UserOrAlias CHAR(8)) AS-- When SP_HelpLogins is run from the client,-- it uses this sproc to load the data into SQL Server.INSERT INTO [DBInventory].[dbo].[temp_sphelplogins2]([ServerName], [LoginName], [DBName], [UserName], [UserOrAlias])VALUES( @ServerName, @LoginName, @DBName, @UserName, @UserOrAlias) This beats ripping it all out and cursoring it back together, Brett. Yecch.~ Shaun MerrillSeattle, WA |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-09 : 16:00:28
|
Define the logical model first, don't let sp_help* drive the structure.Maybe, that was what You were asking for Run osql -L and cycle through all the servers on the network.I would create sql-scripts that hit the information_schema + system tables,and run these scripts on all the servers + databases sucking out the metadata, maybe using OPENQUERY.These scripts would align with the logical model, and crave minimal staging of the data.I believe that using the sp_help* stuff will cause more staging of the data and more work.My 2 cents.rockmoose |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-08-09 : 16:04:11
|
I second!!!Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
|
Thrasymachus
Constraint Violating Yak Guru
483 Posts |
Posted - 2005-08-09 : 20:21:12
|
It was a joke.I do not hang out here too much anymore. There are reasons. Some folks take themselves a little too serious. And I changed my footer a while ago thank you. You guys are database programmers and administrators with apparently too much time on your hands, not rocket scientists. SQL Goddess. Hmph. And Busta I could say some stuff about some of your posts but I am going to show some decorum. Oh BTW, I am going to continue to post here wether you guys like it or not and maybe to just piss you guys off (unless they shutdown my account). I am out. Got somethings to do.====================================================Regards,Sean RoussyGENERAL ADVICE FOR EVERYONE: Please backup all of your databases including master, msdb and model on a regular basis. I am tired of telling people they are screwed. The job you save may be your own. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-08-09 : 21:06:20
|
Even though your last post takes a shot at me, I'm going to ignore it...I certainly don't mind you posting here. I've enjoyed your posts thus far. Perhaps you should use emoticons to indicate when you are joking.Tara |
 |
|
Thrasymachus
Constraint Violating Yak Guru
483 Posts |
Posted - 2005-08-09 : 21:25:55
|
you fired the first shot. You called me a pimp. Where I went to high school, that was a compliment of sorts, but you were'nt using it that way. I mentioned writing a program that stores data after talking about reinventing the wheel. Obviously I was talking about a database engine. 1 + 1 = 2. I do not know, some people have a sense of humor, others don't. You like emoticons, here is one <edited out>. ====================================================Regards,Sean RoussyGENERAL ADVICE FOR EVERYONE: Please backup all of your databases including master, msdb and model on a regular basis. I am tired of telling people they are screwed. The job you save may be your own. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-08-09 : 21:36:24
|
I didn't call you a pimp. It was used as a verb.quote: You like emoticons, here is one <edited out>.
I'm not sure why you would write something like that, but I take it back that I enjoy reading your posts. I was being honest about that.Tara |
 |
|
Kristen
Test
22859 Posts |
Posted - 2005-08-09 : 22:13:02
|
"This is probably just me but I hate it when someone takes a community dedicated to mutaul assistance and exchange of information and turns it into a Marketing and Sales tool."No, you aren't alone.Kristen |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-10 : 09:20:04
|
Sean,Not everyone starts the day with 3 cups of sarcasm and cynisism,so don't expect everyone to understand Your sense of humor.If You get kicked out of the forums, maybe You can put it on Your resumé !?I will now proceed to hire Corey, because he works for biscuits rockmoose |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-08-10 : 09:34:51
|
Guys!ChillTo be fair, Sean was called out. If you want, let's take it to the corral (either one) to discuss.But I don't see the big deal anyway. He could have easily emailed the guy for work.Trust me, Sean is quite a busy guy.MOOAnd where the hell is the Dr.?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
Thrasymachus
Constraint Violating Yak Guru
483 Posts |
Posted - 2005-08-10 : 10:38:07
|
I would not care if they did kick me out.from now on...'code snippetDeclare @forum varchar(10)Declare @humor varchar(10)Declare @fun varchar(10) SET @forum = @inputIF @forum = 'SQLTeam'BEGIN SET @humor = 'false' SET @fun = 'false'END'Enter code for providing advice...====================================================Regards,Sean RoussyThank you, drive through |
 |
|
Pat Phelan
Posting Yak Master
187 Posts |
Posted - 2005-08-10 : 11:07:09
|
quote: Originally posted by rockmoose Not everyone starts the day with 3 cups of sarcasm and cynisism
Good heavens! Why not?quote: Originally posted by rockmoose so don't expect everyone to understand Your sense of humor.
That part I can identify with. Folks rarely get my jokes, but that's ok... It makes the ones that do that much more interesting to me!On a more serious note, while both Brett and Sean will gleefully "pimp out" on a moment's notice, I recognize Sean's comment above as pure joke... I can see how that might not be obvious to someone that doesn't know him well, and it appears that he has (I presume inadvertantly) annoyed some folks here which causes them to take his postings in the worst possible context (because that suits their expectations)...I'd suggest that everyone treat it as a learning opportunity. I think that Sean needs to be a bit more guarded, and that everybody needs to relax just a bit.-PatP |
 |
|
Thrasymachus
Constraint Violating Yak Guru
483 Posts |
Posted - 2005-08-10 : 11:17:47
|
Oh my cold black capitalist heart just grew three sizes. Dbforums is sticking up for me and I wasnt sure if they liked me at all either.====================================================Regards,Sean RoussyThank you, drive through |
 |
|
Next Page
|
|
|
|
|