<?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; Tsql</title>
	<atom:link href="http://www.sambeauvois.be/blog/category/tsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sambeauvois.be/blog</link>
	<description>general dev, .net and other stuff</description>
	<lastBuildDate>Wed, 08 Sep 2010 12:25: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>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>
