Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding column with NULL values to temp table

Tags:

sql

sql-server

I am trying to create a temp table with values from an existing table. I would like the temp table to have an additional column (phone), which does not exist from the permanent table. All values in this column should be NULL. Not sure how to do, but here is my existing query:

SELECT DISTINCT UserName, FirstName, LastName INTO ##TempTable 
FROM (
      SELECT DISTINCT Username, FirstName, LastName 
          FROM PermanentTable
)  data 
like image 511
PixelPaul Avatar asked Nov 17 '25 04:11

PixelPaul


1 Answers

You need to give the column a value, but you don't need a subquery:

SELECT DISTINCT UserName, FirstName, LastName, NULL as phone
INTO ##TempTable 
FROM PermanentTable;

In SQL Server, the default type for NULL is an int. It is more reasonable to store a phone number as a string, so this is perhaps better:

SELECT DISTINCT UserName, FirstName, LastName,
       CAST(NULL as VARCHAR(255)) as phone
INTO ##TempTable 
FROM PermanentTable;
like image 117
Gordon Linoff Avatar answered Nov 18 '25 20:11

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!