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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With