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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Server - How to use Select clause

Author  Topic 

dudus
Starting Member

5 Posts

Posted - 2004-10-17 : 04:49:44
I want to know how can i retrieve several columns into several variables in one select statement.

one column:
select @workerId = (select workerId from workers)

I want to retreive also workername, How can I do that?

VIG
Yak Posting Veteran

86 Posts

Posted - 2004-10-17 : 06:36:02
select @workerId =workerId,@workerName=workerName from workers
Go to Top of Page

dudus
Starting Member

5 Posts

Posted - 2004-10-17 : 08:58:42
thanks,

now how can i go over all the records with a loop.

what's the syntax?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-10-17 : 09:01:02
Don't use a loop.

What are you trying to do exactly?
Go to Top of Page

dudus
Starting Member

5 Posts

Posted - 2004-10-17 : 09:27:59
I want to some kin of a way to calculate values retrieved from the select
for example:

declare @temp bigint
select @payment = payment, @percentage = percentage from workers where workerid >10
@temp = @temp + @payment * @percentage

this line: @temp = @temp + @payment * @percentage, happends after the select statement and i wand it to be in the select statement. I hope you get my meaning...
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-10-17 : 09:39:13
You want a running total of the payment/percentage calculation? Or just a total?

A regular total:

SELECT SUM(Payment * Percentage) FROM workers WHERE workerid>10

Running totals are here:

http://www.sqlteam.com/item.asp?ItemID=3856
Go to Top of Page

dudus
Starting Member

5 Posts

Posted - 2004-10-17 : 11:10:02
No i did'nt mean a Sum, my question is a general questions.

for example in oracle:
for vec in (select id,name from workers)
loop
x:= vec.id;
y:= rec.name;

end loop;

i want the equivalent code in sql server.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-10-17 : 11:33:53
Oracle likes to use cursors for everything, cursors are bad in SQL Server and should be avoided as much as possible. See here for more info:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=41263

Before you reply "I understand, but I'm trying to convert this and I need it quickly..." and so on, understand that this is the BEST advice I can give you, and it WILL save you the most time: DO NOT try to convert Oracle cursors directly to SQL Server. Follow the advice in the above thread, it works.
Go to Top of Page

dudus
Starting Member

5 Posts

Posted - 2004-10-17 : 13:19:34
OK thanks
Go to Top of Page
   

- Advertisement -