2

I have a temptable in my query which could have certain records like

Id Name
34 one
35 two
65 five

Now for each row in temp table, I want to insert a new row in history table

So the history table now would have 3 new records

Id   created date   updatedby
34    createdDate
35    createdDate
65    createdDate
2
  • what is the value of updateby? Commented Mar 5, 2017 at 16:53
  • updated by will remain same and a parameter in query will hold that value.except updated by I also have some other columns but that will remain constants and will pick from parameters supplied to query. only id needs to be picked up from temp table Commented Mar 5, 2017 at 16:59

1 Answer 1

8

You can insert using select.

Something like this:

insert into history (id, created_date, updatedBy)
select id, getdate(), 'add person here??'
from #temptable;

The above require the updatedBy to be supplied in the select query.

If you want current user, you could use SUSER_NAME()

Sign up to request clarification or add additional context in comments.

2 Comments

will it generate new row in history automatically for each row in temp table?
Yes. It will insert one row for each row in temptable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.