How to use Assignment Operator in SQL Server - SQL Server / TSQL Tutorial Part 128

The equal ( = ) sign is used in TSQL as assignment operator. You will be using assignment operator many time when you write your TSQL Code.

1) Assign Value to Variable
Assignment Operator can be used to set the value for variable. You can simply assign static value to variable by using Assignment Operator or value returned by query.

Declare @i int
SET @i=2000
Print @i

Declare @Cnt int
SET @Cnt=(Select COUNT(*) from dbo.Customer)
Print @Cnt

Declare @CntRow int
Select @CntRow=Count(*) from dbo.Customer
Print @CntRow


2) Let's say you have dbo.Customer table and you want to add a static value column, You can use Assignment Operator to assign value to newly added column. In below example I am adding Region column by assigning 'North America'.


--Create Customer Table
Create table dbo.Customer
 (Id int,
  FName VARCHAR(50),
  LName VARCHAR(50),
  CountryShortName CHAR(2),
  Age tinyint)
GO
--Insert Rows in dbo.Customer Table
insert into dbo.Customer
Values (
1,'Raza','M','PK',10),
(2,'Rita','John','US',12),
(3,'Sukhi','Singh',Null,25),
(4,'James','Smith','CA',60),
(5,'Robert','Ladson','US',54),
(6,'Alice','John','US',87)



Also we can use the assignment operator for Alias. I have used CustomerID alias for ID column by using assignment operator.

Select 
CustomerId=Id,
FName,
LName,
CountryShortName,
Age,Region='North America' 
From dbo.Customer


What is Assignment Operator in SQL Server and How to use Assignment Operator - SQL Server / TSQL Tutorial


10 comments:

  1. The branch of accounting is a huge one, and there are mainly three types of offices in accounting. They are financial accounting, cost accounting and management accounting. Most people have a feeling that accounting is primarily a subject playing with numbers. Assignment Help London

    ReplyDelete
  2. I don't understand coding so I thought why not write my assignment a little differently.So I said to the teacher, I don't understand this coding assignment, so my teacher has to work on Business Management Dissertation, so my teacher gave me this assignment.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. BookMyEssay is one of the best writing agency, when it comes to providing Custom Coursework Writing Services with high quality work at an affordable cost.

    ReplyDelete
  5. Assignment operators in SQL Server can be a great help when it comes to professional essay writers for hire. With an assignment operator, you can easily insert data into your essay without having to retype it all. This can save you a lot of time and frustration.

    ReplyDelete
  6. This is a great article on the client reporting dashboard-venuearc in SQL Server. I didn't know that this was a thing, but it makes perfect sense. This will definitely help me with my event management.

    ReplyDelete
  7. As a college student who deals with this issue frequently, I completely agree with you. Even though you say that all students value their time, I completely understand how difficult it might be to submit a project on time through best assignment help service. The only way to finish a project on time is to use online assignment help.

    ReplyDelete
  8. If you want to use this coding language first learn it and use it very effectively with assignment or take my online class.

    ReplyDelete
  9. Pay Someone To Do Your Class Online For Me? Take Your Class Online offers best online class help! Just say take my online class for me. We offer do my online class help!

    ReplyDelete