Learn more about SQL Server tools


Tutorials          DBA          Dev          BI          Career          Categories          Events          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Events

DBA    Dev    BI    Categories


SQL Server INSERT command with variables



Variables are used as a means to assign values in code rather than hard coding values.  In simple examples, variables may seem excessive, but they are a good concept to understand as your development skills progress. 


In this example, we are going to slightly change the first example to use variables in the INSERT command rather than hard coded values.

Let's explain what's going on with this code:

-- 1 - Declare variables
DECLARE @FirstName varchar(25)
DECLARE @LastName varchar(25)
DECLARE @PhoneNumber varchar(15)
DECLARE @EmailAddress varchar(25)
DECLARE @Priority int
DECLARE @CreateDate datetime

-- 2 - Initialize variables
SET @FirstName = 'Kris'
SET @LastName = 'Fierce'
SET @PhoneNumber = '222-222-2222'
SET @EmailAddress = 'kris@FierceFishing.com'
SET @Priority = 1
SET @CreateDate = '2011-09-15'

-- 3 - Execute INSERT command
INSERT INTO [dbo].[Customer]

More SQL Server Solutions

Post a comment or let the author know this tip helped you.

All comments are reviewed, so stay on subject or we may delete your comment.

*Name    *Email    Notify for updates 

Note: your email address is not published. Required fields are marked with an asterisk (*)

Get free SQL tips:

*Enter Code refresh code     

Learn more about SQL Server tools