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
 Easiest way as I intended

Author  Topic 

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-04 : 02:32:23
Hi guys how are you doing? I was wondering how should I set the values in these two variables without making the select, I mean just set, I think that if I select two times it takes much longer is that true?

declare @user_id int, @creation datetime

select @user_id = user_id, @creation = creation from solarUsers

Select @user_id, @creation

Thank you very much =D.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-04 : 02:36:32
Yes You can do this but if you have more than one record in solarUsers, the variable @user_id and @creation will only store the last record of the table.

DECLARE @user_id int, @creation datetime

SELECT @user_id = user_id, @creation = creation FROM solarUsers

SELECT @user_id, @creation



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2007-07-04 : 02:38:44
It will be better if you let us know what are you trying to do ???

Since the code which you have posted doesnt make any sense, since you are not doing any processing with those variable.

if you just want to display the record, then only select statement is more then enough...



Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-04 : 02:43:38
Well I mean just set, because if you select it goes to return as another table answer. I thought do a temp table, but does it cost too much memory? and would be rather make two set queries?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-04 : 02:50:35
rseqecin, Can you explain what are you trying to achieve here ? You just want to return 2 value in a single row or 2 column of multiple rows of data ? ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-04 : 02:58:26
Thank you guys for the quick replies. what I'm trying to ask is if is possible set two variables in a sigle line without return them. If I set just one variable without return any value would be:

set @user_id = (Select top(1) [user_id] from solarUsers)

sorry I forgot the top(1) in the previous code.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2007-07-04 : 03:01:58
oks.. in that case i would go with the Select statement rather then Set statement because the code will be minimized and also it will be done in on select statement...

but if there are is only one variable to be initialized then i would go with the Set Statment..

further you can get the difference between set and select by reading the following link

http://vyaskn.tripod.com/differences_between_set_and_select.htm

Hope this helps you



Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-04 : 03:09:27
And what about creating a table

create table #userTable
(
RowNumber int IDENTITY (1, 1) NOT NULL,
user_id int,
creation datetime
)

insert into #userTable select top(1) user_id, creation from solarUsers

I'm thinking that I'm worrying to much about performance and with “consistent return”
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-04 : 03:11:34
you don't need the temp table. Since you are selecting one record only (TOP 1), your previous query will be fine.

SELECT TOP 1 @user_id = user_id, @creation = creation FROM solarUsers ORDER BY ???

Note that TOP 1 has no meaning without the ORDER BY clause


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2007-07-04 : 03:14:43
Yes, i think you are worrying to much about the performance on the simple thing.. there are more complicated stuff where you actually require to look for the performance

but never the less it will improve you programming skill.. :)

aha about creating the the local temp table, i think it wont help much because all the temp tables are created in the Tempdb.. i think its as the normal select statement... (if the base table has less number of records)



Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-04 : 03:28:38
I quickly read the article, and saw that is no way to do set with multiple variable in a single line, thank you chiragkhabaria for the link.
I didn't know about the top and order by thing, thank you khtan.
Well I'm learning T-SQL everything all by my self, I'm struggling in web developer and I'm not working with team or anyone to have a point of reference, so if you saw my code you would be shocked, cuz it's a kind of nhacc... but the import think that they are working =D lol. so never mind.

Thank you very much guys.
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2007-07-04 : 03:33:26
Well I'm learning T-SQL everything all by my self, I'm struggling in web developer and I'm not working with team or anyone to have a point of reference, so if you saw my code you would be shocked, cuz it's a kind of nhacc... but the import think that they are working =D lol. so never mind.

Well i guess you need to read this too..

http://www.sqlservercentral.com/columnists/jchan/3000.asp

Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-07-04 : 04:02:36
Well may be I just miss a job to get a way to be a guru. =D
Go to Top of Page
   

- Advertisement -