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

时间:2012-07-29 11:17:32

标签: sql-server tsql insert corruption insert-into

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

我也没有得到回滚或错误

mytable的

cmid pk in not null
cmcid int null
cmctitle nvarchar(4000)

查询:

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

1 个答案:

答案 0 :(得分:5)

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

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

因此,您的INSERT 没有损坏您的表 - 您只是将您的交易保持在开放状态,并且在这种情况下(部分)锁定该表。< / p>

另外:如果它是NVARCHAR列,您应该始终在插入中使用N'前缀!像这样:

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

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

您没有提到您正在使用的SQL Server的版本 - 如果您使用的是SQL Server 2005 或更新,那么您应该使用{{ 3}}进行错误处理 - 类似于:

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