IF EXiSTS -- Please help

  • Hi,

    I have a problem with my stored procedure, What I'm trying to do here, is that the stored procedure will check if the username exists, if yes, then it check for machineID = @machinename ( name issued as parameter), if yes update status = 1 , if the user does not exists inset the user, status and machine name.

    Please check if the below script is correct?

    CREATE PROCEDURE ***

    @macname varchar(30),

    @user varchar(30)

    AS

    --Select user from table1 where tuser =@user

    If (Macid= @macname)

    begin

    update table1 SET Macid='1'

    where user=@user

    end

    else

    begin

    Insert Into table1(user,macID, Status)

    Select @user, @macname, '1'

    END

    Thanks in advance

  • Sqldba,

    CREATE PROCEDURE ***

    @macname varchar(30),

    @user varchar(30)

    AS

    --Select user from table1 where tuser =@user

    If (Macid= @macname)

    begin

    update table1 SET Macid='1'

    where user=@user

    end

    else

    begin

    Insert Into table1(user,macID, Status)

    Select @user, @macname, '1'

    END

    Change the IF part as below

    IF exists ( select 1 from table1 where macID = @macname)

    karthik

  • You say you want to update Status = 1 if username exists. On your update statement, are you doing that right as I would of thought you would do something like:

    update table1 SET Status = '1' where user = @user

    Also, I think for the check, you could do something like:

    if exists (Select user from table1 where tuser = @user)

    begin

    assuming tuser is correct.

    Also, im not 100% on SQL, but can you do this:

    Insert Into table1(user,macID, Status)

    Select @user, @macname, '1'

    as I would of thought this is how you did it?:

    Insert Into table1(user,macID, Status)

    values (@user, @macname, '1')

  • Thank you,

    This is working fine

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply