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;
Just add the name of column that you will insert into TempTable and in inner select just select NULL
something like this
SELECT DISTINCT UserName, FirstName, LastName, Phone INTO ##TempTable
FROM (
SELECT DISTINCT Username, FirstName, LastName, NULL
FROM PermanentTable
) data
Rewrite the query as -
CREATE TABLE ##TempTable(UserName datatype, FirstName datatype, LastName datatype,Phone datatype)
INSERT INTO INTO ##TempTable (UserName, FirstName, LastName,Phone )
SELECT DISTINCT UserName, FirstName, LastName,NULL
FROM (
SELECT DISTINCT Username, FirstName, LastName
FROM PermanentTable
) data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.