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)
 Procedure to select and insert

Author  Topic 

Mishenata
Starting Member

2 Posts

Posted - 2010-08-09 : 11:40:16
I'd like to write a procedure that will take the data from an 'Import' table with Select (Id, Name, City, Salary) and update and insert that data in another table 'MonthsImports'. Need Help Please..

quote:

CREATE PROCEDURE p_MonthImports
AS BEGIN
DECLARE id INT
DECLARE name NVARCHAR(10)
DECLARE salary INT
DECLARE city NVARCHAR(10)

DECLARE cur_client CURSOR FOR
SELECT id,name,salary,city from Import

OPEN cur_client
..............
...........


vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-08-09 : 11:42:22
[code]INSERT INTO MonthsImports
SELECT Id, Name, City, Salary FROM ImportTable[/code]
Go to Top of Page

Mishenata
Starting Member

2 Posts

Posted - 2010-08-10 : 03:07:21
Every month the Import table is filled with the current information about the salaries. I have to store the whole history concerning the salaries in another table. For this purpose, I need to write a stored procedure that will Select all the records and fields from the monthly Import table and Update or Insert 'MonthsImports' where the History will be stored.

Import table:
id | name | salary | city

(in oracle I use: merge into MonthsImports MI using (select...) when matched then update.. when not matched then Insert ..)
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-08-10 : 08:15:18
MERGE is available in SQL 2008 but not in 2005. Here's a way to fake it: http://sqlserver-tips.blogspot.com/2006/09/mimicking-merge-statement-in-sql.html

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -