Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case Statements and Stored Procedures

Status
Not open for further replies.

lyric0n

Technical User
Dec 28, 2005
74
Hello,

I would like to pass 3 variables to a Stored Procedure, then query a table on one of those variables, and if there are no results in that table, then insert the data, if there is data, I need to update the data. I have pasted what I have below but ran into issues checking my results, any help is appreciated.

Thanks,
Chris


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE dbo.SP
@IPAddr Varchar(50),
@Int Varchar(50),
@MACAddr Varchar(50)
AS
BEGIN
SELECT MACAddr FROM dbo.Table WHERE MACAddr = '@MACAddr'

--Here is where I draw a blank...
END
GO
 
Code:
CREATE PROCEDURE dbo.SP
    @IPAddr Varchar(50),
    @Int Varchar(50),
    @MACAddr Varchar(50)
AS
BEGIN
  If Exists(SELECT MACAddr FROM dbo.Table WHERE MACAddr = @MACAddr)
    Begin
      Update Table
      Set    Int = @Int,
             IPAddr = @IPAddr
      Where  MacAddr = @MacAddr
    End
  Else
    Begin

      Insert Into Table(MacAddr, IPAddr, Int) Values(@MaxAddr, @IPAddr, @Int)

    End
END

-George

"the screen with the little boxes in the window." - Moron
 
then query a table on one of those variables, and if there are no results in that table, then insert the data, if there is data, I need to update the data.

First, what table, what data, and update it with what?

Next, SELECT MACAddr FROM dbo.Table WHERE MACAddr = '@MACAddr' will not work - you need to remove the single quotes from around the variable name...
[dazed]

Silence is golden.
Duct tape is silver.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top