{"id":2835,"date":"2025-05-06T10:04:18","date_gmt":"2025-05-06T14:04:18","guid":{"rendered":"https:\/\/csimmons.dev\/blog\/?p=2835"},"modified":"2025-05-06T10:04:18","modified_gmt":"2025-05-06T14:04:18","slug":"find-a-column-in-a-table-by-column-name-mssql","status":"publish","type":"post","link":"https:\/\/csimmons.dev\/blog\/2025\/05\/find-a-column-in-a-table-by-column-name-mssql\/","title":{"rendered":"Find a column in a table by column name (MSSQL)"},"content":{"rendered":"<p>A SQL script was provided for me to run on behalf of a workgroup yesterday. The script was supposed to run a simple update on some problematic records. The database is one that is in use by many different clients with varying degrees of customization. The script failed with this error message:<\/p>\n<p><code>Msg 207, Level 16, State 1, Line 1 Invalid column name 'incidentId'.<\/code><\/p>\n<p>It&#8217;s a pretty straightforward error. The column &#8216;incidentId&#8217; doesn&#8217;t exist in the table that the script was trying to update.<\/p>\n<p>Since I am unfamiliar with the database I wanted to provide the workgroup any potentially useful information they could take to the developer such as tables which did contain the column or something close. Below are the queries I used.<\/p>\n<p><strong>Option 1<\/strong><code><br \/>\n-- Find all tables containing the column incidentId<br \/>\nSELECT TABLE_NAME<br \/>\nFROM INFORMATION_SCHEMA.COLUMNS<br \/>\nWHERE COLUMN_NAME = 'incidentId';<br \/>\n<\/code><\/p>\n<p><strong>Option 2<\/strong><code><br \/>\n-- Find all tables and columns with a column name that contains 'incident'<br \/>\nSELECT TABLE_NAME, COLUMN_NAME<br \/>\nFROM INFORMATION_SCHEMA.COLUMNS<br \/>\nWHERE COLUMN_NAME LIKE '%incident%'<br \/>\nORDER BY COLUMN_NAME;<br \/>\n<\/code><\/p>\n<p>Turned out the update script just had the wrong column name.\u00a0 \ud83d\ude41<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A SQL script was provided for me to run on behalf of a workgroup yesterday. The script was supposed to run a simple update on some problematic records. The database is one that is in use by many different clients with varying degrees of customization. The script failed with this error message: Msg 207, Level &#8230; <a title=\"Find a column in a table by column name (MSSQL)\" class=\"read-more\" href=\"https:\/\/csimmons.dev\/blog\/2025\/05\/find-a-column-in-a-table-by-column-name-mssql\/\" aria-label=\"Read more about Find a column in a table by column name (MSSQL)\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"_cloudinary_featured_overwrite":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[68],"tags":[12,16],"class_list":["post-2835","post","type-post","status-publish","format-standard","hentry","category-developer","tag-developer","tag-sql"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pbVg43-JJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/posts\/2835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/comments?post=2835"}],"version-history":[{"count":0,"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/posts\/2835\/revisions"}],"wp:attachment":[{"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/media?parent=2835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/categories?post=2835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csimmons.dev\/blog\/wp-json\/wp\/v2\/tags?post=2835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}