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
 General SQL Server Forums
 New to SQL Server Programming
 Copy row from procedure

Author  Topic 

maevr
Posting Yak Master

169 Posts

Posted - 2008-11-06 : 06:46:41
I want to be able to copy a specific row in a table, how do I do this?

I have tried to create a procedure that takes an id as argument, and based on this id I want to fetch the information from the different columns and store them in variables but I cannot get it to work, later on I wish to run an insert with the copied values to the same table

My procedure:
create procedure test(@myID int)
as
begin
declare @name as char(20);
declare @age as int;

insert into @name values(select name from Person where id = @myID)
insert into @age values(select age from Person where id = @myID)
end

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-06 : 07:18:58
create procedure test(@myID int)
as
begin
declare @name as char(20);
declare @age as int;

select @name = name from Person where id = @myID
select @age = age from Person where id = @myID


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-06 : 07:19:39
otr simply

create procedure test(@myID int)
as
begin
declare @name as char(20);
declare @age as int;

select @name = name,@age = age from Person where id = @myID



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-06 : 07:30:33
and make sure you have only one record satisfying the condition
id = @myID else you might need table variable to store them
Go to Top of Page

maevr
Posting Yak Master

169 Posts

Posted - 2008-11-06 : 07:47:55
Thanks!
I got it to work before you posted, your solution was much better so I will use the one below.
----
create procedure test(@myID int)
as
begin
declare @name as char(20);
declare @age as int;

select @name = name,@age = age from Person where id = @myID
----
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-06 : 07:56:29
quote:
Originally posted by maevr

Thanks!
I got it to work before you posted, your solution was much better so I will use the one below.
----
create procedure test(@myID int)
as
begin
declare @name as char(20);
declare @age as int;

select @name = name,@age = age from Person where id = @myID
----


You are welcome

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -