Understanding SQL syntax

Bill Greer

I'm a C# developer trying to become more familiar with SQL Server stored procedures.

I'm a little confused as to why the syntax in "A" works and "B" does not work with Set @id. What is happening here that makes "B" require Select instead of Set?

Example A (works)

DECLARE @currDateTime DateTime

SET @currDateTime = GetDate()

SELECT @currDateTime

Example B (does not work)

 DECLARE @id int

 SET @id = ID FROM [MyTable] WHERE [Field1] = 'Test'

Example C (works)

 DECLARE @id int

 SELECT @id = ID 
 FROM [MyTable] 
 WHERE [Field1] = 'Test'
Gordon Linoff

SELECT is a built-in type of SQL clause that runs a query and returns a result-set in the format of a table or it assigns variables to the results from a query.

SET is a clause that sets a variable.

The two are very different. SELECT has various other associated clauses, such as FROM, WHERE and so on; SET does not. SELECT returns values as a result table in its normal usage; SET does not.

Admittedly, both look the same in an expression such as:

set @currDateTime = GetDate();
select @currDateTime = GetDate();

However, it is really a coincidence that the syntax for setting a single value happens to look the same.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related