Home All Groups Group Topic Archive Search About

Help with simple SP (newbie)

Author
15 Jul 2005 10:24 AM
Diego F.
Hi. I'm writing my firsts stored procedures.

I'm trying to get a row from one table and insert the results in other
table. By now I have this:

create proc registerTable
@email varchar(30)
as
select name,email
from users
where email=@email
go

insert into employees
exec registerTable 'users'

go

But I need to include the insert in the procedure. How can I do that?

--
Regards,

Diego F.

Author
15 Jul 2005 10:44 AM
Jonathan Chong
In your SP:

INSERT INTO employees select name,email from users where email=@email

Show quote
"Diego F." <diegof***@terra.es> wrote in message
news:%23syp$cSiFHA.3336@tk2msftngp13.phx.gbl...
> Hi. I'm writing my firsts stored procedures.
>
> I'm trying to get a row from one table and insert the results in other
> table. By now I have this:
>
> create proc registerTable
> @email varchar(30)
> as
> select name,email
> from users
> where email=@email
> go
>
> insert into employees
> exec registerTable 'users'
>
> go
>
> But I need to include the insert in the procedure. How can I do that?
>
> --
> Regards,
>
> Diego F.
>
>
Author
15 Jul 2005 10:47 AM
Diego F.
Well, I think I found it:

create proc registerTable
@email varchar(30)
as
insert into employees
select name,email
from users
where email=@email
go

I said I was a newbiew ;-)

--
Regards,

Diego F.




Show quote
"Diego F." <diegof***@terra.es> escribió en el mensaje
news:%23syp$cSiFHA.3336@tk2msftngp13.phx.gbl...
> Hi. I'm writing my firsts stored procedures.
>
> I'm trying to get a row from one table and insert the results in other
> table. By now I have this:
>
> create proc registerTable
> @email varchar(30)
> as
> select name,email
> from users
> where email=@email
> go
>
> insert into employees
> exec registerTable 'users'
>
> go
>
> But I need to include the insert in the procedure. How can I do that?
>
> --
> Regards,
>
> Diego F.
>
>
Author
15 Jul 2005 5:24 PM
JosephPruiett
Here is an example with error checking before insert takes place.

Create table #users
(
name  sysname,
email sysname
)

create table #employees
(
name  sysname,
email sysname
)


insert #users
values ('u1','t***@test.com')

--drop proc registerTable

create proc registerTable
@email varchar(30) = 't***@test.com'
as
IF @email = (select email from #users)
   Begin--insert
   Insert #employees
   select name,email
   from #users
   where email=@email
   end --insert
ELSE
PRINT 'EMAIL ADDRESS NOT FOUND'
go

--insert into employees
exec registerTable 'users' -- does not exist
select * from #employees 

exec registerTable 't***@test.com'
select * from #employees 



Show quote
"Diego F." wrote:

> Hi. I'm writing my firsts stored procedures.
>
> I'm trying to get a row from one table and insert the results in other
> table. By now I have this:
>
> create proc registerTable
> @email varchar(30)
> as
> select name,email
> from users
> where email=@email
> go
>
> insert into employees
> exec registerTable 'users'
>
> go
>
> But I need to include the insert in the procedure. How can I do that?
>
> --
> Regards,
>
> Diego F.
>
>
>

AddThis Social Bookmark Button