Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Data with Select and Value 1

Status
Not open for further replies.

Rob7

Programmer
Dec 12, 2001
152
US
Hi,

I have a situation where I need to add data to a table and the first column needs to be assigned at procedure run time. The rest of the columns can be filled with an INSERT INTO...SELECT staement. What is the syntax for this?

Also, if I am periodicly (once a week) add data to a table and I want to keep previously added data, is INSERT the way to do this or is there an APPEND function?
 
You can specify an arbitray expession in the select list, e.g.

insert into t select getdate(),c1,c2 from t2

There is no append function. Use insert.
 
OK so if I have a variable like @periodID then it would look like this?

INSERT INTO History_SQL
SELECT @periodID c1,c2,...
FROM Source_SQL

 
One last thing. My query is erroring out saying that I need to declare the variable. The code I am using is below. What did I do wrong?

Declare @PeriodID char(6)
SET @PeriodID = "200201"
INSERT INTO Forecast.dbo.SalesHistory_SQL
SELECT @PeridID,
 
Misspelled @PeridID instead of @PeriodID ?

One more thing, you should use ' for strings in SQL rather than "
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top