help with while loops

help with while loops

am 23.08.2007 22:58:14 von lhiregoudar

hi,
I am trying to
1)get all the names of a table that match another table
2)while count=0, then I want to insert into another table 'group'
after getting its key.

I am totally lost, can somebody point me in the right direction.

how do I get the individual name so that I can insert into the group
table,
I tried 'select @name=name, that gave no results'.
Thanks

while (SELECT name FROM names WHERE name in (select name from Group)
and status = 'M' )=0
begin
insert into CAll(group_id,name,action) values (@key, name, 'play' )
end

how do i get the individual names to insert into another table ..

Re: help with while loops

am 23.08.2007 23:56:56 von Erland Sommarskog

(lhiregoudar@hotmail.com) writes:
> hi,
> I am trying to
> 1)get all the names of a table that match another table
> 2)while count=0, then I want to insert into another table 'group'
> after getting its key.
>
> I am totally lost, can somebody point me in the right direction.
>
> how do I get the individual name so that I can insert into the group
> table,
> I tried 'select @name=name, that gave no results'.
> Thanks
>
> while (SELECT name FROM names WHERE name in (select name from Group)
> and status = 'M' )=0
> begin
> insert into CAll(group_id,name,action) values (@key, name, 'play' )
> end
>
> how do i get the individual names to insert into another table ..

You are the only one who is lost. I am also lost in trying to understand
what you want to achieve. A standard recommendation is that you post:

o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o The desired result given the sample.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx

Re: help with while loops

am 24.08.2007 00:43:46 von David Portas

wrote in message
news:1187902694.528964.278210@m37g2000prh.googlegroups.com.. .
> hi,
> I am trying to
> 1)get all the names of a table that match another table
> 2)while count=0, then I want to insert into another table 'group'
> after getting its key.
>
> I am totally lost, can somebody point me in the right direction.
>
> how do I get the individual name so that I can insert into the group
> table,
> I tried 'select @name=name, that gave no results'.
> Thanks
>
> while (SELECT name FROM names WHERE name in (select name from Group)
> and status = 'M' )=0
> begin
> insert into CAll(group_id,name,action) values (@key, name, 'play' )
> end
>
> how do i get the individual names to insert into another table ..
>

Why would you need a loop to do that? I don't think you will because you can
INSERT multiple rows in one statement:

INSERT INTO tbl1 (col1, col2)
SELECT col1, col2 FROM tbl2;

If you need more help, please post DDL, sample data and show your required
end result.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).as px
--