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
450 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now