Skip to content

Instantly share code, notes, and snippets.

@nosajhpled
Created August 31, 2018 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nosajhpled/ac7711c30c5b05a5769212a2a3c125ff to your computer and use it in GitHub Desktop.
Save nosajhpled/ac7711c30c5b05a5769212a2a3c125ff to your computer and use it in GitHub Desktop.
Two ways to fake a SQL table [SQL]
The first way is to create a temporary table and insert values into it.
DECLARE @declareTable TABLE (seq int, name VARCHAR(5) ) ;
INSERT INTO @declareTable (seq,name) values
(1,'one'),
(2,'two'),
(3,'three')
select * from @declareTable as dt;
The second way is to create the values inside the select statement.
select
*
from
(values
(1,'one'),
(2,'two'),
(3,'three')
) as tempTable (seq,name);
This will allow you to join a larger table to a temporary table using multiple values. This could also replace values used in “in”.
Putting it together:
DECLARE @declareTable TABLE (seq int, name VARCHAR(5) ) ;
INSERT INTO @declareTable (seq,name) values
(1,'one'),
(2,'two'),
(3,'three')
select
*
from
(values
(1,'one'),
(2,'two'),
(3,'three')
) as tempTable (seq,name)
join
@declareTable as dt on dt.seq = tempTable.seq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment