Search notes:

SQL Server: influence of syntax errors on batches

In contrast to locigal errors, an SQL batch that contains a syntactical error is not executed at all. This is demonstrated in the following example:
-- -------------------------------------------------------------------
--
-- First Batch. No syntax errors
--

create table tq84_batch (id integer identity, txt varchar(50));
insert into tq84_batch (txt) values ('Inserted in first batch.');

go

-- -------------------------------------------------------------------
--
-- Second batch: insert a few more records into the table.
--

insert into tq84_batch (txt) values ('Batch 2 - a');
insert into tq84_batch (txt) values ('Batch 2 - b');
insert into tq84_batch (txt) values ('Batch 2 - c');

go

-- -------------------------------------------------------------------
--
-- Third batch: This batch contains syntactical errors
--
-- The entire batch is not executed because it contains
-- a syntactic error.
--

insert into tq84_batch (txt) values ('Batch 3 - before error.');
commit;

   -- Note the typo in the next statement:
slect * from tq84_batch;

insert into tq84_batch (txt) values ('Batch 3 - after error.');

go

-- -------------------------------------------------------------------
--
-- fourth batch: show content of table and drop it.
--

select txt from tq84_batch order by id;
   --
   -- The select statement returns:
   --
   --     Inserted in first batch.
   --     Batch 2 - a
   --     Batch 2 - b
   --     Batch 2 - c
   --

drop table tq84_batch;

go
Github repository about-MSSQL, path: /sql/go/batches-with-syntax-errors.sql

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759406144, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/SQL-Server/sql/batch/batches-with-syntax-errors(89): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78