|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help with simple SP (newbie)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. 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. > > 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 ;-) -- Show quoteRegards, Diego F. "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. > > 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. > > > |
|||||||||||||||||||||||