sql table variables in select query code example
Example 1: select into table variable
DECLARE @userData TABLE(
name varchar(30) NOT NULL,
oldlocation varchar(30) NOT NULL
);
INSERT INTO @userData (name, oldlocation)
SELECT name, location FROM myTable
INNER JOIN otherTable ON ...
WHERE age > 30;
Example 2: variables in select statement in sql server
USE AdventureWorks2014;
GO
DECLARE @MyVariable int;
SET @MyVariable = 1;
GO
SELECT BusinessEntityID, NationalIDNumber, JobTitle
FROM HumanResources.Employee
WHERE BusinessEntityID = @MyVariable;