Solved

SELECT INTO #temp table Then add field

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

732 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