I have created the following test SQL code to illustrate a real
problem I have with some SQL code.
CREATE TABLE JCTable ( CustomerName varchar(50) )
ALTER TABLE JCTable ADD CustomerNo int
INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Jon Combe'
, 1 )
INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Bill
Gates' , 1 )
UPDATE JCTable SET CustomerNo = 2 WHERE CustomerName = 'Jon Combe'
SELECT * FROM JCTable
When I run this SQL via the query analyser I get the errors:-
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'CustomerNo'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'CustomerNo'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'CustomerNo'.
It appears the SQL Server is trying to "pre-parse" the query and
hasn't picked up on the ALTER TABLE line that adds this column and so
complains that it doesn't exist. However it doesn't end there.
If I then run this query a line at a time by highlighting each line in
the query analyser and running it all works (not unexpected). However
then dropping the table and then running the full SQL code once more
(I.E. not highlighting each line), it then works as expected. I assume
it was somehow remembering "state" in my session so closed and
re-started the Query Analyser with the same result that the code does
now work.
However changing the table name to something new brings back the
errors once more. Can anyone explain what is going on here? I am
dropping the table before re-running the code each time.
I'm using SQL Server 2000 if that makes a difference.
Thanks.
Jon. 7 5248
Not a bug. As you rightly suspected, SQL Server tries to validate your
code first by attempting to resolve any column or object references to
existing objects and columns. If the table doesn't exist at compile time
then the resolution of that table's columns is deferred until the
statement executes. However, if the table exists then you will receive
an error if the referenced columns don't also exist. A workaround is to
put a GO batch separator after the ALTER TABLE statement so that the
second batch will be compiled independently.
For this reason among others it is good practice always to separate DDL
(Data Definition Language, such as CREATE and ALTER table statements)
and DML (Data Manipulation Language, such as SELECT, UPDATE, INSERT,
DELETE statements). I would create separate scripts for your DDL and DML
statements.
--
David Portas
SQL Server MVP
--
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
David Portas wrote: Not a bug. As you rightly suspected, SQL Server tries to validate your code first by attempting to resolve any column or object references to existing objects and columns. If the table doesn't exist at compile time then the resolution of that table's columns is deferred until the statement executes.
David,
Thanks, this does make sense however the table in my statement did not exist
at compile time (otherwise the create table line would fail), so that is
why I cannot understand why I get that message.
Thanks.
Jon.
Jon Combe (jc****@acxiom.co.uk) writes: Thanks, this does make sense however the table in my statement did not exist at compile time (otherwise the create table line would fail), so that is why I cannot understand why I get that message.
Your batch gets compiled several times. First you have:
CREATE TABLE JCTable ( CustomerName varchar(50) )
ALTER TABLE JCTable ADD CustomerNo int
INSERT INTO JCTable ( CustomerName , CustomerNo )
VALUES ( 'Jon Combe' , 1 )
INSERT INTO JCTable ( CustomerName , CustomerNo )
VALUES ( 'Bill Gates' , 1 )
UPDATE JCTable SET CustomerNo = 2 WHERE CustomerName = 'Jon Combe'
SELECT * FROM JCTable
On the first compile, all but the first statement is deferred. Once the
table has been created, SQL Server hits the ALTER TABLE, finds that the
statement is deferred, and recompiles the batch. This time, all statements
are scrutinized, since once all tables in a query exist, SQL Server per-
form full checks on the query.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Erland Sommarskog wrote: Your batch gets compiled several times. First you have:
CREATE TABLE JCTable ( CustomerName varchar(50) ) ALTER TABLE JCTable ADD CustomerNo int INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Jon Combe' , 1 ) INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Bill Gates' , 1 ) UPDATE JCTable SET CustomerNo = 2 WHERE CustomerName = 'Jon Combe' SELECT * FROM JCTable
On the first compile, all but the first statement is deferred. Once the table has been created, SQL Server hits the ALTER TABLE, finds that the statement is deferred, and recompiles the batch. This time, all statements are scrutinized, since once all tables in a query exist, SQL Server per- form full checks on the query.
Thanks Erland,
Why doesn't it spot that I've added a column and then either defer the last
three statements again, or recognise that the column I'm using does now
exist? I'd expect that sort of behaviour from it although I take the point
made earlier that it's best to split the statements with a GO between them.
Also given the way it is compiling the code it doesn't explain why if I run
these statements one line at a time, then drop the table, reload Query
Analyser and re-run this full batch of code it works yet running the full
batch before the table has ever existed generates an error. That just seems
weird, so if anyone can explain why I'd be interested to hear it! The
database should be in the same state in both cases, but as the behaviour is
different it can't be.
Jon.
Jon Combe wrote: Erland Sommarskog wrote:
Your batch gets compiled several times. First you have:
CREATE TABLE JCTable ( CustomerName varchar(50) ) ALTER TABLE JCTable ADD CustomerNo int INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Jon Combe' , 1 ) INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Bill Gates' , 1 ) UPDATE JCTable SET CustomerNo = 2 WHERE CustomerName = 'Jon Combe' SELECT * FROM JCTable
On the first compile, all but the first statement is deferred. Once the table has been created, SQL Server hits the ALTER TABLE, finds that the statement is deferred, and recompiles the batch. This time, all statements are scrutinized, since once all tables in a query exist, SQL Server per- form full checks on the query.
Thanks Erland,
Why doesn't it spot that I've added a column and then either defer the last three statements again, or recognise that the column I'm using does now exist? I'd expect that sort of behaviour from it although I take the point made earlier that it's best to split the statements with a GO between them.
Also given the way it is compiling the code it doesn't explain why if I run these statements one line at a time, then drop the table, reload Query Analyser and re-run this full batch of code it works yet running the full batch before the table has ever existed generates an error. That just seems weird, so if anyone can explain why I'd be interested to hear it! The database should be in the same state in both cases, but as the behaviour is different it can't be.
Jon.
I have to confess that this behaviour, if it is "normal", is surprising
to me. Is this the way the product is expected to behave?
Thanks.
--
Daniel A. Morgan
University of Washington da******@x.washington.edu
(replace 'x' with 'u' to respond)
Jon Combe (jc****@acxiom.co.uk) writes: Why doesn't it spot that I've added a column
No, you haven't added a column. You get the error the table has been
created, but the ALTER TABLE statement has not been executed. Since the
ALTER TABLE statement was deferred, SQL Server recompiles the batch,
and it recompiles the batch, because that the lowest granularity for
compilation in SQL 2000. And since at this point the columns does not
exist, the compilation fails.
and then either defer the last three statements again,
SQL Server could defer compilation because of unknown columns too, but it
has quite some ramifications, and I am very happy that unknown columns is
reason for deferral. It is bad as it is. To wit, when you create a stored
procedure, you want to be alerted if you have misspelled a table name of a
column name. Due to deferred name resolution, you don't get alerts for
misspelled table names, but since SQL Server checks the query once all
tables are there, you do at least sometimes get alerts about misspelling
column names. (And in our in-house load tool, I scan the code for table
references to find the missing tables, and also perform some tricks to
get SQL Server check queries with temp tables too.
But, there is light at the end of the tunnel. Your script runs as you
expected in SQL 2005. This is because SQL 2005 is able to recompile a
single statement in a batch, so the entire batch is not recompiled at
once.
Also given the way it is compiling the code it doesn't explain why if I run these statements one line at a time, then drop the table, reload Query Analyser and re-run this full batch of code it works yet running the full batch before the table has ever existed generates an error. That just seems weird, so if anyone can explain why I'd be interested to hear it! The database should be in the same state in both cases, but as the behaviour is different it can't be.
This has to do with cached plans. The plans are in the cache, even if
the table is dropped. (This makes sense with temp tables.) Yes, it's
certainly a bit confusing, but for the situations for which the behaviour
is designed, it gives the best result.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Erland Sommarskog wrote: Also given the way it is compiling the code it doesn't explain why if I run these statements one line at a time, then drop the table, reload Query Analyser and re-run this full batch of code it works yet running the full batch before the table has ever existed generates an error. That just seems weird, so if anyone can explain why I'd be interested to hear it! The database should be in the same state in both cases, but as the behaviour is different it can't be.
This has to do with cached plans. The plans are in the cache, even if the table is dropped. (This makes sense with temp tables.) Yes, it's certainly a bit confusing, but for the situations for which the behaviour is designed, it gives the best result.
So basically with the query I have, whether I get an error or not (when the
database is in the same state) depends entirely on what has been run
previously? That doesn't seem very satisfactory!
Jon. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dylan Nicholson |
last post by:
Seems that Oracle 9.2 (using MS ODBC driver) requires extra
parentheses when adding multiple columns to a table:
ALTER TABLE MyTable ADD...
|
by: Lannsjo |
last post by:
I need to change my primary key column type from smallint to int.
I have tried:
ALTER TABLE livegroup MODIFY id INT UNSIGNED NOT NULL...
|
by: anzenews |
last post by:
Hello!
I have a weird problem... The application I use issues this SQL from
time to time:
alter table t modify id int(6) unique not null...
|
by: maricel |
last post by:
Could someone confirm which tablespace is being used when running ALTER &
CREATE INDEX.
Is it the tempspace or the tablespace where the table...
|
by: BuddhaBuddy |
last post by:
Platform is DB2/NT 7.2.9
The table was created like this:
CREATE TABLE MYTEST (
MYTESTOID bigint not null primary key,
FK_OTHEROID bigint not...
|
by: RamaKrishna Narla |
last post by:
In MS SQL Server, I have the following tables with some data in it.
create table table1 (
column1 varchar(32),
column2 int not null,...
|
by: Serge Rielau |
last post by:
Hi all,
Following Ian's passionate postings on problems with ALTOBJ and the
alter table wizard in the control center I'll try to explain how to...
|
by: vasilip |
last post by:
I'm testing out db2 for a project I'm starting that requires proper xml
support and I can't seem to get both xml and spatial data to work well
in...
|
by: raylopez99 |
last post by:
Keep in mind this is my first compiled SQL program Stored Procedure
(SP), copied from a book by Frasier Visual C++.NET in Visual Studio
2005...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |