If exist

Is this possible?


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.


SELECT * from ecart_countrydesc where EXISTS (Select `countryId` FROM ecart_countrydesc WHERE `langId` = 1 )

http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

I have no idea. Did you try it?

@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?

This thing works. Something new for me.


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

Ok, I understand that you’ve solved your problem then. Good job :slight_smile: