Hi,
Code:
CREATE PROCEDURE [dbo].[customer] AS
declare @proj_id int
declare @proj_name varchar(30)
Do a check and Create table only if it NOT exists
create table abc (proj_id int, proj_name nvarchar(50))
declare custcursor cursor for
select proj_id, proj_name from archive order by proj_id
for read only
open custcursor
fetch next from custcursor into @proj_id, @proj_name
while (@@fetch_status = 0)
begin
print cast (@proj_id as varchar (10) ) + ' ' + @proj_name
insert into abc(proj_id, proj_name) values(@proj_id,@proj_name)
fetch next from custcursor into @proj_id, @proj_name
end
close custcursor
deallocate custcursor
GO
You seem to have confused with the variable usage within the cursor.
You can do it this way. But I really wonder why you have do that within cursor for every row, instead of doing it in a simpler way that exists. It is easier to do that without using cursor as given below.
Code:
INSERT INTO abc (select proj_id, proj_name from archive order by proj_id)
Cursor is best fit for doing something like eg: calculating/updating values row by row and/or displaying it a formatted manner, not for inserting row by row into another table, which can be done at one shot in a easier way.
Hope that helps.
Cheers!
_________________________
- Vijay G
Strive for Perfection