IF EXISTS, THEN SELECT ELSE INSERT AND THEN SELECT

前端 未结 7 928
旧时难觅i
旧时难觅i 2020-12-12 21:10

How do you say the following in Microsoft SQL Server 2005:

IF EXISTS (SELECT * FROM Table WHERE FieldValue=\'\') THEN
   SELECT TableID FROM Table WHERE Fiel         


        
7条回答
  •  没有蜡笔的小新
    2020-12-12 21:53

    It sounds like your table has no key. You should be able to simply try the INSERT: if it’s a duplicate then the key constraint will bite and the INSERT will fail. No worries: you just need to ensure the application doesn't see/ignores the error. When you say 'primary key' you presumably mean IDENTITY value. That's all very well but you also need a key constraint (e.g. UNIQUE) on your natural key.

    Also, I wonder whether your procedure is doing too much. Consider having separate procedures for 'create' and 'read' actions respectively.

提交回复
热议问题