Solved

SELECT INTO #temp table Then add field

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

707 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

18 Experts available now in Live!

Get 1:1 Help Now