Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Inserting records into TEMPORARY table

Status
Not open for further replies.

questhaven

Programmer
Mar 28, 2001
81
US
Hi - I am trying to create a temporary table that will contain 10 columns whose values are coming out of join between 3 other tables. There also needs to be an 11th column in this temporary table. The contents of the 11th column need to be dependant on whether or not one specific field that I am inserting into the temporary table is null. For example:
Say that I have 2 columns called username, email and I am inserting these into a temporary table. I need to have a 3rd column that inserts the username unless the value is null or is equal to 'NewUser', in which case I need to insert the email address into this 3rd column. Does this make sense?

Any help is greatly appreciated!!
 
A CASE statement should do the trick:

Code:
SELECT UserName
  , Email
  , CASE WHEN UserName IS NULL THEN Email
      WHEN UserName = 'NewUser' THEN Email
      ELSE UserName END
INTO #TempTable
FROM SourceTable

Good luck!

--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top