0

运行此程序后,我无法选择或删除表。

我也从来没有得到回滚或错误

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
4

1 回答 1

5

首先:在你INSERT的s结束时,如果一切正常,你就需要进行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!

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

您没有提及您使用的是什么版本的 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
于 2012-07-29T11:24:46.637 回答