Thursday, 21 April 2016

Important SQL Query asked in Interview

Important SQL Query asked in Interview

SQL query to check a record in table, if exist then insert else update the record.



BEGIN
  IF EXISTS (SELECT * FROM [users] WHERE email=@email)
    UPDATE [users] SET updated_date = GETDATE() WHERE email=@email
ELSE
    INSERT INTO [users] (uid,first_name,last_name,email,password,gender,is_active,reg_date)
VALUES (NEWID(),@fname,@lname,@email,@password,@gender,1,GETDATE())
END

Stored procedure to return id of last inserted record:

@id int output
AS
BEGIN


INSERT INTO [table] (uid,ad_title,contact_person)
VALUES (@uid,@adtitle,@contactperson)

SET @id=SCOPE_IDENTITY()
      RETURN  @id

END

No comments:

Post a Comment