| Author |
Topic |
|
zubair
Yak Posting Veteran
67 Posts |
Posted - 2003-07-17 : 09:33:19
|
| Hi,I have following code in a stored procedure as folows to return two output variables. As you can see i'm running the same query twice to get the return value for each varaible. set @emaillist = (select emailList from GSF.dbo.ftpDestination where destinationid = @ftpdestinationid)set @ftpdestinationname = (select [name] from GSF.dbo.ftpDestination where destinationid = @ftpdestinationid)However i can return the two values using the query just onne to get both variables like so select emailList, [name] from GSF.dbo.ftpDestination where destinationid = @ftpdestinationidThe problem i am having is how to set both return variables just using the above query once.Any help greatly appreciated |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-07-17 : 13:22:17
|
| select @emaillist = emailList ,@ftpdestinationname = [name] from GSF.dbo.ftpDestination where destinationid = @ftpdestinationid==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.Edited by - nr on 07/17/2003 13:22:41Edited by - nr on 07/17/2003 13:22:57 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-07-17 : 13:42:41
|
| When you are trying to put a column from a table into a variable, do not use SET. Use SELECT @Variable1 = Column1 FROM Table1 method. Use SET for things like this: SET @Variable2 = 0, SET @Variable3 = 'SQLTeam'.Tara |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-07-17 : 14:09:01
|
| Or don't use SET at all...Has anyone made a good argument for it's use?Isn't:SET @Variable2 = 0, SET @Variable3 = 'SQLTeam'Mor overhead thanSELECT @Variable2 = 0, @Variable3 = 'SQLTeam' Doesn't the optimizer have to prepare 2 statements rather than 1?Brett8-) |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-07-17 : 14:49:05
|
| From bolIt is recommended that SET @local_variable be used for variable assignment rather than SELECT @local_variable.Not that's much of a reason.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-07-17 : 14:53:41
|
| I thought I had read that SET should be used, so thanks Nigel for that. Putting it on one line just allows you to write less code. In my previous post, the comma was used to separate the two examples.Tara |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-07-17 : 15:31:46
|
[homer]doooooooooooooooooooooooooooooooooooooooh[/homer]Well it would be less reads BECAUSE I FORGOT THE DECLARE's IN THE SECOND STRINGTried it again and they're the same[homer]doooooooooooooooooooooooooooooooooooooooh[/homer]Well, it looks like SET is more effecientThe following 2 SQL's show that there are more reads associated with the SELECT (not that I understand what SQL Profiler means specifically by "Reads")DECLARE @X int, @y char, @z int, @a int, @b int, @c intSELECT @x = 0, @y = 'x', @z = 0, @b = 0, @c = 0GO Is Less effecient thanDECLARE @X int, @y charSET @x = 0SET @y = 'x'SET @z = 0SET @a = 0SET @b = 0SET @c = 0GO FROM profiler (the numbers in red are the reads)SQL:BatchCompleted DECLARE @X int, @y char, @z int, @a int, @b int, @c intSELECT @x = 0, @y = 'x', @z = 0, @b = 0, @c = 0 SQL Query Analyzer sa 0 12 0 0 330 72 2003-07-17 15:27:59.707 SQL:BatchCompleted DECLARE @X int, @y charSET @x = 0SET @y = 'x'SET @z = 0SET @a = 0SET @b = 0SET @c = 0 SQL Query Analyzer sa 0 8 0 0 330 72 2003-07-17 15:27:59.740 Brett8-)Edited by - x002548 on 07/17/2003 15:33:22Edited by - x002548 on 07/17/2003 15:36:35 |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-07-17 : 18:49:28
|
| While it's strictly a matter of preference, I like the fact that one SELECT can assign multiple variables, but more importantly, you can't use SET to set a variable equal to the results of a query/table. You'd have to use SELECT for that anyway. Almost every time I write a bit of code using SET @var = something I end up needing to add another var, or change it to get results from a table.I've also settled on the philosophy that SET should be used for system settings, like SET ROWCOUNT or SET ANSI_NULLS ON. That is a nice, clean delineation for me that's pretty easy to follow...just natural for me I guess. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-07-17 : 18:57:07
|
| set @t = 1set @t = (select top 1 id from sysobjects)Oops - wouldn't want to do 2 selects soselect @t = id, @n = namefrom (select top 1 * from sysobject) aOr - and I think this is a good reason for not using set to set variablesselect @error = @@error, @rowcount = @@rowcountunless anyone knows how to do that with set statements.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|