Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

conditional declaration of local variables in stored procedure

Posted on 2004-10-05
Last Modified: 2009-12-16
I have a stored procedure that i'm using to either INSERT or UPDATE a record and its dependants. It will do the UPDATE ok but fails the INSERT because I am declaring 2 variables @cat_id1 and @cat_id2 for the whole proc. If it is an update then the webpage can pass the 2 ID's to the Stored Proc ok, for INSERT as there are no primary Keys I cannot pass them in...

So how can i conditionally declare my 2 variables?

-- =============================================
-- Create procedure to add / update contact
-- Author: Simon Martin 27/09/04
-- =============================================
-- creating the store procedure
         FROM   sysobjects
         WHERE  name = 'usp_AddUpdateContact'
         AND         type = 'P')
    DROP PROCEDURE usp_AddUpdateContact

CREATE PROCEDURE usp_AddUpdateContact

      @first_name varchar(50),
      @last_name varchar(50),
      @honorific_id int,
      @sex_id int,
      @address1 varchar(50),
      @address2 varchar(50),
      @street varchar(50),
      @town varchar (50),
      @county varchar (50),
      @postcode char(8),
      @region int,
      @phone varchar(20),
      @mobile varchar(20),
      @email varchar(50),
      @notes text,
      @cat_id1 int,
      @cat_id2 int,
      @cat_name1 varchar(50),
      @cat_name2 varchar(50),
      @cat_sex1 int,
      @cat_sex2 int,
      @age_id1 int,
      @age_id2 int,

      @vet_id int


-- Declare and initialise local variables
DECLARE @action char(1), -- 'I' = Insert; 'U' = Update
      @contact_id int

-- Determine action taken to cause trigger
IF EXISTS (SELECT c.contact_id FROM tbl_contact c WHERE c.postcode = @postcode AND c.address1 = @address1)
      SET @action = 'U'
      SET @action = 'I'

IF EXISTS (SELECT c.contact_id FROM tbl_contact c WHERE c.postcode = @postcode AND c.address1 = @address1)
      SET @contact_id = (SELECT c.contact_id FROM tbl_contact c WHERE c.postcode = @postcode AND c.address1 = @address1)

-- Acutal trigger logic starts here
IF @action = 'U'
            UPDATE tbl_contact
            SET first_name = @first_name, last_name = @last_name, honorific_id = @honorific_id, sex_id = @sex_id, address1 = @address1, address2 = @address2, street = @street, town = @town, county = @county, postcode = @postcode, region = @region, phone = @phone, mobile = @mobile, email = @email, notes = @notes
            WHERE contact_id = @contact_id

            -- Update linked table with cats details
            UPDATE tbl_cat
            SET cat_name = @cat_name1, cat_sex = @cat_sex1, age_id = @age_id1, contact_id = @contact_id
            WHERE cat_id = @cat_id1
            UPDATE tbl_cat
            SET cat_name = @cat_name2, cat_sex = @cat_sex2, age_id = @age_id2, contact_id = @contact_id
            WHERE cat_id = @cat_id2
IF @action = 'I'
            INSERT INTO tbl_contact (first_name, last_name, honorific_id, sex_id, address1, address2, street, town, county, postcode, region, phone, mobile, email, notes)
            VALUES (@first_name, @last_name, @honorific_id, @sex_id, @address1, @address2, @street, @town, @county, @postcode, @region, @phone, @mobile, @email, @notes)
                  -- Pick up new id to create child relationship
                  DECLARE @Newid int;
                  SET @Newid = (SELECT @@IDENTITY)

-- Create details for cat1 and voucher

            INSERT INTO tbl_cat (cat_name, cat_sex, age_id, contact_id)
            VALUES (@cat_name1, @cat_sex1, @age_id1, @Newid)
                  DECLARE @NewCat1 int;
                  SET @NewCat1 = (SELECT @@IDENTITY)
            INSERT INTO tbl_voucher (region_id, contact_id, cat_id, vet_id)
            VALUES (@region, @Newid, @NewCat1, @vet_id)

-- Create details for cat2 and voucher

            INSERT INTO tbl_cat (cat_name, cat_sex, age_id, contact_id)
            VALUES (@cat_name2, @cat_sex2, @age_id2, @Newid)
                  DECLARE @NewCat2 int;
                  SET @NewCat2 = (SELECT @@IDENTITY)
            INSERT INTO tbl_voucher (region_id, contact_id, cat_id, vet_id)
            VALUES (@region, @Newid, @NewCat2, @vet_id)
Question by:catprotection
LVL 69

Accepted Solution

Scott Pletcher earned 450 total points
ID: 12228048
You can make any parameter optional by giving it a default value in the parm declaration:

    @cat_id1 int = NULL,
    @cat_id2 int = NULL,

Author Comment

ID: 12228151
One word...


Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question