Facebook Twitter Sign in | Join
SQL Backup Pro
Getting started with SSIS - Part 1: Introduction to SSIS
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

Community blogs

Your daily dose of "Must Read" blogs from the Community!
Connect your existing Source Control system to SSMS in 5 minutes
Sponsored [Advertise Here]
Learn XSD and XML Schema Collections
beyondrelational.com
This book helps you to learn XML Schema Collections from basic to advanced levels through simple examples and easy to follow walk through labs.

Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

SSRS Tutorial - Getting Started with SQL Server Reporting Services
beyondrelational.com
This tutorial will help you to get started with SQL Server Reporting Services (SSRS)
Loading

Setting value to a variable by using SELECT or SET statement

You can assign value to a variable by using SET or SELECT statement and almost they work the same except that a SELECT statement has the ability to have the source value come from a column within the SELECT statement.

Let's see these examples to clarify the difference between the two statements: 

USE AdventureWorks
GO

DECLARE @Res DateTime
-- Assign value to a variable by using SET statement
SET @Res = GETDATE();
SELECT @Res

SET @Res = 
			(
			SELECT ModifiedDate
			FROM Person.Contact 
			WHERE ContactID = 1
			)
SELECT @Res
--------------------------------------------
--Just this case is show the diff between the SET and SELECT
-- It will cause an error
SET @Res = ModifiedDate FROM Person.Contact WHERE ContactID = 1
SELECT @Res
--------------------------------------------
-- Assign value to a variable by using SELECT statement
SELECT @Res = GETDATE();
SELECT @Res

SELECT @Res = 
			(
			SELECT ModifiedDate 
			FROM Person.Contact 
			WHERE ContactID = 1
			)
SELECT @Res
--------------------------------------------
-- It will success
SELECT @Res = ModifiedDate FROM Person.Contact WHERE ContactID = 1
SELECT @Res
--------------------------------------------

Fadi Ahmad Abdulwahab
248 · 1% · 107
0
Liked
0
Comments



Submit

     

Subscribe to Notifications

Your Comment


Sign Up or Login to post a comment.

Published 03-10-2010 2:40 AM by Fadi Ahmad Abdulwahab
Filed under:

Copyright © Beyondrelational.com