snipt

Ctrl+h for KB shortcuts

SQL

Add a column in a MSSQL table

1
2
3
ALTER TABLE [DBName].[dbo].[TableName]
ADD ColumnName int NOT NULL DEFAULT(DefaultValuefortheColumn)
GO
https://snipt.net/embed/7778fc4ef5c1db45b3c8c428d879c163/
/raw/7778fc4ef5c1db45b3c8c428d879c163/
7778fc4ef5c1db45b3c8c428d879c163
sql
SQL
3
2019-07-15T05:37:50
True
False
False
/api/public/snipt/43198/
add-a-column-in-a-mssql-table
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">ALTER</span> <span class="k">TABLE</span> <span class="p">[</span><span class="n">DBName</span><span class="p">].[</span><span class="n">dbo</span><span class="p">].[</span><span class="n">TableName</span><span class="p">]</span> </span><span id="L-2"><a name="L-2"></a><span class="k">ADD</span> <span class="n">ColumnName</span> <span class="nb">int</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">DEFAULT</span><span class="p">(</span><span class="n">DefaultValuefortheColumn</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a><span class="k">GO</span> </span></pre></div> </td></tr></table>
mssql

SQL

Rename a column in a MSSQL table

EXEC sp_rename 'dbo.tableName.OldColumnName', 'NewColumnName', 'COLUMN'
https://snipt.net/embed/0250de6067f437f5e11a9cd7ee610426/
/raw/0250de6067f437f5e11a9cd7ee610426/
0250de6067f437f5e11a9cd7ee610426
sql
SQL
1
2019-07-12T01:41:17
True
False
False
/api/public/snipt/43197/
rename-a-column-in-a-mssql-table
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">EXEC</span> <span class="n">sp_rename</span> <span class="s1">&#39;dbo.tableName.OldColumnName&#39;</span><span class="p">,</span> <span class="s1">&#39;NewColumnName&#39;</span><span class="p">,</span> <span class="s1">&#39;COLUMN&#39;</span> </span></pre></div> </td></tr></table>
mssql