Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

TSQL variable Expand / Collapse
Author
Message
Posted Friday, August 29, 2008 8:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, April 12, 2009 3:25 AM
Points: 9, Visits: 28
Nancy, it was just a snippet. here's the whole function:

CREATE FUNCTION [dbo].[fnConcatUserComplaints] (@UserId int)

RETURNS varchar(8000)

AS BEGIN

DECLARE @strReturn VARCHAR(max)

SET @strReturn = ''

SELECT @strReturn = @strReturn+ ComplaintDetail+ char(13)
FROM ComplaintsTable
WHERE UserId=@UserId

RETURN @strReturn

END
Post #561263
Posted Friday, August 29, 2008 8:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:35 PM
Points: 6,043, Visits: 8,324
nancy.lytle (8/29/2008)
Hugo, about your code:

SET @ComplaintList = ''

SELECT @ComplaintList = @ComplaintList+ UserComplaint+ char(13)
FROM ComplaintsTable
WHERE UserID=@UserID
..

Return @ComplaintList

I thought that might be something I could use for various things, but I cannot get the code to work, even with the .. commented out. Is there something missing from the code (I of course substituted my name for yours)

Nancy


Hi Nancy,

This code was not posted by me, but by Shnizzle.

Code such as this is not documented and not guaranteed to do what you might expect (and frankly, I don't even know exactly WHAT you expect, as there's more than one result that can be argued to be correct). However, many people seem to report consistent results and choose to rely on this undocumented construction.

Maybe you can post your actual code, along with CREATE TABLE statements for your tables, INSERT statemtents for your test data, and the required results. There might be a better way to achieve what you need.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #561264
Posted Friday, August 29, 2008 9:26 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 20, 2014 7:10 AM
Points: 1,577, Visits: 1,857
Interesting technique! I can think of places where I can use this. Thanks!
Post #561303
Posted Friday, August 29, 2008 12:28 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
To add weight to Hugo's argument, here's a bit from BOL that seems relevant:
Caution:
If there are multiple assignment clauses in a single SELECT statement, SQL Server does not guarantee the order of evaluation of the expressions. Note that effects are only visible if there are references among the assignments.

If a SELECT statement returns more than one row and the variable references a nonscalar expression, the variable is set to the value returned for the expression in the last row of the result set. For example, in this batch @EmpIDVariable is set to the EmployeeID value of the last row returned, which is 1:

USE Northwind
GO
DECLARE @EmpIDVariable int

SELECT @EmpIDVariable = EmployeeID
FROM Employees
ORDER BY EmployeeID DESC

SELECT @EmpIDVariable
GO


Now, of course the QOD result doesn't depend on the order in which the rows are evaluated, but it does depend on all the rows being evaluated. In the BOL example, the lowest EmployeeID is placed in the variable, apparently because it's the last one evaluated by a query that examines each row in table Employees in descending order by Employee.

But what if someone at Microsoft were to improve the performance of this query by tweaking the optimizer to take advantage of an index on EmployeeID and simply return the equivalent of min(EmployeeID)? The query behavior would still meet the description in BOL, only run a bit faster.

Then, even without an "order by" clause, it may be reasonable for the query to still use the value of the last row, even if that is by rule unpredictable. That brings us back to Hugo's point, which is that it can be dangerous to depend upon undocumented behavior of a db engine. By the rules, he's right that the answer could well be 2 or 3, depending on the implementation of the documented expected results for a query in this form.
Post #561425
Posted Friday, August 29, 2008 12:35 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 16, 2014 5:07 PM
Points: 386, Visits: 423
Sorry about that, actually I was just thinking it would be another way to gets lists, but I can do that with regular queries. I just hadn't used that type of technique and thought it looked interesting. But since I can't get it to work in SS2005, and some of you are saying it can't be trusted, I will abandoned my 'playing' with it.

Thanks,
Nancy
Post #561434
Posted Friday, August 29, 2008 1:47 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 20, 2014 7:10 AM
Points: 1,577, Visits: 1,857
Hugo Kornelis (8/29/2008)
If you still want to implement this and put it in production, feel free to do so ... but never forget that it IS undocumented, so you'll have to retest after every service pack, every patch, and every hardware update and still be prepared to be surprised. The fact that many users do use this and do rely on this does not guarantee that Microsoft won't change it - just remember what happened to GROUP BY without ORDER BY when upgrading from SQL 6.5 to SQL 7.0, or to views with TOP 100 PERCENT and ORDER BY when upgrading to SQL 2000 to SQL 2005. For my production code, I'll stick to officially documented code. :)


Ok, Hugo, I get what you are saying about officially documented code. That is a good rule to follow.
Post #561490
Posted Saturday, August 30, 2008 7:09 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 2011 7:20 AM
Points: 977, Visits: 1,499
I'm not sure whether to be confused or intrigued. Good QOD.

Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #561606
Posted Tuesday, September 2, 2008 11:56 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 9:05 PM
Points: 441, Visits: 933
Well, I am confused.

Whatever happened to the concept that you cannot assign a result set to a scalar -- Should this not be barfed out at compile time in the first place ? I certainly would prefer
it be so, in case I make an error while coding something like this, rather than have this bizarre and not necessarily predictable behavior.

If I remember previous versions, it used to be that way.

But even when doing this for a string instead of an integer, it still results in a long string which concatenates all of the values.

Regards
Post #562552
Posted Wednesday, November 26, 2008 8:44 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 8:48 AM
Points: 988, Visits: 806
I reckon it's a good example of bad practice just because the outcome isn't defined
Post #609178
Posted Wednesday, November 26, 2008 9:17 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 19, 2013 2:10 AM
Points: 138, Visits: 236
(with apology)
and i reckon, good programming skills are needed to understand this code.


Tariq
master your setup, master yourself.
http://mssqlsolutions.blogspot.com
Post #609229
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse