SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: IF EXIST

  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    IF EXIST

    Is this possible?

    Code:
    IF EXISTS (Select sn_id as snid FROM device.sn WHERE dname_id = 62 and sn_value = '123415')
            BEGIN
            
            SELECT MAX(id) AS maxid FROM device.list 
            
            INSERT INTO parts (sn_id,device_id) VALUES (snid, maxid)
            
            END
    ELSE
            BEGIN
              PRINT 'id does not exist'
            return 
            END

  2. #2
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by claro View Post
    Is this possible?

    Code:
    IF EXISTS (Select sn_id as snid FROM device.sn WHERE dname_id = 62 and sn_value = '123415')
            BEGIN
            
            SELECT MAX(id) AS maxid FROM device.list 
            
            INSERT INTO parts (sn_id,device_id) VALUES (snid, maxid)
            
            END
    ELSE
            BEGIN
              PRINT 'id does not exist'
            return 
            END
    i haven't seen any syntax in mysql instead were present EXISTS and NOT EXISTS.

    Code:
    SELECT * from ecart_countrydesc where EXISTS (Select `countryId` FROM ecart_countrydesc WHERE `langId` = 1 )
    http://dev.mysql.com/doc/refman/5.0/...ubqueries.html

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by claro View Post
    Is this possible?

    Code:
    IF EXISTS (Select sn_id as snid FROM device.sn WHERE dname_id = 62 and sn_value = '123415')
            BEGIN
            
            SELECT MAX(id) AS maxid FROM device.list 
            
            INSERT INTO parts (sn_id,device_id) VALUES (snid, maxid)
            
            END
    ELSE
            BEGIN
              PRINT 'id does not exist'
            return 
            END
    I have no idea. Did you try it?

  4. #4
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,241
    Mentioned
    239 Post(s)
    Tagged
    1 Thread(s)
    @claro ; It would be nice if you'd tell us which database you're using. While SQL Standard is common to all of them, the extenstions and additions to the language are not and not all databases do things the same way.

    Aslo... do the names of your tables have a dot? really?

  5. #5
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    This thing works. Something new for me.

    Code:
    DECLARE @snid int
    SET @snid = NULL
    Select @snid = sn_id FROM device.sn WHERE dname_id = 62 and sn_value = '123415'
    
    IF @snid IS NULL
    BEGIN
      PRINT 'id does not exist'
    END
    ELSE
    BEGIN
      DECLARE @maxid int
      SELECT @maxid = MAX(id) AS maxid FROM device.list 
      INSERT INTO parts (sn_id,device_id) VALUES (@snid, @maxid)
    END
    Last edited by molona; Jan 31, 2013 at 15:26. Reason: modifying code tags so they would work ;-)

  6. #6
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,241
    Mentioned
    239 Post(s)
    Tagged
    1 Thread(s)
    Ok, I understand that you've solved your problem then. Good job


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •