July 4, 2005 at 7:48 am
This is embarrassing - I can't work out why this is giving me a syntax error at the last line (ORDER BY clause) on SQL Server 7.0. I would welcome any help!
CREATE TABLE #PolicyList
(PlanNo Char(12) NOT NULL,
PolicyHolder1 Integer NOT NULL,
PolicyHolder2 Integer NULL,
CorrespondenceClient Integer NULL)
ALTER TABLE #PolicyList WITH NOCHECK ADD
CONSTRAINT PK_#PolicyList PRIMARY KEY CLUSTERED (PlanNo) WITH FILLFACTOR = 100
/* Code to populate #PolicyList omitted as irrelevant */
CREATE TABLE #SingleHolders (PolicyHolder Integer NOT Null,
CorrespondenceClient Integer NOT Null)
ALTER TABLE #SingleHolders WITH NOCHECK ADD
CONSTRAINT PK_#SingleHolders PRIMARY KEY CLUSTERED (PolicyHolder, CorrespondenceClient) WITH FILLFACTOR = 100
INSERT INTO #SingleHolders
(PolicyHolder, CorrespondenceClient)
(SELECT DISTINCT PolicyHolder1, ISNull(CorrespondenceClient, 0)
FROM #PolicyList
WHERE PolicyHolder2 IS Null
ORDER BY PolicyHolder1, ISNull(CorrespondenceClient, 0))
The SELECT compiles happily but the full INSERT gives :
Server: Msg 156, Level 15, State 1, Line 23
Incorrect syntax near the keyword 'ORDER'.
July 4, 2005 at 7:59 am
CREATE TABLE #PolicyList
(PlanNo Char(12) NOT NULL,
PolicyHolder1 Integer NOT NULL,
PolicyHolder2 Integer NULL,
CorrespondenceClient Integer NULL)
ALTER TABLE #PolicyList WITH NOCHECK ADD
CONSTRAINT PK_#PolicyList PRIMARY KEY CLUSTERED (PlanNo) WITH FILLFACTOR = 100
/* Code to populate #PolicyList omitted as irrelevant */
CREATE TABLE #SingleHolders (PolicyHolder Integer NOT Null,
CorrespondenceClient Integer NOT Null)
ALTER TABLE #SingleHolders WITH NOCHECK ADD
CONSTRAINT PK_#SingleHolders PRIMARY KEY CLUSTERED (PolicyHolder, CorrespondenceClient) WITH FILLFACTOR = 100
INSERT INTO #SingleHolders
(PolicyHolder, CorrespondenceClient)
(
Select dtPol.PolicyHolder1, dtPol.Col2 from
(
SELECT DISTINCT TOP 100 PERCENT PolicyHolder1, ISNull(CorrespondenceClient, 0) as Col2
FROM #PolicyList
WHERE PolicyHolder2 IS Null ORDER BY PolicyHolder1, Col2
) dtPol
)
DROP TABLE #PolicyList
DROP TABLE #SingleHolders
But on a more theorical approach. A table is a set, sets have no ordering. There's no point in ordering the data into the table. You should order the data when you select from it.
July 4, 2005 at 8:06 am
Thanks for the instant response!
I had a clustered key on the table that I'm inserting to so as to speed later queries and included the ORDER BY so that records would be written in key order. Is that unnecessary?
I'm afraid I still don't understand what's wrong with the original syntax. The weather's lousy, the Americans are on holiday and I was up late last night - my brain doesn't want to be at work!
Many thanks for your help.
July 4, 2005 at 8:20 am
Don't know either why it doesn't work, it should work with top 100 percent but it obviously doesn't.
As I said the tables have no order. You need to put an index on the temp table if you want to have better performance. If you have a normal base table, then it'll be done automatially.
July 4, 2005 at 8:23 am
Once again, thanks very much. To echo Steve Jones, you deserve all those points.
July 4, 2005 at 8:31 am
When did he say that?
July 4, 2005 at 8:34 am
A couple of weeks ago. Perhaps I imagined it but if he didn't, he should have!
July 4, 2005 at 8:35 am
Maybe you should drop him a line to have a confirmation... you wouldn't want to say something on his behalf that he doesn't approve of .
July 4, 2005 at 9:01 am
July 4, 2005 at 9:18 am
Now I would call that a bug .
Thanx for the info.
July 4, 2005 at 9:29 am
Thanks, you're right - sort of. It happens in this case but it works fine with the brackets if you take out the ORDER BY clause.
Checking BOL (which I should have done in the first place but interpreting the syntax syntax makes my brain hurt), the syntax does not include brackets so I guess that this is a feature rather than a bug!
July 4, 2005 at 9:42 am
The error should still be clearer than that.
July 7, 2005 at 9:46 pm
Just SELECT, no (SELECT ... ) (a former Oracle kid?):
INSERT INTO #SingleHolders
(PolicyHolder, CorrespondenceClient)
SELECT DISTINCT PolicyHolder1, ISNull(CorrespondenceClient, 0)
FROM #PolicyList
WHERE PolicyHolder2 IS Null
ORDER BY PolicyHolder1, ISNull(CorrespondenceClient, 0)
And yes, sorting according to the physical order imposed, would avoid fragmentation and It'll be a little bit faster.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply