Solved

SELECT INTO #temp table Then add field

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

680 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