<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Diário de um DBA &#187; tabelas</title>
	<atom:link href="http://silasmendes.com/dba/tag/tabelas/feed" rel="self" type="application/rss+xml" />
	<link>http://silasmendes.com/dba</link>
	<description>Tecnologia &#38; Banco de Dados</description>
	<lastBuildDate>Sun, 15 Apr 2012 12:20:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Verificando a existência de objetos</title>
		<link>http://silasmendes.com/dba/metadados-verificando-a-existencia-de-objetos</link>
		<comments>http://silasmendes.com/dba/metadados-verificando-a-existencia-de-objetos#comments</comments>
		<pubDate>Fri, 21 Aug 2009 14:17:22 +0000</pubDate>
		<dc:creator>Silas Mendes</dc:creator>
				<category><![CDATA[Dicas]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>
		<category><![CDATA[SQL SERVER 2008 R2]]></category>
		<category><![CDATA[SQL SERVER 7]]></category>
		<category><![CDATA[colunas]]></category>
		<category><![CDATA[information_schema]]></category>
		<category><![CDATA[metadados]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[syscolumns]]></category>
		<category><![CDATA[sysobjects]]></category>
		<category><![CDATA[tabelas]]></category>

		<guid isPermaLink="false">http://silasmendes.com/dba/?p=230</guid>
		<description><![CDATA[Uma necessidade comum de muitos desenvolvedores é verificar a existência de objetos no banco de dados. Veja nesse artigo como fazer isto, consultando os metadados do SQL Server. <a href="http://silasmendes.com/dba/metadados-verificando-a-existencia-de-objetos">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">Uma necessidade comum de muitos desenvolvedores é verificar a existência de objetos no banco de dados. Então vou fazer aqui uma rapidinha com exemplos bem básicos, pras situações mais comuns, vamos lá:</p>
<p class="MsoNormal">A tabela <strong>tb_Pedido</strong> existe no banco?</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">use</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> [meuBanco]</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">GO</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">if</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"><span style="mso-spacerun: yes;"> </span><span style="color: gray;">exists</span> </span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: gray; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">(</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">select</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> <span style="color: gray;">*</span> <span style="color: blue;">from</span> information_schema<span style="color: gray;">.</span>tables <span style="color: blue;">where</span> table_name <span style="color: gray;">=</span> <span style="color: red;">&#8216;tb_pedido&#8217;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-no-proof: yes;">print</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: red;">&#8216;Tabela existe&#8217;</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-no-proof: yes;">else</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-no-proof: yes;">print</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: red;">&#8216;Tabela não existe&#8217;</span></span></p>
<p class="MsoNormal"> </p>
</blockquote>
<p class="MsoNormal">Quais tabelas no banco possuem a coluna <strong>cod_cliente</strong>?</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">use</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> [meuBanco]</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">GO</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">select</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> table_name<span style="color: gray;">,</span> data_type </span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">from</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> information_schema<span style="color: gray;">.</span>columns </span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">where</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> column_name <span style="color: gray;">=</span> <span style="color: red;">&#8216;cod_cliente&#8217;</span></span></p>
</blockquote>
<p class="MsoNormal"><span style="mso-ansi-language: EN-US;" lang="EN-US"> </span></p>
<p class="MsoNormal">A coluna <strong>cod_cliente</strong> existe na tabela <strong>tb_Pedido</strong>? Se não existe, adicionar:</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-no-proof: yes;">use</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> [meuBanco]</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">GO</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">if</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"><span style="mso-spacerun: yes;"> </span><span style="color: gray;">exists</span> </span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: gray; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">(</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">select</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> <span style="color: gray;">*</span> <span style="color: blue;">from</span> information_schema<span style="color: gray;">.</span>columns </span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US">where</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"> table_name <span style="color: gray;">=</span> <span style="color: red;">&#8216;tb_pedido&#8217;</span> <span style="color: gray;">and</span> column_name <span style="color: gray;">=</span> <span style="color: red;">&#8216;cod_cliente&#8217;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-ansi-language: EN-US; mso-no-proof: yes;" lang="EN-US"><span style="mso-tab-count: 1;"> </span></span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-no-proof: yes;">print</span><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: red;">&#8216;Coluna existe.&#8217;</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 10.0pt; font-family: &quot;Courier New&quot;; color: blue; mso-no-proof: yes;">else</span></p>
<p class="MsoNormal"><span style="font-size: 10.0pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="color: blue;">alter</span> <span style="color: blue;">table</span> tb_pedido <span style="color: blue;">add</span> cod_cliente <span style="color: blue;">int</span></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size: 10.0pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="color: blue;"><br />
</span></span></p>
<p class="MsoNormal">Observe que nos três exemplos acima, utilizamos as seguintes views de metadados:</p>
<ul>
<li><strong>information_schema.tables</strong> – que apresenta diversas informações sobre as tabelas de um banco.</li>
<li><strong>information_schema.columns</strong> – que apresenta informações sobre as colunas, das tabelas de um banco.</li>
</ul>
<p class="MsoNormal">Ok, mas afinal o que são <strong>METADADOS</strong>?</p>
<p class="MsoNormal">A definição mais comum é: <strong>informação sobre os dados</strong>. Esta definição não é muito amigável, eu sei, mas o que você precisa saber é que todos os objetos que você cria no banco, como tabelas, procedures, índices, etc, tem suas informações armazenadas em <strong>tabelas de sistema</strong> do SQL Server e as views utilizadas neste post buscam esses dados nas tabelas de sistema.</p>
<p class="MsoNormal">Comentaremos mais sobre metadados em outro posts, mas se você deseja se aprofundar no assunto, dê uma estudada neste <a href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" target="_blank">link</a>.</p>
<p class="MsoNormal">Até +</p>
]]></content:encoded>
			<wfw:commentRss>http://silasmendes.com/dba/metadados-verificando-a-existencia-de-objetos/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

