2012-07-29 55 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