<?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>Sam Beauvois &#187; Sql Server</title>
	<atom:link href="http://www.sambeauvois.be/blog/category/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sambeauvois.be/blog</link>
	<description>general dev, .net and other stuff</description>
	<lastBuildDate>Tue, 27 Jul 2010 07:23:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SqlServer : Saving changes is not permitted</title>
		<link>http://www.sambeauvois.be/blog/2010/07/sqlserver-saving-changes-is-not-permitted/</link>
		<comments>http://www.sambeauvois.be/blog/2010/07/sqlserver-saving-changes-is-not-permitted/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 07:22:51 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[Reminders]]></category>
		<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=491</guid>
		<description><![CDATA[With SqlServer, when you want to change the structure of a table, you may encounter the following warning message :
&#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can&#8217;t be re-created or enabled the option Prevent [...]]]></description>
			<content:encoded><![CDATA[<p>With SqlServer, when you want to change the structure of a table, you may encounter the following warning message :</p>
<blockquote><p>&#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can&#8217;t be re-created or enabled the option Prevent saving changes that require the table to be re-created.&#8221;</p></blockquote>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/warn1.png"><img class="aligncenter size-full wp-image-492" title="warn1" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/warn1.png" alt="warn1" width="456" height="367" /></a></p>
<p>To avoid this warning message, go to the &#8220;tools&#8221; menu, then click &#8220;Options&#8221;</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/toolsoptions.png"><img class="aligncenter size-full wp-image-493" title="toolsoptions" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/toolsoptions.png" alt="toolsoptions" width="307" height="266" /></a>Click on the &#8220;Designer&#8221; node</p>
<p style="text-align: center;"><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/Designers.png"><img class="aligncenter size-full wp-image-494" title="Designers" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/Designers.png" alt="Designers" width="386" height="221" /></a></p>
<p>Then  <strong>uncheck </strong>the &#8220;Prevent saving changes that require table re-creation&#8221; checkbox</p>
<p style="text-align: center;"><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/uncheck.png"><img class="aligncenter size-full wp-image-495" title="uncheck" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/07/uncheck.png" alt="uncheck" width="386" height="223" /></a><br />
And you are done, the message box will not appears anymore</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/07/sqlserver-saving-changes-is-not-permitted/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get the list of all stored procedures using a specific table</title>
		<link>http://www.sambeauvois.be/blog/2010/02/how-to-get-the-list-of-all-stored-procedures-using-a-specific-table/</link>
		<comments>http://www.sambeauvois.be/blog/2010/02/how-to-get-the-list-of-all-stored-procedures-using-a-specific-table/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 15:25:25 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=173</guid>
		<description><![CDATA[Big projects with a lot of stored procedures can be a mess to maintain.
If you change one field in a table, you have to check if there is no side effect in the stored procedures using this table.
This &#8220;sp_depends&#8221; system stored procedure can help you finding your stored procedures using the table you modified.
Just create [...]]]></description>
			<content:encoded><![CDATA[<p>Big projects with a lot of stored procedures can be a mess to maintain.</p>
<p>If you change one field in a table, you have to check if there is no side effect in the stored procedures using this table.</p>
<p>This &#8220;sp_depends&#8221; system stored procedure can help you finding your stored procedures using the table you modified.</p>
<p>Just create a new query, type</p>
<pre class="brush: sql;">

EXEC sp_depends @objname='tableName'
</pre>
<p>(replace &#8220;tableName&#8221; by the name of your table)</p>
<p>then execute.</p>
<p>result example :</p>
<div id="attachment_175" class="wp-caption alignleft" style="width: 457px"><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/02/20100202_SP_depends.png"><img class="size-full wp-image-175" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/02/20100202_SP_depends.png" alt="example result" width="447" height="809" /></a><p class="wp-caption-text">example result</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/02/how-to-get-the-list-of-all-stored-procedures-using-a-specific-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use the undocumented stored procedure sp_msforeachtable to add a column to all tables in a database ?</title>
		<link>http://www.sambeauvois.be/blog/2010/01/how-to-use-the-undocumented-stored-procedure-sp_msforeachtable-to-add-a-column-to-all-tables-in-a-database/</link>
		<comments>http://www.sambeauvois.be/blog/2010/01/how-to-use-the-undocumented-stored-procedure-sp_msforeachtable-to-add-a-column-to-all-tables-in-a-database/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 20:13:27 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Tsql]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=169</guid>
		<description><![CDATA[There are two examples, in both I add a column to each tables except the spnet membership tables.
Example :
Add the &#8220;CreatedDate&#8221; column to all tables :


print 'Add [CreatedDate] Column for each user table'

EXEC sp_msforeachtable
 '
-- add column
exec(''
 declare @tableName as nvarchar(max)
 set @tableName = ''''_?_''''

 IF (CHARINDEX(''''aspnet_'''',@tableName) = 0) -- no aspnet membership
 BEGIN
 IF [...]]]></description>
			<content:encoded><![CDATA[<p>There are two examples, in both I add a column to each tables except the spnet membership tables.</p>
<p>Example :</p>
<p>Add the &#8220;CreatedDate&#8221; column to all tables :</p>
<pre class="brush: sql;">

print 'Add [CreatedDate] Column for each user table'

EXEC sp_msforeachtable
 '
-- add column
exec(''
 declare @tableName as nvarchar(max)
 set @tableName = ''''_?_''''

 IF (CHARINDEX(''''aspnet_'''',@tableName) = 0) -- no aspnet membership
 BEGIN
 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''''?'''') AND type in (N''''U''''))
 BEGIN
 IF columnproperty(object_id(''''?''''), ''''CreatedDate'''', ''''ColumnId'''') is null
 BEGIN
 ALTER TABLE ? ADD [CreatedDate] datetime NOT NULL
 END
 END
 END
'')

' ;
print ' [CreatedDate] Column is created for each user table'
</pre>
<p>Example 2 :</p>
<p>Add the &#8220;CreatedUserID&#8221; column to all tables and enable constraints. For the example, the reference use the aspnet_Users table</p>
<pre class="brush: sql;">
print 'Add [CreatedUserID] Column for each user table'

EXEC sp_msforeachtable
 '
-- add column
exec(''

 declare @tableName as nvarchar(max)
 set @tableName = ''''_?_''''

 IF (CHARINDEX(''''aspnet_'''',@tableName) = 0) -- no aspnet membership
 BEGIN
 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''''?'''') AND type in (N''''U''''))
 BEGIN
 IF columnproperty(object_id(''''?''''), ''''CreatedUserID'''', ''''ColumnId'''') is null
 BEGIN
 ALTER TABLE ? ADD [CreatedUserID] uniqueidentifier NOT NULL
 END
 END
 END
'')
-- add constraint

 declare @constraintName as nvarchar(max)
 set @constraintName = ''FK_?_CreatedUserID''
 IF (CHARINDEX(''aspnet_'',@constraintName) = 0) -- no aspnet membership
 BEGIN
 set @constraintName = REPLACE(@constraintName,''[dbo].['','''')
 set @constraintName = REPLACE(@constraintName,'']'','''')
 IF NOT EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N''''+@constraintName+'''') AND type = (N''F''))
 BEGIN
 exec(''
 ALTER TABLE ? ADD CONSTRAINT [''+@constraintName+''] FOREIGN KEY([CreatedUserID])
 REFERENCES [aspnet_Users]([UserID])
 '')
 END
 END

' ;
print ' [CreatedUserID] Column is created for each user table'
</pre>
<p>The difficulty is to add the right numbers of  &#8216; .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/01/how-to-use-the-undocumented-stored-procedure-sp_msforeachtable-to-add-a-column-to-all-tables-in-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zeros and strings manipulation in Tsql</title>
		<link>http://www.sambeauvois.be/blog/2010/01/zeros-and-strings-in-tsql/</link>
		<comments>http://www.sambeauvois.be/blog/2010/01/zeros-and-strings-in-tsql/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 12:56:52 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Tsql]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=161</guid>
		<description><![CDATA[How do I convert an int to a zero padded string in T-SQL?
Let’s say I have an int with the value of 1.
How can I convert that int to a zero padded string, such as &#8220;00000001&#8243;?

DECLARE @iVal int
SET @iVal = 1
select REPLACE(STR(@iVal,8,0),' ','0')

result is &#8220;00000001&#8243;
And the inverse :
Let&#8217;s say you have a string &#8220;0000000120345FER&#8221; and [...]]]></description>
			<content:encoded><![CDATA[<p>How do I convert an int to a zero padded string in T-SQL?</p>
<p>Let’s say I have an int with the value of 1.<br />
How can I convert that int to a zero padded string, such as &#8220;00000001&#8243;?</p>
<pre class="brush: sql;">
DECLARE @iVal int
SET @iVal = 1
select REPLACE(STR(@iVal,8,0),' ','0')
</pre>
<p>result is &#8220;00000001&#8243;</p>
<p>And the inverse :</p>
<p>Let&#8217;s say you have a string &#8220;0000000120345FER&#8221; and you want to remove the leading zeros</p>
<pre class="brush: sql;">
DECLARE @iVal nvarchar(max)
set @iVal = '0000000120345FER'

DECLARE @iVal2 nvarchar(max)
set @iVal2= SUBSTRING(@iVal, PATINDEX('%[^0]%', @iVal+'.'), LEN(@iVal))

print @iVal2
</pre>
<p>result is &#8220;120345FER&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/01/zeros-and-strings-in-tsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
