IF EXISTS, THEN SELECT ELSE INSERT AND THEN SELECT

前端 未结 7 909
旧时难觅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:37
    create schema tableName authorization dbo
    go
    IF OBJECT_ID ('tableName.put_fieldValue', 'P' ) IS NOT NULL 
    drop proc tableName.put_fieldValue
    go
    create proc tableName.put_fieldValue(@fieldValue int) as
    declare @tableid int = 0
    select @tableid = tableid from table where fieldValue=''
    if @tableid = 0 begin
       insert into table(fieldValue) values('')
       select @tableid = scope_identity()
    end
    return @tableid
    go
    declare @tablid int = 0
    exec @tableid = tableName.put_fieldValue('')
    
    0 讨论(0)
  • 2020-12-12 21:41

    You just have to change the structure of the if...else..endif somewhat:

    if exists(select * from Table where FieldValue='') then begin
      select TableID from Table where FieldValue=''
    end else begin
      insert into Table (FieldValue) values ('')
      select TableID from Table where TableID = scope_identity()
    end
    

    You could also do:

    if not exists(select * from Table where FieldValue='') then begin
      insert into Table (FieldValue) values ('')
    end
    select TableID from Table where FieldValue=''
    

    Or:

    if exists(select * from Table where FieldValue='') then begin
      select TableID from Table where FieldValue=''
    end else begin
      insert into Table (FieldValue) values ('')
      select scope_identity() as TableID
    end
    
    0 讨论(0)
  • 2020-12-12 21:43
    IF EXISTS (SELECT 1 FROM Table WHERE FieldValue='') 
    BEGIN
       SELECT TableID FROM Table WHERE FieldValue=''
    END
    ELSE
    BEGIN
       INSERT INTO TABLE(FieldValue) VALUES('')
       SELECT SCOPE_IDENTITY() AS TableID
    END
    

    See here for more information on IF ELSE

    Note: written without a SQL Server install handy to double check this but I think it is correct

    Also, I've changed the EXISTS bit to do SELECT 1 rather than SELECT * as you don't care what is returned within an EXISTS, as long as something is I've also changed the SCOPE_IDENTITY() bit to return just the identity assuming that TableID is the identity column

    0 讨论(0)
  • 2020-12-12 21:51

    You were close:

    IF EXISTS (SELECT * FROM Table WHERE FieldValue='')
       SELECT TableID FROM Table WHERE FieldValue=''
    ELSE
    BEGIN
       INSERT INTO TABLE (FieldValue) VALUES ('')
       SELECT TableID FROM Table WHERE TableID=SCOPE_IDENTITY()
    END
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 21:58
    DECLARE @t1 TABLE (
        TableID     int         IDENTITY,
        FieldValue  varchar(20)
    )
    
    --<< No empty string
    IF EXISTS (
        SELECT *
        FROM @t1
        WHERE FieldValue = ''
    ) BEGIN
        SELECT TableID
        FROM @t1
        WHERE FieldValue=''
    END
    ELSE BEGIN
        INSERT INTO @t1 (FieldValue) VALUES ('')
        SELECT SCOPE_IDENTITY() AS TableID
    END
    
    --<< A record with an empty string already exists
    IF EXISTS (
        SELECT *
        FROM @t1
        WHERE FieldValue = ''
    ) BEGIN
        SELECT TableID
        FROM @t1
        WHERE FieldValue=''
    END
    ELSE BEGIN
        INSERT INTO @t1 (FieldValue) VALUES ('')
        SELECT SCOPE_IDENTITY() AS TableID
    END
    
    0 讨论(0)
提交回复
热议问题