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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Update table where values exist in another?

Author  Topic 

readysetstop
Posting Yak Master

123 Posts

Posted - 2007-06-29 : 15:14:40
I'm putting together an audit script (yes another one) to audit database option settings. I've got a full list of all databases and options, as well as a list of what databases have what options set.

The question is, how do I update my full list of dbs and options to show what's in the other list? (What's set.)

Hope I explained that right...

Here's what I have so far. Please tell me if I'm going about this the wrong way.


-- Get database options
-- D Maxwell, June 2007
-- Just a wrapper script for sp_dboption
-- that gets the options for all dbs.

-- First, get a list of possible options:
create table ##all_options (option_name varchar(50) not null)
insert into ##all_options exec master..sp_dboption

-- Now, get the list of databases.
create table ##dbnames (dbname varchar(25) not null)
insert into ##dbnames select name from master..sysdatabases

-- Now, get the set options for each database.
-- First you need a table to hold them.
create table ##dboptions_set (
dbname varchar(25) null,
dboption varchar(50) not null
)

-- Now you can insert the db names and options into the table.

while (select count(*) from ##dbnames) > 0
begin
declare @db varchar(25)
set @db = (select top 1 dbname from ##dbnames)

-- We do this part first since it returns multiple rows.
insert into ##dboptions_set(dboption)
exec master..sp_dboption @dbname = @db

-- Now we go back and fill in the db names.
update ##dboptions_set
set dbname = @db where dbname is null

-- We're done with that one. Loop to the next one.
delete from ##dbnames where dbname = @db
end

-- We have the list, now we make it useful. First,
-- we need to build a report table that shows all DBs
-- and all options. An actual good use of a cross join.

-- Got to get the name list back, first.
insert into ##dbnames select name from master..sysdatabases

create table ##option_report (
dbname varchar(25) not null,
dboption varchar(50) not null,
setting tinyint null
)

insert into ##option_report
select d.dbname, o.option_name, 0
from ##dbnames d cross join ##all_options o
order by d.dbname

-- Now we set the options as set or not set,
-- according to the ##dboptions_set table.



...and I'm stuck. I've tried a few different methods, involving joins and EXISTS and such, but nothing is working so far. The logic escapes me...

Any suggestions or pointers on how to best accomplish this are appreciated.

Thanks.

-D.

____________________________________________________________________________________
"Believe in those who are seeking the truth. Doubt those who say they have found it." -Andre Gide

readysetstop
Posting Yak Master

123 Posts

Posted - 2007-06-29 : 15:25:25
And 30 seconds later, foolish me, I got this. If anyone has a more efficient method, I'd love to hear it.


while (select count(*) from ##dboptions_set) > 0
begin
declare @dbchecked varchar(25)
declare @optionset varchar(50)

set @dbchecked = (select top 1 dbname from ##dboptions_set)
set @optionset = (select top 1 dboption from ##dboptions_set)

update ##option_report
set setting = 1
where dbname = @dbchecked
and dboption = @optionset

delete from ##dboptions_set
where dbname = @dbchecked
and dboption = @optionset
end


Thanks.

-D.

____________________________________________________________________________________
"Believe in those who are seeking the truth. Doubt those who say they have found it." -Andre Gide
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-07-02 : 02:55:12
Why not a simple join?

update r
set setting = 1
from ##option_report r JOIN ##dboptions_set s
on r.dbname = s.dbname and r.dboption = s.dboption


Also, why you need global temp table? why not local temp table?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

readysetstop
Posting Yak Master

123 Posts

Posted - 2007-07-02 : 08:57:15
quote:
Originally posted by harsh_athalye

Why not a simple join?
update r
set setting = 1
from ##option_report r JOIN ##dboptions_set s
on r.dbname = s.dbname and r.dboption = s.dboption



Because I didn't know you could use a join that way.
quote:

Also, why you need global temp table? why not local temp table?


I think because originally, I had this written as several batches, rather than one, and my understanding was that once you end the batch (with a GO), you'd lose a local temp table. I went back and read the BOL entry on it, and after a little experimentation, I see I was incorrect and the tables won't be dropped until the session is.

As you can see, I'm still learning. A lot. Thanks for the help.

____________________________________________________________________________________
"Believe in those who are seeking the truth. Doubt those who say they have found it." -Andre Gide
Go to Top of Page
   

- Advertisement -