Solved

SELECT INTO #temp table Then add field

Posted on 2013-01-05
2
808 Views
Last Modified: 2013-01-05
I'm creating a temp table in a stored procedure by selecting 4 fields from an existing table. Then I add a new field to the table into which I want to store the current date and time.  I'm getting an error on the update statement. The error is "invalid column DateAdded".

The code is:
SELECT WorkSites_ContactsImport.ExtOrgID, WorkSites_ContactsImport.ContactNum,
      WorkSites_ContactsImport.Location, WorkSites_ContactsImport.ContactName
      INTO #CNTCS_temp FROM WorkSites_ContactsImport LEFT JOIN WorkSiteContacts
      ON (WorkSites_ContactsImport.ContactNum = WorkSiteContacts.ContactNum)
      AND (WorkSites_ContactsImport.ExtOrgID = WorkSiteContacts.ExtOrgID)
      WHERE (((WorkSiteContacts.ExtOrgID) Is Null));
--SELECT * FROM #CNTCS_temp

ALTER TABLE #CNTCS_temp ADD DateAdded datetime
SELECT * FROM #CNTCS_temp
UPDATE #CNTCS_temp SET DateAdded = convert(datetime, convert(char(10), getdate(), 110))

The SELECT statement before the update gives this result, so I know the field is added.
results of SELECT
What can I do to put the DateAdded into the temp table which will then be put into the production table using this statement.
INSERT INTO WorkSiteContacts (ExtOrgID, ContactNum, LocNum, ContactName, DateAdded) SELECT * FROM #CNTCS_temp

Thanks, Susan
0
Comment
Question by:scover22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 250 total points
ID: 38747538
just add it in the first query:

SELECT WorkSites_ContactsImport.ExtOrgID, WorkSites_ContactsImport.ContactNum,
      WorkSites_ContactsImport.Location, WorkSites_ContactsImport.ContactName,
convert(datetime, convert(char(10), getdate(), 110)) as dateAdded
      INTO #CNTCS_temp FROM WorkSites_ContactsImport LEFT JOIN WorkSiteContacts
      ON (WorkSites_ContactsImport.ContactNum = WorkSiteContacts.ContactNum)
      AND (WorkSites_ContactsImport.ExtOrgID = WorkSiteContacts.ExtOrgID)
      WHERE (((WorkSiteContacts.ExtOrgID) Is Null));
--SELECT * FROM #CNTCS_temp
0
 

Author Closing Comment

by:scover22
ID: 38747580
Perfect, just what I was looking for.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question