Solved

SELECT INTO #temp table Then add field

Posted on 2013-01-05
2
797 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
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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now