Solved

tsql to loop thru all user ('master', 'model', 'msdb', 'tempdb') dbases except and determine which dbases are missing a specific field for a specific tbl

Posted on 2010-09-15
14
453 Views
Last Modified: 2012-05-10
we are looking for a tsql to loop thru all user ('master', 'model', 'msdb', 'tempdb') dbases except and determine which dbases are missing a specific field for a specific tbl
0
Comment
Question by:rastafaray
  • 8
  • 6
14 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33679960
you could check this:
exec sp_msforeachdb ' if ( not ''?'' in ( ''master'', ''msdb'', ''tempdb'', ''model'') )
  begin  
    if not exists(select * from syscolumns where name = ''column_name'' and id = object_id(''table_name'') )
    begin
      select ''not found in ?''
    end 
    else
    begin
      select ''found in ?''
    end 
  end
'

Open in new window

0
 

Author Comment

by:rastafaray
ID: 33680415
in our case we need the schema name.  how can we do this?  here is a sample statement that works:

select * from syscolumns where name = 'sFieldName' and id = object_id('schema_name.tbl_name')

good news is that the schema name is the same as the dbname, so this too will work
select * from syscolumns where name = 'sFieldName' and id = object_id('db_name.tbl_name')

so the following line needs a revision.  not entirely sure about the syntax:

if not exists(select * from syscolumns where name = ''column_name'' and id = object_id(''db_name''+'.'+''table_name'') )


ty angelIII
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33680429
exec sp_msforeachdb ' if ( not ''?'' in ( ''master'', ''msdb'', ''tempdb'', ''model'') )
  begin  
    if not exists(select * from syscolumns where name = ''column_name'' and id = object_id(''table_name'') and exists( select null from sys.objects where id = object_id("table_name") and schema_id = user_id("schema_name"))  )
    begin
      select ''not found in ?''
    end
    else
    begin
      select ''found in ?''
    end
  end
'
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:rastafaray
ID: 33680439
is this also acceptable?

if not exists(select * from syscolumns where name = ''sOrderCancelFilter'' and id = object_id(DB_NAME()+''.SR_UserOptions'') )
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33680473
no. object_id only uses the object name, which does not include the schema name
0
 

Author Comment

by:rastafaray
ID: 33680479
though the fld and tbl names are static, the following may not be usable as the schema name for each dbase is unique and not static.
the schema name is the same as the dbname


if not exists(select * from syscolumns where name = ''column_name'' and id = object_id(''table_name'') and exists( select null from sys.objects where id = object_id("table_name") and schema_id = user_id("schema_name"))  )


or


 if not exists(select * from syscolumns where name = ''field_name'' and  schema_id = DBNAME() and id = object_id(''tbl_name'') )
0
 

Author Comment

by:rastafaray
ID: 33680482
k will try this next....

if not exists(select * from syscolumns where name = ''field_name'' and  schema_id = ? and id = object_id(''tbl_name'') )


0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33680488
syscolumns  does not have the schema_id column (or user_id cvolumn)
0
 

Author Comment

by:rastafaray
ID: 33680527
any other workaround in being able to pass the dbname as schema name to the 'select * from syscolumns' query?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33680570
as I showed in above example, you have to "join" with sysobjects table to check for the schema_id (from the schema name).
so, if you get the schemaname + tablename in 1 string, you have to first "split" it ...
0
 

Author Comment

by:rastafaray
ID: 33680589
got it.

for the following:
user_id("schema_name"))

can i use:

user_id(''?''))

so that the dbname is used?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33680617
that would only work if the db name is the same as the schema name.
if that is the case, sure
0
 

Author Comment

by:rastafaray
ID: 33680770
cool - that happens to be the case :)


TY for your help

0
 

Author Closing Comment

by:rastafaray
ID: 33680777
ty
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question