{"id":120,"date":"2006-06-06T21:51:00","date_gmt":"2006-06-06T21:51:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2006\/06\/06\/searching-tables-and-columns\/"},"modified":"2006-06-06T21:51:00","modified_gmt":"2006-06-06T21:51:00","slug":"searching-tables-and-columns","status":"publish","type":"post","link":"http:\/\/www.panahy.nl\/index.php\/2006\/06\/06\/searching-tables-and-columns\/","title":{"rendered":"Searching tables and columns"},"content":{"rendered":"<p>I am working with some big databases having similar tables for different group.<br \/>It seems to be poluted by different parties and have no administrator. Some developers<br \/>put their application initials as a prefix of the table names and as a result we<br \/>got several Contact tables and several Items each with diffeent layouts.<\/p>\n<p>When I am dealing with these tables I need to find everything to do with lets say<br \/>Code. I need to find out all the tables having a field like Tax_Code or TaxCode<br \/>or ItemCode etc. It might be even good to know if there are tables called like Meta_Code<br \/>or Item_Code etc.<\/p>\n<p>To find these two things I have written two stored procedures to use as follows<br \/>:<\/p>\n<p><span style=\"font-family:courier new;color:#3333ff;\">sp_FindColumns \u2018Code\u2019<br \/>And<br \/>sp_FindTables \u2018Code\u2019<br \/><\/span><br \/>When I was using these two I realised that also some stored procedures<br \/>are dealing with changing a field internally. So I had to also write something to<br \/>search the definitions of the stored rocedures to find al those mentioning the field<br \/>in the code and I came up with the following procedure:<\/p>\n<p><span style=\"font-family:courier new;color:#3366ff;\">sp_FindInProcedure \u2018Tax_Code\u2019<\/span><\/p>\n<p>.And below are the defenitions for them.<\/p>\n<p><span style=\"font-family:courier new;color:#33cc00;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; <\/span><\/p>\n<p>if exists (select * from sysobjects<br \/>where id = object_id(N\u2018[dbo].[sp_FindColumns]\u2019) and OBJECTPROPERTY(id,<br \/>N\u2018IsProcedure\u2019) = 1)<br \/>drop procedure [dbo].[sp_FindColumns]<br \/>GO<\/p>\n<p>Create Procedure sp_FindColumns<br \/>@sToSearch varchar(255)<br \/>AS<br \/>BEGIN<br \/><span style=\"color:#009900;\">\/************************************************************************************* *<br \/>* sp_FindColumns<br \/>*<br \/>* This stored procedure will look into all table collumns<br \/>* search the string which is given as the only parameter.<br \/>*<br \/>************************************************************************************* * By Asghar Panahy<br \/>* 12-jun-2006<br \/>**************************************************************************************\/ <\/span><\/p>\n<p>SELECT SO.name + \u2018.\u2019 + Sc.name<br \/>FROM SysColumns Sc<br \/>Inner Join SysObjects SO<br \/>on SO.ID = SC.ID<br \/>Where SO.xtype=\u2018U\u2019<br \/>And Sc.Name like \u2018%\u2019 + @sToSearch + \u2019%\u2019<\/p>\n<p>END<\/p>\n<p><span style=\"color:#009900;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/><\/span><br \/>if exists (select * from sysobjects<br \/>where id = object_id(N\u2018[dbo].[sp_FindTables]\u2019) and OBJECTPROPERTY(id,<br \/>N\u2018IsProcedure\u2019) = 1)<br \/>drop procedure [dbo].[sp_FindTables]<br \/>GO<\/p>\n<p>Create Procedure sp_FindTables<br \/>@sToSearch varchar(255)<br \/>AS<br \/>BEGIN<br \/><span style=\"color:#009900;\">\/************************************************************************************* *<br \/>* sp_FindTables<br \/>*<br \/>* This stored procedure will look into all table names<br \/>* search the string which is given as the only parameter.<br \/>*<br \/>************************************************************************************* * By Asghar Panahy<br \/>* 12-jun-2006<br \/>*************************************************************************************<\/span><span style=\"color:#009900;\">*\/ <\/span><br \/>SELECT Distinct So.Name<br \/>FROM SysColumns Sc<br \/>Inner Join SysObjects SO on SO.ID = SC.ID<br \/>Where So.xtype= \u2019U\u2019<br \/>And SO.Name like \u2018%\u2019 + @sToSearch + \u2018%\u2019<\/p>\n<p>END<\/p>\n<p>if exists(select * from sysobjects where id = object_id (N\u2018[dbo].[sp_FindInProcedure]\u2019) and<br \/>OBJECTPROPERTY(id, N\u2018IsProcedure\u2019) = 1) color=&#8221;#0000ff\u201d<br \/>drop procedure [dbo].[sp_FindInProcedure]<br \/>GO<\/p>\n<p>Create Procedure sp_FindInProcedure<br \/>@sToSearch<br \/>varchar(255)<br \/>AS<\/p>\n<p>BEGIN<br \/><span style=\"color:#009900;\">\/************************************************************************************* *<br \/>* sp_FindInProcedure<br \/>*<br \/>* This stored procedure will look into all stored procedures defenitions to<br \/>* search the string which is given as the only parameter.<br \/>*<br \/>************************************************************************************* * By Asghar Panahy<br \/>* 12-jun-2006<br \/>**************************************************************************************\/<\/span><\/p>\n<p>SELECT so.name, sc.text<br \/>FROM syscomments sc<br \/>JOIN sysobjects so ON sc.id = so.id<br \/>WHERE so.xtype = \u2018P\u2019<br \/>And sc.Text like \u2019%\u2019 + @sToSearch + \u2018%\u2019<br \/>Order By so.Name Asc<\/p>\n<p>END<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working with some big databases having similar tables for different group.It seems to be poluted by different parties and have no administrator. Some developersput their application initials as a prefix of the table names and as a result wegot several Contact tables and several Items each with diffeent layouts. When I am dealing &hellip; <a href=\"http:\/\/www.panahy.nl\/index.php\/2006\/06\/06\/searching-tables-and-columns\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Searching tables and columns&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[97],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/www.panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"I am working with some big databases having similar tables for different group.It seems to be poluted by different parties and have no administrator. Some developersput their application initials as a prefix of the table names and as a result wegot several Contact tables and several Items each with diffeent layouts. When I am dealing&hellip;","_links":{"self":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/120"}],"collection":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":0,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}