2012-07-29 44 views
0

我运行后,我无法选择或删除表。这是为什么这插入损坏我的表?

我也从来没有回滚或错误

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 

回答

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! 

否则,插入你的字符串被插入Unicode的NVARCHAR柱之前被转换回非Unicode

你没有提到什么你使用SQL Server的版本 - 如果你的SQL Server上或更新版本,你应该使用TRY ... CATCH mechanism of SQL 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 
+0

这是所有真正的帮助和工作没有问题。非常感谢。我星期一上午需要这张桌子。你说什么是有道理的。我从来没有与SQL错误,所以我只是不打扰。但在这废话之后。在它周围添加它并不是很多。说实话它也困惑了我。开始,结束,开始尝试,开始交易.....以及例子,如果你不明白它,有时他们会搞砸,所以如果没有它,它就会有一些逻辑。与N'一样不知道,这似乎并不重要。我跑了它,它工作得很好,现在我可以享受我的假期。 – 2012-07-29 16:19:08

+0

BEGIN TRY BEGIN TRANSACTION - 启动交易 INSERT INTO [SQL2008R2_770505_clubs] [DBO] [Web_Classes_clink] (CCID,cctitle) 值 (396,N'* ADVANCED 2-d ART - 。绘画&绘图'), (397,N'Advanced 3D艺术')提交交易 - 如果成功 - * COMMIT *您的交易 END TRY BEGIN CATCH ROLLBACK TRANSACTION - 如果出错,回滚事务 END CATCH – 2012-07-29 16:23:58

+0

@misterg :如果这个答案帮助你解决了你的问题,你应该[**接受这个答案**](http://meta.stackexchange.com/q/5234/153998)。这将表明你对那些花时间帮助你的人表示感谢。 – 2012-07-29 19:22:17