{"id":1060,"date":"2024-02-08T17:54:08","date_gmt":"2024-02-08T22:54:08","guid":{"rendered":"https:\/\/www.oopus.info\/blog\/?p=1060"},"modified":"2024-02-08T18:20:41","modified_gmt":"2024-02-08T23:20:41","slug":"a-sql-procedure-to-query-a-column-in-all-the-tables","status":"publish","type":"post","link":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/","title":{"rendered":"A SQL procedure to query a column in all the tables"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">The procedure to query<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\nCREATE PROCEDURE FindCSourceRecords()\nBEGIN\n  DECLARE done INT DEFAULT FALSE;\n  DECLARE tableName VARCHAR(255);\n  DECLARE cur CURSOR FOR \n    SELECT TABLE_NAME \n    FROM INFORMATION_SCHEMA.COLUMNS \n    WHERE COLUMN_NAME = 'c_source' \n    AND TABLE_SCHEMA = 'cbdb_data'; -- Replace with your database name\n  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;\n\n  OPEN cur;\n\n  read_loop: LOOP\n    FETCH cur INTO tableName;\n    IF done THEN\n      LEAVE read_loop;\n    END IF;\n\n    -- Corrected dynamic SQL to properly execute and check for existence\n    SET @s = CONCAT('SELECT EXISTS (SELECT 1 FROM ', tableName, ' WHERE c_source = 38299) INTO @exists;');\n    PREPARE stmt FROM @s;\n    EXECUTE stmt;\n    DEALLOCATE PREPARE stmt;\n\n    -- Check if the query found a row and then do something with the result\n    IF @exists THEN\n      -- This is a placeholder action; you might want to SELECT the table name or insert it into a log\n      SELECT CONCAT('Found in table: ', tableName) AS Result;\n    END IF;\n\n  END LOOP;\n\n  CLOSE cur;\nEND$$\n\nDELIMITER ;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Call query procedure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CALL FindCSourceRecords();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Remove query procedure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP PROCEDURE IF EXISTS FindCSourceRecords;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The procedure to update<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER $$\n\nCREATE PROCEDURE UpdateCSourceRecords()\nBEGIN\n  DECLARE done INT DEFAULT FALSE;\n  DECLARE tableName VARCHAR(255);\n  DECLARE cur CURSOR FOR \n    SELECT TABLE_NAME \n    FROM INFORMATION_SCHEMA.COLUMNS \n    WHERE COLUMN_NAME = 'c_source' \n    AND TABLE_SCHEMA = 'cbdb_data'; -- Replace with your actual database name\n  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;\n\n  OPEN cur;\n\n  read_loop: LOOP\n    FETCH cur INTO tableName;\n    IF done THEN\n      LEAVE read_loop;\n    END IF;\n\n    -- Dynamic SQL for updating c_source values\n    SET @s = CONCAT('UPDATE ', tableName, ' SET c_source = 9334 WHERE c_source = 38299;');\n    PREPARE stmt FROM @s;\n    EXECUTE stmt;\n    DEALLOCATE PREPARE stmt;\n\n  END LOOP;\n\n  CLOSE cur;\nEND$$\n\nDELIMITER ;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Call update procedure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CALL UpdateCSourceRecords();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Remove update procedure<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP PROCEDURE IF EXISTS UpdateCSourceRecords;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The procedure to query Call query procedure Remove query procedure The procedure to update Call update procedure Remove update procedure<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-computer-science"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>A SQL procedure to query a column in all the tables - Oopus Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A SQL procedure to query a column in all the tables - Oopus Blog\" \/>\n<meta property=\"og:description\" content=\"The procedure to query Call query procedure Remove query procedure The procedure to update Call update procedure Remove update procedure\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"Oopus Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-08T22:54:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-08T23:20:41+00:00\" \/>\n<meta name=\"author\" content=\"oopus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"oopus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\"},\"author\":{\"name\":\"oopus\",\"@id\":\"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9\"},\"headline\":\"A SQL procedure to query a column in all the tables\",\"datePublished\":\"2024-02-08T22:54:08+00:00\",\"dateModified\":\"2024-02-08T23:20:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\"},\"wordCount\":31,\"articleSection\":[\"computer science\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\",\"url\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\",\"name\":\"A SQL procedure to query a column in all the tables - Oopus Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/#website\"},\"datePublished\":\"2024-02-08T22:54:08+00:00\",\"dateModified\":\"2024-02-08T23:20:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.oopus.info\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A SQL procedure to query a column in all the tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.oopus.info\/blog\/#website\",\"url\":\"https:\/\/www.oopus.info\/blog\/\",\"name\":\"Oopus Blog\",\"description\":\"Oopu&#039;s self-recording\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.oopus.info\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9\",\"name\":\"oopus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g\",\"caption\":\"oopus\"},\"sameAs\":[\"http:\/\/www.oopus.info\/blog\"],\"url\":\"https:\/\/www.oopus.info\/blog\/author\/oopus\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A SQL procedure to query a column in all the tables - Oopus Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/","og_locale":"en_US","og_type":"article","og_title":"A SQL procedure to query a column in all the tables - Oopus Blog","og_description":"The procedure to query Call query procedure Remove query procedure The procedure to update Call update procedure Remove update procedure","og_url":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/","og_site_name":"Oopus Blog","article_published_time":"2024-02-08T22:54:08+00:00","article_modified_time":"2024-02-08T23:20:41+00:00","author":"oopus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"oopus","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/#article","isPartOf":{"@id":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/"},"author":{"name":"oopus","@id":"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9"},"headline":"A SQL procedure to query a column in all the tables","datePublished":"2024-02-08T22:54:08+00:00","dateModified":"2024-02-08T23:20:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/"},"wordCount":31,"articleSection":["computer science"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/","url":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/","name":"A SQL procedure to query a column in all the tables - Oopus Blog","isPartOf":{"@id":"https:\/\/www.oopus.info\/blog\/#website"},"datePublished":"2024-02-08T22:54:08+00:00","dateModified":"2024-02-08T23:20:41+00:00","author":{"@id":"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9"},"breadcrumb":{"@id":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.oopus.info\/blog\/2024\/02\/08\/a-sql-procedure-to-query-a-column-in-all-the-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.oopus.info\/blog\/"},{"@type":"ListItem","position":2,"name":"A SQL procedure to query a column in all the tables"}]},{"@type":"WebSite","@id":"https:\/\/www.oopus.info\/blog\/#website","url":"https:\/\/www.oopus.info\/blog\/","name":"Oopus Blog","description":"Oopu&#039;s self-recording","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.oopus.info\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.oopus.info\/blog\/#\/schema\/person\/0491ca0bc5ba1f23b62bb7be621f29c9","name":"oopus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e6169b542ea65ef728fae42c117aa13b4897842f8263161765d1384a2a6e6fd9?s=96&d=mm&r=g","caption":"oopus"},"sameAs":["http:\/\/www.oopus.info\/blog"],"url":"https:\/\/www.oopus.info\/blog\/author\/oopus\/"}]}},"_links":{"self":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":11,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":1077,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/posts\/1060\/revisions\/1077"}],"wp:attachment":[{"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oopus.info\/blog\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}