Showing posts with label key. Show all posts
Showing posts with label key. Show all posts

Tuesday, March 27, 2012

Export database as SQL scripts - Primary key

This is what I did to generate the SQL Scipt of my database in EM (SQL
Server 2000):
- Right click the database
- All Tasks
- Generate SQL Sripts
- Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
check constraints.
However, the output script does not define the primary key of my tables.Hi Alan
"Alan T" wrote:
> This is what I did to generate the SQL Scipt of my database in EM (SQL
> Server 2000):
> - Right click the database
> - All Tasks
> - Generate SQL Sripts
> - Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
> check constraints.
> However, the output script does not define the primary key of my tables.
>
Have you checked at the end of the file, there should be a load of ALTER
TABLE statements that add primary keys and foreign keys. Fo example in the
pubs database the first ALTER TABLE statement I get is
ALTER TABLE [dbo].[authors] ADD
CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [phone],
CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
(
[au_id]
) ON [PRIMARY] ,
CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'),
CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
GO
John|||Yes, thanks.
The ALTER statements are at the bottom.
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:EF1CD898-1644-4ADA-BFD4-C184C1A63436@.microsoft.com...
> Hi Alan
> "Alan T" wrote:
>> This is what I did to generate the SQL Scipt of my database in EM (SQL
>> Server 2000):
>> - Right click the database
>> - All Tasks
>> - Generate SQL Sripts
>> - Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults,
>> and
>> check constraints.
>> However, the output script does not define the primary key of my tables.
> Have you checked at the end of the file, there should be a load of ALTER
> TABLE statements that add primary keys and foreign keys. Fo example in the
> pubs database the first ALTER TABLE statement I get is
> ALTER TABLE [dbo].[authors] ADD
> CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [phone],
> CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY] ,
> CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'),
> CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
> GO
> John
>

Export database as SQL scripts - Primary key

This is what I did to generate the SQL Scipt of my database in EM (SQL
Server 2000):
- Right click the database
- All Tasks
- Generate SQL Sripts
- Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
check constraints.
However, the output script does not define the primary key of my tables.Hi Alan
"Alan T" wrote:

> This is what I did to generate the SQL Scipt of my database in EM (SQL
> Server 2000):
> - Right click the database
> - All Tasks
> - Generate SQL Sripts
> - Options tab, check the 'Script PRIMARY keys, FOREIGN keys, defaults, and
> check constraints.
> However, the output script does not define the primary key of my tables.
>
Have you checked at the end of the file, there should be a load of ALTER
TABLE statements that add primary keys and foreign keys. Fo example in the
pubs database the first ALTER TABLE statement I get is
ALTER TABLE [dbo].[authors] ADD
CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [p
hone],
CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
(
[au_id]
) ON [PRIMARY] ,
CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-[
;0-9][0-9][0-9][0-9]'),
CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
GO
John|||Yes, thanks.
The ALTER statements are at the bottom.
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:EF1CD898-1644-4ADA-BFD4-C184C1A63436@.microsoft.com...
> Hi Alan
> "Alan T" wrote:
>
> Have you checked at the end of the file, there should be a load of ALTER
> TABLE statements that add primary keys and foreign keys. Fo example in the
> pubs database the first ALTER TABLE statement I get is
> ALTER TABLE [dbo].[authors] ADD
> CONSTRAINT [DF__authors__phone__78B3EFCA] DEFAULT ('UNKNOWN') FOR [
;phone],
> CONSTRAINT [UPKCL_auidind] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY] ,
> CHECK ([au_id] like '[0-9][0-9][0-9]-[0-9][0-9]-&#
91;0-9][0-9][0-9][0-9]'),
> CHECK ([zip] like '[0-9][0-9][0-9][0-9][0-9]')
> GO
> John
>

Wednesday, March 7, 2012

Expand table rows based on two column ranges

Hi,
I've got a table I'm working with in SQL (through a SAS interface) with the following structure: There is a primary key and for each primary key there are two columns that together constitute a range of values for the primary key. The first column is the start value, the second is the edn value. Here's a quick example:

prim_key startvalue endvalue
1 1023 1026
2 1028 1033
3 1036 1038

What I want to do is expand the table so that each value associated with a primary key is contained on its own row as shown in the table below. I've tried worked with sequences, but this doesn't appear to be the way to go. Does anyone have any suggestions on how I might accomplish this task?

prim_key value
1 1023
1 1024
1 1025
1 1026
2 1028
2 1029
2 1030
2 1031
2 1032
2 1033
3 1036
3 1037
3 1038What you need is a table with all the possible VALUEs in it, i.e. from the min(start_value) to the max(end_value). Or from 1 to max(end_value) would do.

You could create such a table, or a table of integers from 1 to some big enough number, and then join to it like this:

select t.prim_key, v.value
from my_table t, values_table v
where v.value between t.start_value and t.end_value;

You may also be able to "fake" such a table, e.g. in Oracle you could fake it like this:

select t.prim_key, v.value
from my_table t, (select rownum as value from all_objects) v
where v.value between t.start_value and t.end_value;|||I'm having trouble creating the table. The number of inserts to this table is over 500,000. How would I do this usign loops?

Originally posted by andrewst
What you need is a table with all the possible VALUEs in it, i.e. from the min(start_value) to the max(end_value). Or from 1 to max(end_value) would do.

You could create such a table, or a table of integers from 1 to some big enough number, and then join to it like this:

select t.prim_key, v.value
from my_table t, values_table v
where v.value between t.start_value and t.end_value;

You may also be able to "fake" such a table, e.g. in Oracle you could fake it like this:

select t.prim_key, v.value
from my_table t, (select rownum as value from all_objects) v
where v.value between t.start_value and t.end_value;|||I'm having trouble creating the table. The number of inserts to this table is over 500,000. How would I do this usign loops?please, no loops, okay?

you can generate a million integers from just the integers 0 though 9 like this:create table integers (i integer);
insert into integers (i) values (0);
insert into integers (i) values (1);
insert into integers (i) values (2);
insert into integers (i) values (3);
insert into integers (i) values (4);
insert into integers (i) values (5);
insert into integers (i) values (6);
insert into integers (i) values (7);
insert into integers (i) values (8);
insert into integers (i) values (9);

select 100000*hk.i
+ 10000*tk.i
+ 1000*k.i
+ 100*h.i
+ 10*t.i
+ u.i as num
from integers hk
cross
join integers tk
cross
join integers k
cross
join integers h
cross
join integers t
cross
join integers uand then left join to your tables as desired|||Pipelined function may come in handy here...

Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production

SQL> CREATE TYPE numbers_table AS TABLE OF NUMBER (10);
2 /

Type created.

SQL> CREATE OR REPLACE FUNCTION expand (
2 start_value IN NUMBER,
3 end_value IN NUMBER)
4 RETURN numbers_table PIPELINED
5 AS
6 BEGIN
7 FOR i IN start_value..end_value LOOP
8 pipe row (i);
9 END LOOP;
10 RETURN;
11 END;
12 /

Function created.

SQL> CREATE TABLE table_name (
2 prim_key NUMBER,
3 startvalue NUMBER,
4 endvalue NUMBER);

Table created.

SQL> INSERT INTO table_name VALUES (1, 1023, 1026);

1 row created.

SQL> INSERT INTO table_name VALUES (2, 1028, 1033);

1 row created.

SQL> INSERT INTO table_name VALUES (3, 1036, 1038);

1 row created.

SQL> SELECT prim_key, column_value
2 FROM table_name, TABLE (expand (startvalue, endvalue));

PRIM_KEY COLUMN_VALUE
---- ----
1 1023
1 1024
1 1025
1 1026
2 1028
2 1029
2 1030
2 1031
2 1032
2 1033
3 1036
3 1037
3 1038

13 rows selected.

SQL>

Expand Recent File List in Query Analyzer

We have a need to be able to see more than 5 files in the recent file list i
n
Query Analyzer. We tried hacking the following key:
HKEY_CURRENT_USER\Software\Microsoft\Mic
rosoft SQL Server\80\Tools\SQL
Query Analyzer\Recent File List
We tried to add addtional files in this key with no success. I know this is
kind of an odd request, and it's certainly only for convenience, but if
anyone can help us it would be most appreciated!!I don't believe this setting is configurable.
http://www.aspfaq.com/
(Reverse address to reply.)
"Greg Wilt" <Greg Wilt@.discussions.microsoft.com> wrote in message
news:C3062449-88C8-46E5-8C50-F5C7A8674950@.microsoft.com...
> We have a need to be able to see more than 5 files in the recent file list
in
> Query Analyzer. We tried hacking the following key:
> HKEY_CURRENT_USER\Software\Microsoft\Mic
rosoft SQL Server\80\Tools\SQL
> Query Analyzer\Recent File List
> We tried to add addtional files in this key with no success. I know this
is
> kind of an odd request, and it's certainly only for convenience, but if
> anyone can help us it would be most appreciated!!|||I've tried the same thing :-) I don't think it's possible.
HTH
Jasper Smith (SQL Server MVP)
http://www.sqldbatips.com
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Greg Wilt" <Greg Wilt@.discussions.microsoft.com> wrote in message
news:C3062449-88C8-46E5-8C50-F5C7A8674950@.microsoft.com...
> We have a need to be able to see more than 5 files in the recent file list
> in
> Query Analyzer. We tried hacking the following key:
> HKEY_CURRENT_USER\Software\Microsoft\Mic
rosoft SQL Server\80\Tools\SQL
> Query Analyzer\Recent File List
> We tried to add addtional files in this key with no success. I know this
> is
> kind of an odd request, and it's certainly only for convenience, but if
> anyone can help us it would be most appreciated!!

Sunday, February 26, 2012

exists: the absence of data

Can someone please tell me how to check if a record does not exist (as a secondary key) in one table when it exists in another as a primary key. The actual value of the record is not known ahead of time. I am dealing in sybase t-sql if that makes a difference. Also, can anyone recommend a good tutorial on t-sql.

ThanksYou can use the NOT EXISTS condition in your WHERE clause to find rows in the parent table that have no related rows in the child table.

There are lots of good books on Transact-SQL, as well as SQL in general. If all you need is a good introduction, the SQL for Dummies (http://www.dummies.com/WileyCDA/DummiesTitle/productCd-0764540750.html) will probably do the job.

-PatP|||Thanks Pat. Works exactly how I wanted it. Thanks for the book rec. too.

Adam

Originally posted by Pat Phelan
You can use the NOT EXISTS condition in your WHERE clause to find rows in the parent table that have no related rows in the child table.

There are lots of good books on Transact-SQL, as well as SQL in general. If all you need is a good introduction, the SQL for Dummies (http://www.dummies.com/WileyCDA/DummiesTitle/productCd-0764540750.html) will probably do the job.

-PatP

Friday, February 17, 2012

Execution plan behavior

hello Guru's.
Environment:
SQL 7.0 SP4
TableA:
col1 int Primary Key NONCLUSTERED identity NOT NULL
col2 int NOT NULL
col2 nvarchar(255) NOT NULL
No other indexes
select distinct COL2 from tableA
The execution plan performs a hash/match aggregate operation
however
TableB:
col1 int Primary Key NONCLUSTERED identity NOT NULL
col2 nvarchar(255) NOT NULL
col3 nvarchar(255) NOT NULL
col4 nvarchar(255) NOT NULL
col5 nvarchar(4000) NULL
No other indexes
The execution plan performs a SORT/DISTINCT SORT operation
select distinct COL2 from tableA
Anyone know why this is happening? The reason why I am looking into this is
because development ran into a issue where the first query returned data in
sorted order, now occording to them, the data is not sorted. Of course I
explained that since there is no clustered index the data returned is
unpredictable. I have since added a clustered index to TableA to resolve
their issue but I need to know why the second query performs the sort with n
o
order by clause.
I have tested this on 2000 and both queries perform the SORT/DISTINCT SORT
operation.
Itzik if you are out there I appreciate your feedback too.Hi Fred
It doesn't matter whether a Clustered Index is present or not. If you don't
explicitly use an ORDER BY, you're implicitly stating that you don't care
about the order of output. If you really want sorted output, use ORDER BY.
As for the hash match aggregate qn, you have col2 listed twice in TableA, so
I'm a bit confused about what's really going on here. Can you provide some
more information about how much data is in these tables please?
Regards,
Greg Linwood
SQL Server MVP
http://blogs.sqlserver.org.au/blogs/greg_linwood
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
> hello Guru's.
> Environment:
> SQL 7.0 SP4
> TableA:
> col1 int Primary Key NONCLUSTERED identity NOT NULL
> col2 int NOT NULL
> col2 nvarchar(255) NOT NULL
> No other indexes
> select distinct COL2 from tableA
> The execution plan performs a hash/match aggregate operation
> however
> TableB:
> col1 int Primary Key NONCLUSTERED identity NOT NULL
> col2 nvarchar(255) NOT NULL
> col3 nvarchar(255) NOT NULL
> col4 nvarchar(255) NOT NULL
> col5 nvarchar(4000) NULL
> No other indexes
> The execution plan performs a SORT/DISTINCT SORT operation
> select distinct COL2 from tableA
> Anyone know why this is happening? The reason why I am looking into this
> is
> because development ran into a issue where the first query returned data
> in
> sorted order, now occording to them, the data is not sorted. Of course I
> explained that since there is no clustered index the data returned is
> unpredictable. I have since added a clustered index to TableA to resolve
> their issue but I need to know why the second query performs the sort with
> no
> order by clause.
> I have tested this on 2000 and both queries perform the SORT/DISTINCT SORT
> operation.
> Itzik if you are out there I appreciate your feedback too.
>|||Greg is 100% correct. The order in which the data is returned is only
guaranteed with an ORDER BY clause. No ifs ands or buts.
Andrew J. Kelly SQL MVP
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
> hello Guru's.
> Environment:
> SQL 7.0 SP4
> TableA:
> col1 int Primary Key NONCLUSTERED identity NOT NULL
> col2 int NOT NULL
> col2 nvarchar(255) NOT NULL
> No other indexes
> select distinct COL2 from tableA
> The execution plan performs a hash/match aggregate operation
> however
> TableB:
> col1 int Primary Key NONCLUSTERED identity NOT NULL
> col2 nvarchar(255) NOT NULL
> col3 nvarchar(255) NOT NULL
> col4 nvarchar(255) NOT NULL
> col5 nvarchar(4000) NULL
> No other indexes
> The execution plan performs a SORT/DISTINCT SORT operation
> select distinct COL2 from tableA
> Anyone know why this is happening? The reason why I am looking into this
> is
> because development ran into a issue where the first query returned data
> in
> sorted order, now occording to them, the data is not sorted. Of course I
> explained that since there is no clustered index the data returned is
> unpredictable. I have since added a clustered index to TableA to resolve
> their issue but I need to know why the second query performs the sort with
> no
> order by clause.
> I have tested this on 2000 and both queries perform the SORT/DISTINCT SORT
> operation.
> Itzik if you are out there I appreciate your feedback too.
>|||Sorry TableA
The second Col2 should be Col3
TableA has 3K rows
TableB has 1K rows
If that is the case then tell me why tableB without the clustered index is
returning data in sorted order every time the results are returned. Also,
tell me why when I added the clustered index the returned data was sorted
when I remove the clustered index the data was not sorted. I am sorry but
seeing is believing and there more to this than just "no if ands or buts"
So how you doing Andrew? Fred G. from SEI.
"Andrew J. Kelly" wrote:

> Greg is 100% correct. The order in which the data is returned is only
> guaranteed with an ORDER BY clause. No ifs ands or buts.
> --
> Andrew J. Kelly SQL MVP
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
>
>|||http://www.sqlmag.com/articles/inde...articleid=92887
"Andrew J. Kelly" wrote:

> Greg is 100% correct. The order in which the data is returned is only
> guaranteed with an ORDER BY clause. No ifs ands or buts.
> --
> Andrew J. Kelly SQL MVP
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
>
>|||The problem is you are only seeing a part of the picture
Whilst the execution plan SQL Server has chosen might be providing sorted
output presently, remember that SQL Server can arbitrarily chose to change
the plan at will & provide unsorted output if you haven't explicitly stated
you want sorted output.
One example of how this can happen is where you add more CPUs to your
machine & SQL Server decides to use parallelism to process the query - as
each thread scans data & writes output buffers, output will be interleaved
based on wherever each thread's scan is up to rather than written in an
overall sort order. At a later stage, SQL Server might then decide to stop
using parallelism & run the query serially on a single CPU (perhaps in
response to increased user activity) & you might see your output sorted
again.
Another example is that version upgrades & service packs change the way SQL
Server processes queries internally & your output might be affected as a
result - this is actually a very common problem that inexperienced
developers experience when they don't take good advice from experienced
database professionals - just search Google & you will find many sorry
stories in this area (c:
Regards,
Greg Linwood
SQL Server MVP
http://blogs.sqlserver.org.au/blogs/greg_linwood
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:A7FB8ECD-6209-4057-B3B1-C8EAA40027BE@.microsoft.com...[vbcol=seagreen]
> Sorry TableA
> The second Col2 should be Col3
> TableA has 3K rows
> TableB has 1K rows
> If that is the case then tell me why tableB without the clustered index is
> returning data in sorted order every time the results are returned. Also,
> tell me why when I added the clustered index the returned data was sorted
> when I remove the clustered index the data was not sorted. I am sorry but
> seeing is believing and there more to this than just "no if ands or buts"
> So how you doing Andrew? Fred G. from SEI.
> "Andrew J. Kelly" wrote:
>|||Here is the entire article by Itzik
http://www.sqlmag.com/articles/inde...articleid=92886
http://www.sqlmag.com/articles/inde...articleid=92887
http://www.sqlmag.com/articles/inde...articleid=92888
"Andrew J. Kelly" wrote:

> Greg is 100% correct. The order in which the data is returned is only
> guaranteed with an ORDER BY clause. No ifs ands or buts.
> --
> Andrew J. Kelly SQL MVP
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
>
>|||I have tried to explain to the developers that that a query without an order
by is unpredictable in regards to sorted data. However, its obvious that I
need to study more on the internals and perform tests. Moreover, once they
told me that this particular query has been running in production for a few
years without issues I could not explain to them why it was now broken. My
understanding of how data is stored became confused. I really appreciate you
r
response and Andrew's as well.
"Greg Linwood" wrote:

> The problem is you are only seeing a part of the picture
> Whilst the execution plan SQL Server has chosen might be providing sorted
> output presently, remember that SQL Server can arbitrarily chose to change
> the plan at will & provide unsorted output if you haven't explicitly state
d
> you want sorted output.
> One example of how this can happen is where you add more CPUs to your
> machine & SQL Server decides to use parallelism to process the query - as
> each thread scans data & writes output buffers, output will be interleaved
> based on wherever each thread's scan is up to rather than written in an
> overall sort order. At a later stage, SQL Server might then decide to stop
> using parallelism & run the query serially on a single CPU (perhaps in
> response to increased user activity) & you might see your output sorted
> again.
> Another example is that version upgrades & service packs change the way SQ
L
> Server processes queries internally & your output might be affected as a
> result - this is actually a very common problem that inexperienced
> developers experience when they don't take good advice from experienced
> database professionals - just search Google & you will find many sorry
> stories in this area (c:
> Regards,
> Greg Linwood
> SQL Server MVP
> http://blogs.sqlserver.org.au/blogs/greg_linwood
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:A7FB8ECD-6209-4057-B3B1-C8EAA40027BE@.microsoft.com...
>
>|||"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:75ABF0D6-345B-4595-B94D-0A42D5B8DDE1@.microsoft.com...[vbcol=seagreen]
>I have tried to explain to the developers that that a query without an
>order
> by is unpredictable in regards to sorted data. However, its obvious that I
> need to study more on the internals and perform tests. Moreover, once they
> told me that this particular query has been running in production for a
> few
> years without issues I could not explain to them why it was now broken. My
> understanding of how data is stored became confused. I really appreciate
> your
> response and Andrew's as well.
> "Greg Linwood" wrote:
>|||Hey Fred. Sorry I didn't recognize you. I see you already found some links
so I won't go into too many details. But the bottom line is that while it
may return results (or appear to) this can not be trusted and can change
with service packs and versions. Let me know if the links you found don't
answer all the questions. Hope all is well.
Andrew J. Kelly SQL MVP
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:A7FB8ECD-6209-4057-B3B1-C8EAA40027BE@.microsoft.com...[vbcol=seagreen]
> Sorry TableA
> The second Col2 should be Col3
> TableA has 3K rows
> TableB has 1K rows
> If that is the case then tell me why tableB without the clustered index is
> returning data in sorted order every time the results are returned. Also,
> tell me why when I added the clustered index the returned data was sorted
> when I remove the clustered index the data was not sorted. I am sorry but
> seeing is believing and there more to this than just "no if ands or buts"
> So how you doing Andrew? Fred G. from SEI.
> "Andrew J. Kelly" wrote:
>

Execution plan behavior

hello Guru's.
Environment:
SQL 7.0 SP4
TableA:
col1 int Primary Key NONCLUSTERED identity NOT NULL
col2 int NOT NULL
col2 nvarchar(255) NOT NULL
No other indexes
select distinct COL2 from tableA
The execution plan performs a hash/match aggregate operation
however
TableB:
col1 int Primary Key NONCLUSTERED identity NOT NULL
col2 nvarchar(255) NOT NULL
col3 nvarchar(255) NOT NULL
col4 nvarchar(255) NOT NULL
col5 nvarchar(4000) NULL
No other indexes
The execution plan performs a SORT/DISTINCT SORT operation
select distinct COL2 from tableA
Anyone know why this is happening? The reason why I am looking into this is
because development ran into a issue where the first query returned data in
sorted order, now occording to them, the data is not sorted. Of course I
explained that since there is no clustered index the data returned is
unpredictable. I have since added a clustered index to TableA to resolve
their issue but I need to know why the second query performs the sort with no
order by clause.
I have tested this on 2000 and both queries perform the SORT/DISTINCT SORT
operation.
Itzik if you are out there I appreciate your feedback too.
Greg is 100% correct. The order in which the data is returned is only
guaranteed with an ORDER BY clause. No ifs ands or buts.
Andrew J. Kelly SQL MVP
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
> hello Guru's.
> Environment:
> SQL 7.0 SP4
> TableA:
> col1 int Primary Key NONCLUSTERED identity NOT NULL
> col2 int NOT NULL
> col2 nvarchar(255) NOT NULL
> No other indexes
> select distinct COL2 from tableA
> The execution plan performs a hash/match aggregate operation
> however
> TableB:
> col1 int Primary Key NONCLUSTERED identity NOT NULL
> col2 nvarchar(255) NOT NULL
> col3 nvarchar(255) NOT NULL
> col4 nvarchar(255) NOT NULL
> col5 nvarchar(4000) NULL
> No other indexes
> The execution plan performs a SORT/DISTINCT SORT operation
> select distinct COL2 from tableA
> Anyone know why this is happening? The reason why I am looking into this
> is
> because development ran into a issue where the first query returned data
> in
> sorted order, now occording to them, the data is not sorted. Of course I
> explained that since there is no clustered index the data returned is
> unpredictable. I have since added a clustered index to TableA to resolve
> their issue but I need to know why the second query performs the sort with
> no
> order by clause.
> I have tested this on 2000 and both queries perform the SORT/DISTINCT SORT
> operation.
> Itzik if you are out there I appreciate your feedback too.
>
|||Sorry TableA
The second Col2 should be Col3
TableA has 3K rows
TableB has 1K rows
If that is the case then tell me why tableB without the clustered index is
returning data in sorted order every time the results are returned. Also,
tell me why when I added the clustered index the returned data was sorted
when I remove the clustered index the data was not sorted. I am sorry but
seeing is believing and there more to this than just "no if ands or buts"
So how you doing Andrew? Fred G. from SEI.
"Andrew J. Kelly" wrote:

> Greg is 100% correct. The order in which the data is returned is only
> guaranteed with an ORDER BY clause. No ifs ands or buts.
> --
> Andrew J. Kelly SQL MVP
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
>
>
|||http://www.sqlmag.com/articles/index.cfm?articleid=92887
"Andrew J. Kelly" wrote:

> Greg is 100% correct. The order in which the data is returned is only
> guaranteed with an ORDER BY clause. No ifs ands or buts.
> --
> Andrew J. Kelly SQL MVP
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
>
>
|||Here is the entire article by Itzik
http://www.sqlmag.com/articles/index.cfm?articleid=92886
http://www.sqlmag.com/articles/index.cfm?articleid=92887
http://www.sqlmag.com/articles/index.cfm?articleid=92888
"Andrew J. Kelly" wrote:

> Greg is 100% correct. The order in which the data is returned is only
> guaranteed with an ORDER BY clause. No ifs ands or buts.
> --
> Andrew J. Kelly SQL MVP
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:9E4B8155-C3FC-47BC-9664-2AA3C83E695E@.microsoft.com...
>
>
|||I have tried to explain to the developers that that a query without an order
by is unpredictable in regards to sorted data. However, its obvious that I
need to study more on the internals and perform tests. Moreover, once they
told me that this particular query has been running in production for a few
years without issues I could not explain to them why it was now broken. My
understanding of how data is stored became confused. I really appreciate your
response and Andrew's as well.
"Greg Linwood" wrote:

> The problem is you are only seeing a part of the picture
> Whilst the execution plan SQL Server has chosen might be providing sorted
> output presently, remember that SQL Server can arbitrarily chose to change
> the plan at will & provide unsorted output if you haven't explicitly stated
> you want sorted output.
> One example of how this can happen is where you add more CPUs to your
> machine & SQL Server decides to use parallelism to process the query - as
> each thread scans data & writes output buffers, output will be interleaved
> based on wherever each thread's scan is up to rather than written in an
> overall sort order. At a later stage, SQL Server might then decide to stop
> using parallelism & run the query serially on a single CPU (perhaps in
> response to increased user activity) & you might see your output sorted
> again.
> Another example is that version upgrades & service packs change the way SQL
> Server processes queries internally & your output might be affected as a
> result - this is actually a very common problem that inexperienced
> developers experience when they don't take good advice from experienced
> database professionals - just search Google & you will find many sorry
> stories in this area (c:
> Regards,
> Greg Linwood
> SQL Server MVP
> http://blogs.sqlserver.org.au/blogs/greg_linwood
> "FredG" <FredG@.discussions.microsoft.com> wrote in message
> news:A7FB8ECD-6209-4057-B3B1-C8EAA40027BE@.microsoft.com...
>
>
|||"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:75ABF0D6-345B-4595-B94D-0A42D5B8DDE1@.microsoft.com...[vbcol=seagreen]
>I have tried to explain to the developers that that a query without an
>order
> by is unpredictable in regards to sorted data. However, its obvious that I
> need to study more on the internals and perform tests. Moreover, once they
> told me that this particular query has been running in production for a
> few
> years without issues I could not explain to them why it was now broken. My
> understanding of how data is stored became confused. I really appreciate
> your
> response and Andrew's as well.
> "Greg Linwood" wrote:
|||Hey Fred. Sorry I didn't recognize you. I see you already found some links
so I won't go into too many details. But the bottom line is that while it
may return results (or appear to) this can not be trusted and can change
with service packs and versions. Let me know if the links you found don't
answer all the questions. Hope all is well.
Andrew J. Kelly SQL MVP
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:A7FB8ECD-6209-4057-B3B1-C8EAA40027BE@.microsoft.com...[vbcol=seagreen]
> Sorry TableA
> The second Col2 should be Col3
> TableA has 3K rows
> TableB has 1K rows
> If that is the case then tell me why tableB without the clustered index is
> returning data in sorted order every time the results are returned. Also,
> tell me why when I added the clustered index the returned data was sorted
> when I remove the clustered index the data was not sorted. I am sorry but
> seeing is believing and there more to this than just "no if ands or buts"
> So how you doing Andrew? Fred G. from SEI.
> "Andrew J. Kelly" wrote: