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)
 Strange select behavior

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-11-01 : 09:17:46
Martin writes "Good day,

Here is my small question.

In MS SQL Server 2000 I've created table:

CREATE TABLE test (x int)
GO

Then I've populated it with data:

INSERT INTO test VALUES (1)
INSERT INTO test VALUES (2)
INSERT INTO test VALUES (3)
INSERT INTO test VALUES (4)
INSERT INTO test VALUES (5)
GO


Then I run this snippet of code:

declare @t varchar(32)
SELECT @t = x FROM test
PRINT @t

and got result:

5

This is obvious - SQL Server selected last inserted value from
the table as it found it first in physical storage...

Next snipped shocked me:

declare @t varchar(1024)
SELECT @t = ''
SELECT @t = @t + CONVERT(varchar(12),x) FROM test
PRINT @t

Result:

12345


Why? Does SQL Server create an implicit cursor and loop through
the table?

Thanks,

Martin"
   

- Advertisement -