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
451 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

21 Experts available now in Live!

Get 1:1 Help Now