A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations

Data assigning and Data retrival can not be used in a same statement.

/*Creating a table*/
If OBJECT_ID('Tb_Sample1') Is Null
Create Table Tb_Sample1
(
Id Int Identity(1,1),
Col1 Varchar(10),
Col2 DateTime
)
Go


/*Inserting one record*/
Insert Tb_Sample1(Col1, Col2) Values('SQL Server',CURRENT_TIMESTAMP)
Go


/*Using Data assigning and Data retrival in same statement*/
Declare @Column1 Varchar(10)
select Id, Col2,@Column1 = Col1 from Tb_Sample1
Select @Column1


Id, Col2 - is the retrival part
@Column1 = Col1 - is the assigning data to variable, These both operations can not be used in a single statement

It'll throw an error...
Msg 141, Level 15, State 1, Line 2
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete