NEED HELP TO MODIFY STORED PROCEDURE CODE...

Last post 04-03-2007 3:11 PM by ndinakar. 4 replies.

Sort Posts:

  • NEED HELP TO MODIFY STORED PROCEDURE CODE...

    04-03-2007, 11:33 AM
    • Loading...
    • riaaa
    • Joined on 04-03-2007, 2:52 PM
    • Posts 5

    Pls check my code for the stored procedure which i created for the companydetails including

    companyid P.K. Not Null int(4),

    companyname Not Null varchar (20),

    address varchar(30)

    where companyid is the primary key and it should be autogenerate.

    I also want that it should check if the name exits or not.It should also check that the field is entered and not kept null.If it's null then should return the error message.

    I want to write the queries select,insert,update and delete in the single stored procedure.

    How can i differ all the query individually in a stored procedure.

    The select and insert query are done on the button click event whereas the update,delete queries are performed in the gridview link event. 

    Pls help me and modify my code accordingly with ur suggestions who know the stored procedure very well.First read what i want then give reply.

    waiting for the reply and with corrections.

    The coding is perfomed in sql server 2005 and asp.net with C# 2005, 

    1    ALTER PROCEDURE CompanyStoredProcedure
    2 @uspcompanyid int,
    3 @uspcompanyname varchar(20),
    4 @uspaddress1 varchar(30),
    5 @frmErrorMessage as varchar(256) OUTPUT,
    6 @RETURNVALUE as int OUTPUT,
    7 @RETURNID as int OUTPUT
    8 AS
    9 declare
    10 @companyid int,
    11 @companyname varchar(20),
    12 @address1 varchar(30)
    13
    14 BEGIN
    15
    16 begin
    17 Select
    @RETURNVALUE = -9
    18 RETURN -9
    19 end
    20
    21 begin
    22 Select
    @frmErrorMessage = 'The Operation Mode Has Not Been Specified' 23 return -9
    24 end
    25
    26
    27
    28 begin

    29 --validation... 30 if (@uspcompanyname is Null or @uspcompanyname = '')
    31 begin
    32 Select
    @RETURNVALUE = -9
    33 select @frmErrorMessage = 'Company Name is empty' 34 return -9
    35 end
    36 37 if
    exists (select companyid from companymaster
    38 where upper(companyname) = upper(cast(@uspcompanyname as varchar(20))))
    39 begin
    40 select
    @companyid = companyid from companymaster
    41 where upper(companyname)=upper(cast(@uspcompanyname as varchar(20) ) )
    42 end
    43 else
    44
    45 select
    @companyname = cast (@uspcompanyname as varchar(20))
    46 select @address1 = cast(@uspaddress1 as varchar(30))
    47 select @companyid = isnull(max(companyid),0) + 1 from companymaster
    48
    49 IF exists(SELECT * from companymaster where companyname=@companyname)
    50 begin
    51 Select
    @frmErrorMessage = 'Record With Company Name '
    52 + @companyname + ' is Already Exisiting For The Company Name '
    53 return -9
    54 end
    55
    56 -- the following codes inserts 57 begin transaction
    58 INSERT INTO
    companymaster
    59 ( companyname, address1)
    60 VALUES (@companyname,@address1)
    61 commit transaction
    62
    63 select
    @RETURNVALUE = 0
    64 select @RETURNID = @companyid
    65
    66 end
    67
    68
    69
    -- the following codes edit/updates 70 begin
    71 UPDATE
    companymaster
    72 SET companyname=@companyname,
    73 address1=@address1
    74 WHERE companyid =cast(@uspcompanyid as int)
    75
    76 select @RETURNVALUE = 0
    77 select @RETURNID = cast(@uspcompanyid as int)
    78 end
    79
    -- the following codes delete 80 begin
    81 DELETE
    companymaster WHERE (companyid = @companyid)
    82 end
    83
    84 END

    85
      

    Pls help me and modify my code accordingly with ur suggestions who know the stored procedure very well.First read what i want then give reply.

     

     

  • Re: NEED HELP TO MODIFY STORED PROCEDURE CODE...

    04-03-2007, 12:18 PM
    • Loading...
    • ndinakar
    • Joined on 05-05-2003, 4:57 PM
    • Redmond, WA
    • Posts 6,837
    • Moderator
      TrustedFriends-MVPs
    First read what i want then give reply.
    As much as I want to help it sounds more like a demand than a request. But since this is your first post I am assuming you are not yet aware of the 'Netiquette' and will catch up.
     
    Keeping that aside, Welcome to the forums.
     
    Your code is confusing. None of your code will be executed since you have a RETURN statement right at the beginning of the code.
    From your statement of requirements, you want to do some checks for any existing data and either INSERT or UPDATE accordingly. I do not understand when you want to a DELETE. Is there anything in the parameters that says 1= INSERT, 2 = UPDATe, 3 = DELETE? For INSERT/UPDATE if the record exists an UPDATE can be done else an INSERT. But its not so obvious for a DELETE.  (2) Please do not put line numbers in your code, when I try to cut/paste into my Query Analyzer to make the changes I have to manually remove all the line numbers.
     
    Also, I would recommend separate stored procedure for DELETE. In  your code you are trying to return if the @Company already exists. So do you want to do an update if the record already exists or just return? If you want to just exit out then there is no need for the UPDATE.
     
    Please rephrase your requirements before attempting to code.
    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
  • Re: NEED HELP TO MODIFY STORED PROCEDURE CODE...

    04-03-2007, 1:37 PM
    • Loading...
    • riaaa
    • Joined on 04-03-2007, 2:52 PM
    • Posts 5

    <quote>

    As much as I want to help it sounds more like a demand than a request. But since this is your first post I am assuming you are not yet aware of the 'Netiquette' and will catch up.

    </quote>

    No,it was not at all the demand.

    I have read on forums that if i mention that i have created my procedure in sql 2005 then they write the code for anyother database or suggest which is irrelevant to my post...

    Anyways,thanxs for welcoming me..Ofcourse thanxs for ur all answers.

    But,pls do help me for my this post about the stored procedure.The other posts are answered thanxs...

    I m new to sql and also to ur forum so help me...

    ya,i want  to do some checks for any existing data on either INSERT or UPDATE accordingly.

    No, there is not anything in the parameters that says 1= INSERT, 2 = UPDATE, 3 = DELETE?

    How can i do that in sql ??

    No,actually i don't want that For INSERT/UPDATE if the record exists an UPDATE can be done else an INSERT.I want that only the new records are inserted and old records updated.So,i think there should be use of parameters but i don't know how to use with this.I mean how to call that query thenafter in asp.net with C# 2005.As i have told above insert is done on button click event and update is done thru gridview.I have used sqldatasource to retrieve data.

    Sorry i have modified somewhat my code but now i don't know what should be done.I have removed line nos."sorry".

    OK,i will keep the separate procedure for the delete.
    .

     

     
       ALTER PROCEDURE CompanyStoredProcedure
           @uspcompanyid int,
        	@uspcompanyname varchar(20),
        	@uspaddress1 varchar(30),  
        	@frmErrorMessage as varchar(256)  OUTPUT,
        	@RETURNVALUE as int OUTPUT,
        	@RETURNID as int OUTPUT
        AS
        	declare
       	  @companyid int,
       	  @companyname varchar(20),
       	  @address1 varchar(30) 
       	  
       BEGIN
       	SET NOCOUNT ON	
              begin
       			Select @frmErrorMessage = 'The Operation Mode Has Not Been Specified'
       			return -9
       	    end
       	
       		
       	
      		begin	  
      	                    --validation...
                       if (@uspcompanyname is Null or @uspcompanyname = '')
       	    			begin
       		    			Select @RETURNVALUE = -9
       		    			select @frmErrorMessage = 'Company Name is empty'
       			    		return -9						
       				end 	
       
       		      if exists  (select companyid from companymaster 
       		                  where upper(companyname) = upper(cast(@uspcompanyname as varchar(20))))
       	         	begin
       			      select @companyid = companyid from companymaster 
                               where upper(companyname)=upper(cast(@uspcompanyname as varchar(20) ) )
       		        end
       		     else 
       		        				    
       				select @companyname	= cast (@uspcompanyname	as varchar(20))
       				select @address1	= cast(@uspaddress1 as varchar(30))
       				select @companyid = isnull(max(companyid),0) + 1 from companymaster	
       				 
       	IF exists(SELECT * from companymaster where  companyname=@companyname)
       	 	begin
      			Select @frmErrorMessage = 'Record With Company Name ' 
       			             + @companyname + ' is Already Exisiting For The Company Name ' 
       			return -9
      	  	end 	
       				
       				-- the following codes inserts	
       		begin transaction
       			IF NOT EXISTS( SELECT * FROM companymaster WHERE CompanyName = @companyname AND Address1 = address1)
    	      BEGIN
    		INSERT INTO companymaster
    			  ( companyname, address1)
    		VALUES     (@companyname,@address1)
    		SELECT @companyid = SCOPE_IDENTITY()
    	      END
       		commit transaction
       					
       				select @RETURNVALUE = 0
       				select @RETURNID = @companyid		
       							
       		end	
      						
            
                      -- the following codes edit/updates
       	    begin	
       	        UPDATE companymaster 
      	        SET companyname=@companyname,
       				address1=@address1 		
       			WHERE companyid =cast(@uspcompanyid as int)
       								
       			    select @RETURNVALUE = 0
       			    select @RETURNID = cast(@uspcompanyid as int)
       		 end
                                        
      	SET NOCOUNT OFF	 
        END 
      
    
     
  • Re: NEED HELP TO MODIFY STORED PROCEDURE CODE...

    04-03-2007, 2:00 PM
    • Loading...
    • riaaa
    • Joined on 04-03-2007, 2:52 PM
    • Posts 5

    Can anyone please help me to generate the sequence alphanumeric autogenerated id's in sql server in the above insert stored procedure.

    For E.g.

    Table 1 : companydetails 

    company id - P.K. 

    companyname

    address1

    Table 2 : itemdetails

    itemid - P.K.

    companyid - F.K.

    itemname

    description 

    Now, for companyid it should be C001,C002,C003......C999

                  itemid it should be I001,I002,I003.....I999

     thanxs

  • Re: NEED HELP TO MODIFY STORED PROCEDURE CODE...

    04-03-2007, 3:11 PM
    Answer
    • Loading...
    • ndinakar
    • Joined on 05-05-2003, 4:57 PM
    • Redmond, WA
    • Posts 6,837
    • Moderator
      TrustedFriends-MVPs
     
    ALTER PROCEDURE CompanyStoredProcedure
        @uspcompanyid INT NULL,
        @uspcompanyname VARCHAR(20),
        @uspaddress1 VARCHAR(30),
        @frmErrorMessage AS VARCHAR(256) OUTPUT,
        @RETURNVALUE AS INT OUTPUT,
        @RETURNID AS INT OUTPUT
    AS 
    BEGIN
    
    SET NOCOUNT ON
    
    /*
    
    RETURN_VALUE Comments
    1			Data Inserted
    2			Data Updated 
    -9			Other errors
    */
        DECLARE @companyid INT,
            @companyname VARCHAR(20),
            @address1 VARCHAR(30) 
    
    --validation...
    IF ( @uspcompanyname IS NULL OR @uspcompanyname = '' ) 
    	BEGIN
    		SET  @RETURNVALUE = -9
    		SET  @frmErrorMessage = 'Company Name is empty'
    		RETURN -9						
    	END 	
    
    IF EXISTS ( SELECT  * FROM Companymaster WHERE Companyid = @companyid ) 
    	BEGIN
    		UPDATE Company
    		SET companyname = @companyname,
    				address1 = @address1
    		WHERE    companyid= @uspcompanyid
    
    		SET  @frmErrorMessage = 'Company Name/Address  has been updated'
    		SET @RETURNVALUE = 2
    	END
    ELSE 
    	BEGIN
    		INSERT  INTO companymaster ( companyname, address1 )
    		VALUES  (@companyname,@address1)
    
    		SET  @frmErrorMessage = 'Company Name/Address info has been Inserted'
    		SET @RETURNVALUE = 1
    	END
    
    SET NOCOUNT OFF	 
    END 
    
    
     
    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter