为什么这个插页会破坏我的桌子?

0 t-sql sql-server insert corruption insert-into

运行之后,我无法选择或删除表格.

我也没有得到回滚或错误

mytable:

cmid pk in not null
cmcid int null
cmctitle nvarchar(4000)
Run Code Online (Sandbox Code Playgroud)

查询:

begin transaction
INSERT INTO [mydatabasename].[dbo].[mytable]
           (cmcid,cmctitle)
         values(396,'*ADVANCED 2-D ART – Painting  & Drawing'),
(397,'Advanced 3D Art'),
(398,'AP Studio Art')
(399,'Digital Art'),
(400,'Intro to Visual Art'),
(401,'Bible 9 - Scripture'),
(402,'Bible 10 - God  & Christ'),
(403,'Bible 11 -Doctrine and World Religions'),
(404,'Bible 12 - Worldviews'),
(405,'Accounting'),
(406,'AP Macroeconomics'),
(407,'AP Microeconomics'),
(408,'Personal Finance'),
(409,'Introduction to Life Calling'),
(410,'*ACADEMIC SKILLS'),
(411,'*BASIC SKILLS TRAINING – Resource'),
(412,'Directed Studies'),
(413,'*INTERNATIONAL STUDENT SEMINAR'),
(414,'*STUDENT ACHIEVEMENT TRAINING'),
(415,'AP Language and Composition 11'),
(416,'American Literature 11'),
(417,'British Literature 12'),
(418,'Grammar  & Composition 9'),
(419,'*ENGLISH 10 – World Literature'),
(420,'Honors American Literature 11'),
(421,'Honors British Literature 12'),
(422,'Honors Dramatic Literature'),
(423,'Honors World Lit and Composition 10'),
(424,'World Literature  & Composition 10'),
(425,'*HONORS SHAKESPEAREAN COMEDIES'),
(426,'Speech'),
(427,'Yearbook'),
(428,'AP Calculus'),
(429,'AP Statistics'),
(430,'Algebra 1'),
(431,'Algebra 1 with Lab'),
(432,'Algebra 2'),
(433,'Geometry'),
(434,'Honors Geometry'),
(435,'PreCalculus'),
(436,'Introduction to Life Calling'),
(437,'Missions'),
(438,'*ADVANCED MUSIC THEORY'),
(439,'*ADVANCED ORCHESTRA'),
(440,'Concert Band'),
(441,'HS Chapel Praise  & Worship Team'),
(442,'Encore'),
(443,'Colla Voce'),
(444,'Musical Theatre'),
(445,'Jazz Band'),
(446,'Intro to Music Theory'),
(447,'HS Orchestra  & Ensemble'),
(448,'Theater Production'),
(449,'*THEATRE ARTS'),
(450,'Theater Production'),
(451,'*ADVANCED PE, STRENGTH  & CONDITIONING '),
(452,'Physical Education - Health'),
(453,'*HEALTH – Girls'),
(454,'Physical Education for Boys'),
(455,'Physical Education for Girls'),
(456,'Physical Education for Athletes'),
(457,'Physical Education Advanced Weight Training'),
(458,'AP Biology'),
(459,'*ADVANCED PLACEMENT CHEMISTRY'),
(460,'Bioethics'),
(461,'Biology 1'),
(462,'Botany'),
(463,'*BIOLOGY II - ZOOLOGY'),
(464,'Biomedical Sciences Professional Seminar'),
(465,'Chemistry'),
(466,'Honors Biology 1'),
(467,'Honors Bio II = Physiology'),
(468,'Honors Chemistry'),
(469,'Honors Physics'),
(470,'Integrated Chemistry-Physics'),
(471,'Zoology'),
(472,'*SCIENCE RESEARCH - INDEPENDENT STUDY'),
(473,'AP Government'),
(474,'AP US History'),
(475,'Civil War  & Reconstruction'),
(476,'Economics'),
(477,'Geography and History of the World'),
(478,'Psychology'),
(479,'Sociology'),
(480,'Government'),
(481,'US History'),
(482,'*TEACHER ASSISTANCE'),
(483,'Video Production'),
(484,'*ADVANCED PLACEMENT COMPUTER SCIENCE'),
(485,'*DATABASE APPLICATION'),
(486,'Computer Programming'),
(487,'*VIDEO PRODUCTION'),
(488,'Web Design'),
(489,'French 1'),
(490,'French 2'),
(491,'French 3'),
(492,'Honors French 4'),
(493,'Latin I'),
(494,'Latin II'),
(495,'Latin III'),
(496,'Spanish 1'),
(497,'Spanish 2'),
(498,'Spanish 3'),
(499,'Honors Spanish 4'),
(500,'Honors Spanish 5')

go
IF @@ERROR <> 0
BEGIN
   PRINT 'error!'
   ROLLBACK TRANSACTION
   RETURN
END
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 5

首先:在你INSERT的结束时,如果一切正常,你需要COMMIT交易!

否则,实际上没有数据插入到表中,并且您正在锁定表(因此您不能再从中进行选择 - 而该事务仍处于打开状态并等待已提交或回滚).

所以,你INSERT不会破坏你的表-你只是离开你的交易处于打开状态,且表(部分)锁定,而出现这种情况.

而且:如果它是一个NVARCHAR列,你应该总是N'在插入中使用前缀!像这样的东西:

INSERT INTO [mydatabasename].[dbo].[mytable](cmcid, cmctitle)
VALUES(396, N'*ADVANCED 2-D ART – Painting  &amp; Drawing')` . 
            *
            *-- this N prefix is important!
Run Code Online (Sandbox Code Playgroud)

否则,在插入支持Unicode的列之前,插入的字符串将转换回非Unicode字符串NVARCHAR!

您没有提到您正在使用的SQL Server 版本 - 如果您使用的是SQL Server 2005或更高版本,您应该使用TRY ... CATCHSQL Server机制来执行错误处理 - 例如:

BEGIN TRY
BEGIN TRANSACTION    -- Start the transaction

  INSERT .....

  COMMIT TRANSACTION  -- if successful - *COMMIT* your transaction
END TRY
BEGIN CATCH
  ROLLBACK TRANSACTION   -- if error, roll back your transaction
END CATCH
Run Code Online (Sandbox Code Playgroud)