September 6, 2010 at 6:36 am
i also caught the same as chris , varchar(10) is playing the trick , but why ? , dont know:unsure:
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 6, 2010 at 6:39 am
ColdCoffee (9/6/2010)
i increased the VARCHAR to 12 and voila, i get the desired result "always"...Hmm..
yes .. but size should work according to select len('10_samjack')
which is 10 which means varchar(10) should give us "10_samjack" but its giving "10_jack"
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 6, 2010 at 7:18 am
There's more:
DECLARE @s1 varchar(10), @s2 varchar(10)
-- '10__Jack'
SET @s1 = '10__'
SELECT @s1 = @s1 + Name--, @s2 = 'x'
FROM (SELECT ID = 1, Name = CAST('Sam' AS VARCHAR(10)) UNION ALL SELECT 2, 'Jack') tb ORDER BY id
SELECT @s1
-- '10__Sam'
SET @s1 = '10__'
SELECT @s1 = @s1 + Name
FROM (SELECT ID = 1, Name = CAST('Sam' AS VARCHAR(10)) UNION ALL SELECT 2, 'Jack') tb ORDER BY 1 desc
SELECT @s1
-- '10__SamJac'
SET @s1 = '10__'
SELECT @s1 = @s1 + Name
FROM (SELECT ID = 1, Name = CAST('Sam' AS VARCHAR(10)) UNION ALL SELECT 2, 'Jack') tb ORDER BY Name DESC
SELECT @s1
-- '10__SamJac'
SET @s1 = '10__'
SELECT @s1 = @s1 + Name
FROM (SELECT ID = 1, Name = CAST('Sam' AS VARCHAR(10)) UNION ALL SELECT 2, 'Jack') tb
SELECT @s1
-- '10__SamJac'
SET @s1 = '10__'
SELECT @s1 = @s1 + Name
FROM (SELECT TOP 2 ID = 1, Name = CAST('Sam' AS VARCHAR(10)) UNION ALL SELECT 2, 'Jack') tb ORDER BY ID
SELECT @s1
-- '10__Jack'
SET @s1 = '10__'
SELECT @s1 = @s1 + Name, @s2 = 'x'
FROM (SELECT ID = 1, Name = CAST('Sam' AS VARCHAR(10)) UNION ALL SELECT 2, 'Jack') tb ORDER BY id
SELECT @s1
If you check out the plans, the first two queries give a first step (constant scan) row size of 20B, compared with the queries which give the expected result, which have a first step row size of 16B.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 6, 2010 at 7:28 am
Are we in for a Connect Item ?
September 6, 2010 at 7:36 am
It's looking promising. I'd like to hear a few opinions from others first.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 6, 2010 at 9:11 am
ColdCoffee (9/6/2010)
Are we in for a Connect Item ?
Nope, it's an unsupported use of variable assignment which yields unpredictable results and has already been covered on the forum here.
The examples we've come up with merely support this fact.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 6, 2010 at 10:58 am
Chris Morris-439714 (9/6/2010)
ColdCoffee (9/6/2010)
Are we in for a Connect Item ?Nope, it's an unsupported use of variable assignment which yields unpredictable results and has already been covered on the forum here.
The examples we've come up with merely support this fact.
Oh, thats news, to me atleast 😛 Thanks Morris, learnt a new thing today 🙂
September 6, 2010 at 8:37 pm
thanks.
«Inside SQL Server 2005:T-SQL»says:
"SET @var = value" is the right way to assign value to variables;
when u use "SELECT @var=col",the results may diff,
because u dont know how query optimizer executes ur SQL,espically when u use ORDER BY clause.
1?assign value first,and then sort
the result will be "10_jack"
2?sort first, and then assign value
the result will be "10_samjack"
thank u again...
September 7, 2010 at 7:02 am
Note that @s1 is declared as varchar(10) whereas @s2 as varchar(20). Use varchar(20) for @s1 and see the result. It gives the correct result
DECLARE @TB TABLE (id INT,name varchar(10))
INSERT @TB SELECT 1 ,'jack'
UNION ALL SELECT 2 ,'sam'
UNION ALL SELECT 6 ,'micle'
UNION ALL SELECT 7 ,'Jop'
UNION ALL SELECT 7 ,'Jop'
UNION ALL SELECT 12, 'Nill'
DECLARE @s1 varchar(20),@s2 varchar(20) --len diff
--SQL 1
SELECT @s1='10_'
SELECT @s1=@s1+NAME FROM ( SELECT TOP 2 id,name FROM @TB ORDER BY ID ) a ORDER BY ID DESC
SELECT @s1
--SQL 2(the same with SQL 1,just replace @s1 with @s2,the result diffs)
SELECT @s2='20_'
SELECT @s2=@s2+NAME FROM ( SELECT TOP 2 id,name FROM @TB ORDER BY ID ) a ORDER BY ID DESC
SELECT @s2
--SQL 3(use 2 variable together,the same results)
SELECT @s1='10_',@s2='20_'
SELECT @s1=@s1+NAME,@s2=@s2+NAME
FROM ( SELECT TOP 2 id,name FROM @TB ORDER BY ID ) a ORDER BY ID DESC
SELECT @s1,@s2
Failing to plan is Planning to fail
September 7, 2010 at 7:12 am
Madhivanan-208264 (9/7/2010)
Note that @s1 is declared as varchar(10) whereas @s2 as varchar(20). Use varchar(20) for @s1 and see the result. It gives the correct result
It's not as simple as changing the size of the variable. Depending upon how you lay out the data retrieval query and the variable assignment, even a VARCHAR(20) receiving variable may contain only one of the two names expected.
There is no "correct result" for this type of query because it's unsupported, undocumented and discouraged. When an expected result is obtained it is by accident, not by design.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 10 posts - 16 through 24 (of 24 total)
You must be logged in to reply to this topic. Login to reply