<?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>Discrete Information &#187; OUTPUT</title>
	<atom:link href="http://www.discreteinformation.com/tag/output/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.discreteinformation.com</link>
	<description>Providing a finite amount of information.</description>
	<lastBuildDate>Sun, 23 Aug 2009 03:42:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using OUTPUT with UPDATE</title>
		<link>http://www.discreteinformation.com/2009/06/using-output-with-update/</link>
		<comments>http://www.discreteinformation.com/2009/06/using-output-with-update/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 03:52:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[OUTPUT]]></category>

		<guid isPermaLink="false">http://www.discreteinformation.com/?p=62</guid>
		<description><![CDATA[As seen in earlier posts, a very useful clause in SQL Server 2005 and beyond is OUTPUT. Here is an example within an UPDATE statement.
Here&#8217;s our table structure:
IF EXISTS (SELECT * FROM sys.tables WHERE name = 'users')
BEGIN
  DROP TABLE [users];
END;

CREATE TABLE [users] (
  [user_id] INT PRIMARY KEY IDENTITY(1,1),
  username VARCHAR(16),
  [password] [...]]]></description>
			<content:encoded><![CDATA[<p>As seen in earlier posts, a very useful clause in SQL Server 2005 and beyond is <code>OUTPUT</code>. Here is an example within an <code>UPDATE</code> statement.</p>
<p>Here&#8217;s our table structure:</p>
<pre>IF EXISTS (SELECT * FROM sys.tables WHERE name = 'users')
BEGIN
  DROP TABLE [users];
END;

CREATE TABLE [users] (
  [user_id] INT PRIMARY KEY IDENTITY(1,1),
  username VARCHAR(16),
  [password] VARCHAR(16),
  email VARCHAR(128)
);</pre>
<p>Here is a <code>changeUserName</code> procedure to update a user&#8217;s username, using an <code>OUTPUT</code> clause:</p>
<pre>
CREATE PROC changeUserName
  @user_ip varchar(15),
  @password varchar(16),
  @username_old varchar(16),
  @username_new varchar(16)
AS

  SET NOCOUNT ON;

  CREATE TABLE #updatedUser(
    user_id INT,
    username_old VARCHAR(16),
    username_new VARCHAR(16));

  UPDATE [users]
    SET [username] = @username_new
  OUTPUT DELETED.user_id, DELETED.username, INSERTED.username
    INTO #updatedUser(user_id, username_old, username_new)
  WHERE [users].username = @username_old
    AND CAST([users].[password] AS VARBINARY(16))
      = CAST(@password AS VARBINARY(16));

  SET NOCOUNT OFF;

  SELECT user_id, username_old, username_new
    FROM #updatedUser;

  DROP TABLE #updatedUser;
</pre>
<p>Here is an example of that procedure in action:</p>
<pre>
EXEC [addUser] @user_ip = '192.168.1.2',
  @username = 'john',
  @password = 'john!password',
  @email = 'john@yahoo.com';

user_id
-----------
1

EXEC [changeUserName]
  @user_ip = '192.168.1.1',
  @username_old = 'john',
  @username_new = 'john_new',
  @password='john!password';

user_id     old_username     new_username
------- ---------------- ----------------
1           john         john_new
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.discreteinformation.com/2009/06/using-output-with-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Forget the OUTPUT Clause</title>
		<link>http://www.discreteinformation.com/2009/05/dont-forget-the-output-clause/</link>
		<comments>http://www.discreteinformation.com/2009/05/dont-forget-the-output-clause/#comments</comments>
		<pubDate>Wed, 27 May 2009 01:22:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[OUTPUT]]></category>

		<guid isPermaLink="false">http://www.discreteinformation.com/?p=44</guid>
		<description><![CDATA[One of the more useful clauses available in SQL Server 2005 and beyond is OUTPUT, but it is rarely seen in use. Here is a quick example of how to use it within an INSERT statement.
Let&#8217;s setup our table structure first:
IF EXISTS (SELECT * FROM sys.tables WHERE name = 'users')
BEGIN
  DROP TABLE [users];
END;

CREATE TABLE [users] [...]]]></description>
			<content:encoded><![CDATA[<p>One of the more useful clauses available in SQL Server 2005 and beyond is <code>OUTPUT</code>, but it is rarely seen in use. Here is a quick example of how to use it within an <code>INSERT</code> statement.</p>
<p>Let&#8217;s setup our table structure first:</p>
<pre>IF EXISTS (SELECT * FROM sys.tables WHERE name = 'users')
BEGIN
  DROP TABLE [users];
END;

CREATE TABLE [users] (
  [user_id] INT PRIMARY KEY IDENTITY(1,1),
  username VARCHAR(16),
  [password] VARCHAR(16),
  email VARCHAR(128)
);

IF EXISTS (SELECT * FROM sys.tables WHERE name = 'login_activity')
BEGIN
  DROP TABLE [login_activity];
END;

CREATE TABLE [login_activity] (
  user_id INT,
  user_ip VARCHAR(15),
  login_username VARCHAR(16),
  login_success BIT,
  login_time DATETIME
);</pre>
<p>Let&#8217;s assume we want an <code>addUser</code> procedure to create a user, log in said user, and return the new user&#8217;s <code>user_id</code>. Here is how we could do that with the <code>OUTPUT</code> clause:</p>
<pre>
CREATE PROC addUser
  @user_ip VARCHAR(15),
  @username VARCHAR(16),
  @password VARCHAR(16),
  @email VARCHAR(128)
AS

SET NOCOUNT ON;

CREATE TABLE #new_user(user_id INT);

INSERT INTO users (username, [password], email)
OUTPUT inserted.[user_id] INTO #new_user(user_id)
VALUES (@username, @password, @email);

INSERT INTO login_activity(user_id, login_ip, login_username,
  login_success, login_time)
SELECT user_id, @username, @user_ip, 1, CURRENT_TIMESTAMP
FROM #new_user;

SET NOCOUNT OFF;

SELECT user_id
FROM new_user;
</pre>
<pre>
EXEC [addUser] @user_ip = '192.168.1.1',
  @username = 'bob',
  @password = 'bob!password',
  @email = 'bob@gmail.com';

user_id
-----------
1

EXEC [addUser] @user_ip = '192.168.1.2',
  @username = 'john',
  @password = 'john!password',
  @email = 'john@yahoo.com';

user_id
-----------
2
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.discreteinformation.com/2009/05/dont-forget-the-output-clause/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
