Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Using cursor to insert into two tables

Author  Topic 

bustercoder
Starting Member

1 Post

Posted - 2008-02-12 : 01:57:36
Hello,

I have one temp table and two production tables. I'm trying to insert new records into each table from the temp table. There are only a few fields in the temp table I need which will insert into each table. I know how to fetch into a cursor and produce a final resultset (in this case insert), but the challenge I face here is that there are not null fields in each table that need to have a value. So, my question is, do I have to add all the not null fields to my temp table so the cursor has the same number of variables (values to table fields), or can I just hard code the values for the not null fields in the select statements, knowing that the nullable and identity fields will take care of themselves on the insert. In other words, the only fields I would need to be concerned with are the 5 fields from the temp table I'm fetching into the cursor, then hard coding values in the select statements for the not nullable fields and letting the nullable and identity seed increment fields do their thing. Is this a correct assumption? Or am I way off? In any case, if anyone could share their expertise on this matter it would be most greatly appreciated.

Thank you for your time,
Dennis

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-02-12 : 02:20:25
why are you using cursor ?

you can simply use

insert into production_table(col1, col2, col3, col4, col5, . . .)
select cola, colb, 10, colc, . . . .
from #temp_table



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -