<?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>Serge&#039;s Technology View &#187; sql</title>
	<atom:link href="http://blog.dragonsoft.us/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dragonsoft.us</link>
	<description>Talk about Technologies, Software Architecture and Management</description>
	<lastBuildDate>Tue, 31 Jan 2012 01:43:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tip: Get list of columns for the table</title>
		<link>http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/</link>
		<comments>http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/#comments</comments>
		<pubDate>Mon, 12 May 2008 20:26:47 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[list of columns]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[table]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/?p=180</guid>
		<description><![CDATA[When you dealing with dynamic SQL or unknown table structure, SQL meta data could help recover the missing information. There are two main system tables which give you such information: dbo.sysobjects dbo.syscolumns Example below shows how to gather column list &#8230; <a href="http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When you dealing with dynamic SQL or unknown table structure, SQL meta data could help recover the missing information.</p>
<p>There are two main system tables which give you such information:</p>
<ul>
<li>dbo.sysobjects</li>
<li>dbo.syscolumns</li>
</ul>
<p>Example below shows how to gather column list for specified table.</p>
<pre class="brush: sql; title: ; notranslate">DECLARE
  @TABLENAME varchar(256),
  @COLUMNS varchar(4000)
SET @TABLENAME = 'mytable'
SET @COLUMNS = ''
SELECT @COLUMNS = @COLUMNS + c.name + ', '
FROM syscolumns c
INNER JOIN sysobjects o ON o.id = c.id
WHERE o.name = @TABLENAME AND o.xtype='U'
ORDER BY colid
SET @COLUMNS = SUBSTRING(@COLUMNS, 1, Datalength(@COLUMNS) - 2)
SELECT @COLUMNS</pre>
<p>or if you prefer more generic approach</p>
<pre class="brush: sql; title: ; notranslate">DECLARE
  @TABLENAME varchar(256),
  @COLUMNS varchar(4000)
SET @TABLENAME = 'mytable'
SET @COLUMNS = ''
SET @COLUMNS = ''
SELECT @COLUMNS = @COLUMNS + COLUMN_NAME + ', '
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = @TABLENAME
ORDER BY ORDINAL_POSITION
SET @COLUMNS = SUBSTRING(@COLUMNS, 1, Datalength(@COLUMNS) - 2)
SELECT @COLUMNS</pre>
<p>Result is a coma-separated list of columns in the table in order they appear.</p>
<p><strong>Note</strong>. Check for xtype in the first example could be useful but not required, since you probably do not mix table name with any other object names. Otherwise, it would be necessary to ensure that you are looking at the table and not something else.<br />
Other possible values for this column are:</p>
<ul>
<li><strong>S</strong> &#8211; System tables</li>
<li><strong>U</strong> &#8211; User table</li>
<li><strong>TR</strong> &#8211; Triggers</li>
<li><strong>P</strong> &#8211; Stored procedure</li>
<li><strong>RF</strong> &#8211; Replication filter stored procedure</li>
<li><strong>X</strong> &#8211; Extended stored procedure</li>
<li><strong>V</strong> &#8211; View</li>
<li><strong>TF</strong> &#8211; Functions</li>
<li><strong>C</strong> &#8211; CHECK constraint</li>
<li><strong>D</strong> &#8211; DEFAULT constraint</li>
<li><strong>F</strong> &#8211; FOREIGN KEY constraint</li>
<li><strong>PK</strong> &#8211; PRIMARY KEY constraint (type is K)</li>
<li><strong>UQ</strong> &#8211; UNIQUE constraint (type is K)</li>
<li><strong>L</strong> &#8211; Log</li>
</ul>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL date operations: Last 90 days, Get Date from DateTime&#8230;</title>
		<link>http://blog.dragonsoft.us/2008/05/12/sql-date-operations-last-90-days-get-date-from-datetime/</link>
		<comments>http://blog.dragonsoft.us/2008/05/12/sql-date-operations-last-90-days-get-date-from-datetime/#comments</comments>
		<pubDate>Mon, 12 May 2008 20:14:16 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[date manipulations]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/?p=179</guid>
		<description><![CDATA[I am continuing putting some common examples of operations for Microsoft SQL Server. In addition to some of the Date related code snippets published before, there are few more today: Copyright &#169; 2012 Serge&#039;s Technology View. This Feed is for &#8230; <a href="http://blog.dragonsoft.us/2008/05/12/sql-date-operations-last-90-days-get-date-from-datetime/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I am continuing putting some common examples of operations for Microsoft SQL Server.<br />
In addition to some of the Date related code snippets <a href="http://blog.dragonsoft.us/2008/02/05/first-day-last-day-of-the-month-and-sql/" target="_self">published before</a>, there are few more today:</p>
<pre class="brush: sql; title: ; notranslate">-- Last 90 days:
select DATEADD(day, -90, GETDATE())
-- Get Date portion of DateTime value
select DATEADD(d, 0, DATEDIFF(d, 0, getdate()))
</pre>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2008/05/12/sql-date-operations-last-90-days-get-date-from-datetime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File management from inside SQL code</title>
		<link>http://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/</link>
		<comments>http://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 20:28:35 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[file management]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/</guid>
		<description><![CDATA[Writing a log, dumping data, looking for file to be used in Bulk insert&#8230; There are many situations when you would want to have access to files from inside your SQL code on Microsoft SQL Server. Did you know that you &#8230; <a href="http://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Writing a log, dumping data, looking for file to be used in Bulk insert&#8230;</p>
<p>There are many situations when you would want to have access to files from inside your SQL code on Microsoft SQL Server.</p>
<p>Did you know that you actually can do this? No? Check below for code snippets to perform various operations on files. It is presented in form of the functions but you are actually not limited to that</p>
<p><strong>Prerequisites and assumptions</strong></p>
<ol>
<li>Your script should have sufficient rights to perform required access to files (not necessarily local).</li>
<li><em>Scripting.FileSystemObject </em>should be present at your SQL Server location and accessible.</li>
</ol>
<p><strong>Check if file exists</strong><br />
<span id="more-144"></span></p>
<pre class="brush: sql; title: ; notranslate">
CREATE FUNCTION [dbo].[ufn_IsFileExists] (
@FilePath VARCHAR(255)
)
RETURNS INT
AS
BEGIN
DECLARE
  @objFileSystem int,
  @hr int,
  @i int
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @objFileSystem out
IF @HR = 0
BEGIN
  EXEC sp_OAMethod @objFileSystem, 'FileExists', @i out, @FilePath
  EXEC sp_OADestroy @objFileSystem
END
ELSE
BEGIN
  SET @i = -1
END
RETURN @i
END
</pre>
<p><strong>Write string into file</strong></p>
<pre class="brush: sql; title: ; notranslate">
CREATE FUNCTION [dbo].[ufn_WriteStringToFile] (
  @CreateFile int,
  @FilePath varchar(500),
  @String varchar(4000) )
RETURNS varchar(200)
AS
BEGIN
DECLARE
  @objFileSystem int,
  @objTextStream int,
  @objErrorObject int,
  @strErrorMsg varchar(1000),
  @Command varchar(1000),
  @HR int,
  @fileAndPath varchar(80)
SET @strErrorMsg = 'opening the File System Object'
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUT
IF @HR = 0
BEGIN
  SET @objErrorObject = @objFileSystem
  IF (@CreateFile = 1)
  BEGIN
SET @strErrorMsg= 'Creating file &quot;' + @FilePath + '&quot;'
EXEC @hr = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 2, True
  END
  ELSE
  BEGIN
  SET @strErrorMsg = 'Opening file &quot;' + @FilePath + '&quot;'
  EXEC @hr = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 8, True
  END
END
IF @HR = 0
BEGIN
  SET @objErrorObject = @objTextStream
  SET @strErrorMsg = 'Writing to the file &quot;' + @FilePath + '&quot;'
  EXEC @hr = sp_OAMethod @objTextStream, 'WriteLine', Null, @String
END
IF @HR=0
BEGIN
  SET @objErrorObject = @objTextStream
  SET @strErrorMsg = 'Closing the file &quot;' + @FileAndPath + '&quot;'
  EXEC @hr = sp_OAMethod @objTextStream, 'Close'
END
IF @HR &lt;&gt; 0
BEGIN
  DECLARE
  @Source varchar(255),
  @Description varchar(255),
  @Helpfile varchar(255),
  @HelpID int
EXEC sp_OAGetErrorInfo @objErrorObject, @Source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT
  SET @strErrorMsg = 'Error: ' + coalesce(@strErrorMsg, 'Unknown') + ', ' + coalesce(@Description, '')
END
EXEC sp_OADestroy @objTextStream
RETURN @strErrorMsg
END
</pre>
<p><strong>Read File As Table</strong></p>
<pre class="brush: sql; title: ; notranslate">
CREATE FUNCTION [dbo].[ufn_ReadFileAsTable] (
@FilePath VARCHAR(255)
)
RETURNS @File TABLE ([LineNo] int identity(1,1), [Line] varchar(8000))
AS
BEGIN
DECLARE
@objFileSystem int,
@objTextStream int,
@objErrorObject int,
@strErrorMsg varchar(1000),
  @hr int,
  @String VARCHAR(8000),
@YesOrNo INT
SET @strErrorMsg = 'opening the File System Object'
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @objFileSystem OUT
IF @HR=0
BEGIN
SET @objErrorObject = @objFileSystem
SET @strErrorMsg = 'Opening file &quot;' + @FilePath + '&quot;'
--Open for reading, FormatASCII
EXEC @HR = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 1, False, 0
END
WHILE @HR = 0
BEGIN
  IF @HR=0
  BEGIN
SET @objErrorObject = @objTextStream
SET @strErrorMsg = 'Check if there is more to read in &quot;' + @FilePath + '&quot;'
EXEC @HR = sp_OAGetProperty @objTextStream, 'AtEndOfStream', @YesOrNo OUTPUT
IF @YesOrNo &lt;&gt; 0 BREAK
  END
  IF @HR=0
  BEGIN
SET @objErrorObject = @objTextStream
SET @strErrorMsg = 'Reading from the output file &quot;' + @FilePath + '&quot;'
EXEC @HR = sp_OAMethod @objTextStream, 'Readline', @String OUTPUT
    INSERT INTO @file(line) SELECT @String
  END
END
IF @HR=0
BEGIN
SET @objErrorObject = @objTextStream
SET @strErrorMsg = 'Closing the output file &quot;' + @FilePath + '&quot;'
EXEC @HR = sp_OAMethod @objTextStream, 'Close'
END
IF @hr &lt;&gt; 0
BEGIN
  DECLARE
@Source varchar(255),
@Description varchar(255),
@Helpfile varchar(255),
@HelpID int
EXEC sp_OAGetErrorInfo @objErrorObject, @Source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT
SET @strErrorMsg = 'Error: ' + coalesce(@strErrorMsg, 'Unknown') + ', ' + coalesce(@Description, '')
INSERT INTO @File(line) select @strErrorMsg
END
EXEC sp_OADestroy @objTextStream
-- Fill the table variable with the rows for your result set
RETURN
END
</pre>
<p>Enjoy.</p>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL date operations: First day, Last day of the month&#8230;</title>
		<link>http://blog.dragonsoft.us/2008/02/05/first-day-last-day-of-the-month-and-sql/</link>
		<comments>http://blog.dragonsoft.us/2008/02/05/first-day-last-day-of-the-month-and-sql/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 16:56:14 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[date manipulations]]></category>
		<category><![CDATA[piclens]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<guid isPermaLink="false">http://blog.dragonsoftru.com/2007/01/18/first-day-last-day-of-the-month-and-sql/</guid>
		<description><![CDATA[Did you ever faced a situation when you need quickly calculate some boundaries of the Date value like First Day of the month, Last Day of the month, etc&#8230; in T-SQL? Colegue of mine asked me this question&#8230; WOW! What do &#8230; <a href="http://blog.dragonsoft.us/2008/02/05/first-day-last-day-of-the-month-and-sql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Did you ever faced a situation when you need quickly calculate some boundaries of the Date value like First Day of the month, Last Day of the month, etc&#8230; in T-SQL?</p>
<p>Colegue of mine asked me this question&#8230; WOW! What do you know&#8230;<br />
It is actually very easy if we think about it for a second&#8230;</p>
<p>So lets fun begin&#8230;</p>
<pre class="brush: sql; title: ; notranslate">--First Day of the Current Month:
select DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
-- Last Day of the Current Month:
select DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))
-- Last Day of Prior Month:
select DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
-- First Day of the Current Year:
select DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
-- Last Day of the Current Year:
select DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0))
-- Last Day of Prior Year:
select DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0))
</pre>
<p>And some more&#8230;</p>
<pre class="brush: sql; title: ; notranslate">-- First Day of the Quarter:
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
-- Monday of the Current Week:
select DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)
-- Midnight for the Current Day:
select DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
-- First Monday of the Month:
select DATEADD(wk, DATEDIFF(wk, 0, DATEADD(dd, 6 - DATEPART(day, GETDATE()), GETDATE())), 0)
-- Last 2 full months
select DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0))
</pre>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2008/02/05/first-day-last-day-of-the-month-and-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

