Monday, 19 January 2015

Procedure Example



  1.         Transaction examples (270914)
    -- Author:  Mohd Fahim 
    -- Create date: 31 May 2011 
    -- Description: Exhiitor Company Details 
    -- ============================================= 
     
    Create PROCEDURE [dbo].[uspExhCompDesc] 
    (   
      @eventid nvarchar(max) =NULL, 
      @exid nvarchar(max) =NULL, 
      @fname nvarchar(max) =NULL,  
            @lName nvarchar(max) =NULL,   
         @company_name nvarchar(max) =NULL,  
            @address nvarchar(max) =NULL,  
            @field1 nvarchar(max) =NULL,  
            @aptemail nvarchar(max) =NULL,  
            @city nvarchar(max) =NULL,  
            @state nvarchar(max) =NULL, 
            @country nvarchar(max) =NULL, 
            @zip nvarchar(max) =NULL, 
            @telephone nvarchar(max) =NULL, 
            @fax nvarchar(max) =NULL, 
            @boothNumber nvarchar(max) =NULL, 
            @email nvarchar(max) =NULL, 
            @expdesc nvarchar(max) =NULL, 
            @url nvarchar(max) =NULL, 
            @Salutation nvarchar(max) =NULL, 
            @Mobile nvarchar(max) =NULL, 
            @prefix nvarchar(max) =NULL, 
            @Type nvarchar(250)=NULL, 
            @MessageOut varchar(1000) ='' output 
     
     ) 
     
    AS 
    BEGIN  
       SET NOCOUNT ON; 
    BEGIN TRANSACTION 
     
     IF @Type='Update_Data'  
         BEGIN 
               update tblExRegInfo set fname=@fname,lName=@lName,company_name=@company_name,address=@address, 
               field1=@field1,aptemail=@aptemail,city=@city,state=@state,country=@country,zip=@zip,telephone=@telephone, 
               fax=@fax,confirm_status='Yes',boothNumber=@boothNumber,email=@email,prefix=@Salutation,field2=@Mobile,field3=@prefix where eventid=@eventid and exid=@exid 
                
               update tblexadditionalinfo set expdesc=@expdesc,url=@url where eventid=@eventid and exid=@exid  
                   
               set @MessageOut='Success'         
              
           End  
     
         IF @TYPE='Select_Data' 
            BEGIN 
               select ex.*,exadd.url,exadd.expdesc from tblexreginfo ex,tblexadditionalinfo exadd where  
                ex.eventid=@eventid and ex.exid=@exid and exadd.eventid=@eventid and exadd.exid=@exid and  
               (ex.confirm_status='yes' or ex.confirm_status='Yes') 
            End  
    COMMIT TRANSACTION           
    END

2. Dynamic query for search procedure example
USE [ATCA15]
GO
/****** Object:  StoredProcedure [dbo].[uspManageATCAMemberDirectory]    Script Date: 3/7/2015 7:15:39 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[uspManageATCAMemberDirectory] --'SelectIndiMemberSearchedByUserParam','','Pradeep','',null,null,null,null
(
@ActionType nvarchar(50)=null,   
@SearchType nvarchar(50)=null,
@FirstName nvarchar(50)=null,
@LastName nvarchar(50)=null,
@CompanyName nvarchar(50)=null,
@CountryName nvarchar(50)=null,
@State nvarchar(50)=null,
@OrderBy nvarchar(50)=null,
@Message nvarchar(max)=null output 
)
AS
------Created Date 03/03/15-------

--Procedure is used to bind and serach ATCA Corporate as well as Indivisual Member
--------Amod-------------------------
Begin
set nocount off
---------Begin to select corporate member------------

if(@ActionType='SelectCorpMember')
Begin
if(@SearchType='View All')
select Company_Name, Logo,Add_Line1+' '+ State+', '+PostCode as Address from TblMember_Registration where Membership_Type='corporate' and Delete_Status='No' and confirm_status='Approved' order by company_name
else if(@SearchType='0-9')
select Company_Name, Logo,Add_Line1+' '+ State+', '+PostCode as Address from TblMember_Registration where Membership_Type='corporate' and Delete_Status='No' and confirm_status='Approved' and company_name between '0' and '9' order by company_name
else
select Company_Name, Logo,Add_Line1+' '+ State+', '+PostCode as Address from TblMember_Registration where Membership_Type='corporate' and Delete_Status='No' and confirm_status='Approved' and company_name like ''+@SearchType+'%'  order by company_name
End


if(@ActionType='SelectIndiMember')
Begin
if(@SearchType='View All')
select upper(FName+' '+isnull(LName,'')) as Name, Company_Name, City+', '+State+', '+ Add_Line1+' '+PostCode as Address,Logo from TblMember_Registration where Membership_Type='Individual' and Delete_Status='No' and confirm_status='Approved' order by company_name
else if(@SearchType='0-9')
select upper(FName+' '+isnull(LName,'')) as Name, Company_Name, City+', '+State+', '+ Add_Line1+' '+PostCode as Address,Logo from TblMember_Registration where Membership_Type='Individual' and Delete_Status='No' and confirm_status='Approved' and company_name between '0' and '9' order by company_name
else
select upper(FName+' '+isnull(LName,'')) as Name, Company_Name, City+', '+State+', '+ Add_Line1+' '+PostCode as Address,Logo from TblMember_Registration where Membership_Type='Individual' and Delete_Status='No' and confirm_status='Approved' and company_name like ''+@SearchType+'%'  order by company_name
End

if(@ActionType='SelectIndiMemberSearchedByUserParam')
Begin

Declare @SQLQuery AS NVarchar(4000)
Set @SQLQuery = 'select upper(FName+'' ''+isnull(LName,'''')) as Name,Company_Name, City+'', ''+State+'', ''+ Add_Line1+'' ''+PostCode as Address,Logo from TblMember_Registration where Membership_Type=''Individual'' and Delete_Status=''No'' and confirm_status=''Approved'' ' 
    
If (@FirstName Is Not Null and @FirstName<>'')
Set @SQLQuery = @SQLQuery + ' And (FName like '''+@FirstName+'%'')'

If (@LastName Is Not Null  and @LastName<>'')
Set @SQLQuery = @SQLQuery + ' And (LName like '''+@LastName+'%'')'

If (@CompanyName Is Not Null and @CompanyName<>'')
Set @SQLQuery = @SQLQuery + ' And (Company_Name like '''+@CompanyName+'%'')'

If (@CountryName Is Not Null and @CountryName<>'')
Set @SQLQuery = @SQLQuery + ' And (Country like '''+@CountryName+'%'')'

If (@State Is Not Null and @State<>'')
Set @SQLQuery = @SQLQuery + ' And (State like '''+@State+'%'')'

Set @SQLQuery = @SQLQuery + ' order by '+@OrderBy+''
 
-- print @SQLQuery
exec(@SQLQuery)

End
---------End to select corporate member------------

set nocount on

End

3. Use of out put and error message and scope identity

USE [ATCA15]
GO
/****** Object:  StoredProcedure [dbo].[Ins_Speaker_Moderator_Regis]    Script Date: 4/2/2015 8:40:27 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


--execute [Ins_speaker_moderator_regis] '1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1',
--'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'

ALTER PROCEDURE [dbo].[Ins_Speaker_Moderator_Regis]
@UniId NVARCHAR(400),
@eventid NVARCHAR(20),
@Prefix NVARCHAR(50),
@Fname NVARCHAR(200),
@Lname NVARCHAR(200),
@Suffix NVARCHAR(50),
@Nick_Name NVARCHAR(200),
@Organization NVARCHAR(500),
@Title NVARCHAR(200),
@Department NVARCHAR(500),
@Address1 NVARCHAR(200),
@Address2 NVARCHAR(200),
@City NVARCHAR(200),
@State NVARCHAR(200),
@PostCode NVARCHAR(50),
@Country NVARCHAR(200),
@Telephone NVARCHAR(50),
@Cell NVARCHAR(20),
@Email NVARCHAR(200),
@Session_Time NVARCHAR(100),
@Fee NVARCHAR(50),
@Speaking_Date NVARCHAR(100),
@Speaker_YesNo NVARCHAR(20),
@Reception_Attend_YesNo NVARCHAR(20),
@Permission_Recorded_YesNo NVARCHAR(50),
@OrderID NVARCHAR(200),
@ClientIp NVARCHAR(50),
@Track nvarchar(200),
@USCitizen nvarchar(50),
@ContactEmail nvarchar(200),
@GoingFaa nvarchar(100),
@MemberUniId nvarchar(200),
@Meeting nvarchar(200),

--Variables for Visitor International User
 @IntPageType nvarchar(50) ,
 @IntFName nvarchar(100),
 @IntMName nvarchar(100) ,
 @IntLName nvarchar(100) ,
 @IntAddress1 nvarchar(200) ,
 @IntAddress2 nvarchar(200) ,
 @IntCity nvarchar(100) ,
 @IntState nvarchar(100) ,
 @IntPostCode nvarchar(100) ,
 @IntCountry nvarchar(100) ,
 @IntGender nvarchar(100) ,
 @IntDateOfBirth nvarchar(100) ,
 @IntBirthCity nvarchar(100) ,
 @IntBirthCountry nvarchar(100) ,
 @IntCurrentCitizenship nvarchar(100) ,
 @IntDualCitizenship nvarchar(100) ,
 @IntPassportNum nvarchar(50) ,
 @IntPassportIssueBy nvarchar(50) ,
 @IntPassportExpirationDate nvarchar(100) ,
 @IntVisaType nvarchar(50) ,
 @IntVisaExpirationDate nvarchar(100) ,
 @IntPermanentResident nvarchar(100) ,
 @IntStc nvarchar(50) ,
 @IntCtc nvarchar(50) ,
 @IntCcc nvarchar(50) ,
 @IntJobTitle nvarchar(100) ,
 @IntEmployeeName nvarchar(100) ,
 @IntEmpAddress1 nvarchar(100) ,
 @IntEmpAddress2 nvarchar(100) ,
 @IntEmpCity nvarchar(100) ,
 @IntEmpState nvarchar(100) ,
 @IntEmpPostCode nvarchar(100) ,
 @IntEmpCountry nvarchar(100) ,
 @IntCreByIp nvarchar(50),
 @Result INT OUTPUT,
 @UserIdOut [NVARCHAR] (200) OUTPUT,
 @PasswordOut [NVARCHAR] (200) OUTPUT

 --select * from tbl_speaker_moderator_regis
 -- select * from tblInternational_Visitor
 --delete from tbl_speaker_moderator_regis where uniid='1'
-- delete from tblInternational_Visitor
AS
  BEGIN
      BEGIN try
          BEGIN TRANSACTION

 if exists(select 'E' from tblmember_registration where Email=@Email)
Begin
declare @MainMemberUniId nvarchar(100);
if(@MemberUniId is not null or @MemberUniId<>'')
set @MainMemberUniId=@MemberUniId
else
set @MainMemberUniId=(select UniId from tblmember_registration where Email=@Email)

-- Insertion for tbl_speaker_moderator_regis

INSERT INTO dbo.tbl_speaker_moderator_regis(uniid, eventid, prefix, fname, lname, suffix, nick_name,organization,title, department, address1, address2,city,state, postcode,
 country, telephone,cell, email, session_time, fee, speaking_date, speaker_yesno,reception_attend_yesno, permission_recorded_yesno,
  entry_date, clientip,Track,USCitizen,ContactEmail,GoingFaa,MemberUniId,Meeting)
          VALUES      ( @UniId, @eventid, @Prefix, @Fname, @Lname, @Suffix,@Nick_Name,@Organization, @Title, @Department, @Address1,@Address2, @City,@State, @PostCode,
@Country, @Telephone, @Cell, @Email, @Session_Time, @Fee, @Speaking_Date,@Speaker_YesNo,@Reception_Attend_YesNo, @Permission_Recorded_YesNo,
Getdate(), @ClientIp,@Track,@USCitizen,@ContactEmail,@GoingFaa,@MainMemberUniId,@Meeting)

-- Insertion for internationalvisitorregistration

insert into dbo.tblInternational_Visitor(
SpeakerUniId, PageType, FName, MName, LName, Address1, Address2, City, State, PostCode, Country, Gender,
DateOfBirth, BirthCity, BirthCountry, CurrentCitizenship, DualCitizenship, PassportNum, PassportIssueBy, PassportExpirationDate,
 VisaType, VisaExpirationDate, PermanentResident, Stc, Ctc, Ccc, JobTitle, EmployeeName, EmpAddress1, EmpAddress2,
  EmpCity, EmpState, EmpPostCode, EmpCountry, CreById, CreByIp)
values(@UniId,  @IntPageType, @IntFName, @IntMName, @IntLName, @IntAddress1, @IntAddress2, @IntCity, @IntState, @IntPostCode, @IntCountry, @IntGender,
@IntDateOfBirth, @IntBirthCity, @IntBirthCountry, @IntCurrentCitizenship, @IntDualCitizenship, @IntPassportNum, @IntPassportIssueBy, @IntPassportExpirationDate,
@IntVisaType, @IntVisaExpirationDate, @IntPermanentResident, @IntStc, @IntCtc, @IntCcc, @IntJobTitle, @IntEmployeeName, @IntEmpAddress1, @IntEmpAddress2,
 @IntEmpCity, @IntEmpState, @IntEmpPostCode, @IntEmpCountry, @MainMemberUniId, @IntCreByIp )


  IF( @Fee IS NOT NULL OR @Fee <> '' )
            BEGIN
                INSERT dbo.tblmemberorders
(memberuniid, membersubscriptionuniid, orderid, prodstoreorderid, producttype, productdesc, totalamount, paymentstatus, orderdate,
billcontactname, billadr1, billadr2, billcity, billstate, billpostcode, billcountry, clientip, entry_date)
VALUES (@MemberUniId, Null, @OrderID, NULL, 'ATCA-Tech Symposium 2015-SPEAKER-MODERATOR REGISTRATION' , 'Non Member', @Fee, 'Not Paid', Getdate(),
@Fname + ' ' + @Lname, @Address1, @Address2, @City, @State, @PostCode, @Country, @ClientIp, Getdate() )
            END

set @Result=1;
set @UserIdOut='';
set @PasswordOut='';

End
 else
Begin
-- Insertion for tblmember_registration

declare @UserId varchar(12);
declare @MainUserId varchar(12);
set @userid=(SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RandomNumber]);
set @MainUserId= 'ATCA'+@userid;

declare @Password varchar(12);
set @Password=(SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RandomNumber]);

INSERT INTO dbo.tblmember_registration (
eventid,email,company_name,prefix,suffix,military_rank,job_title,dob,service_branch,fname,lname,cont_fname,cont_lname,
add_line1,add_line2,city,[state],postcode,country,geog_region,phone1,phone2,fax_phone,extnsn,logo,logid,passwrd,entry_date
,update_date,clientip,confirm_status,delete_status,membership_type)
VALUES (@eventid,@Email,@Organization,@Prefix,@Suffix,'',@Title,'','',@Fname,@Lname,'','',
@Address1,@Address2,@City,@State,@PostCode,@Country,'',@Telephone, @Cell,'','','',@MainUserId,@Password,Getdate()
,NULL,@ClientIp,'Approved','No','Individual')

declare @NewMemberUniId nvarchar(100);
set @NewMemberUniId=scope_identity();
-- Insertion for tbl_speaker_moderator_regis

INSERT INTO dbo.tbl_speaker_moderator_regis(uniid, eventid, prefix, fname, lname, suffix, nick_name,organization,title, department, address1, address2,city,state, postcode,
 country, telephone,cell, email, session_time, fee, speaking_date, speaker_yesno,reception_attend_yesno, permission_recorded_yesno,
  entry_date, clientip,Track,USCitizen,ContactEmail,GoingFaa,MemberUniId,Meeting)
            VALUES   ( @UniId, @eventid, @Prefix, @Fname, @Lname, @Suffix,@Nick_Name,@Organization, @Title, @Department, @Address1,@Address2, @City,@State, @PostCode,
@Country, @Telephone, @Cell, @Email, @Session_Time, @Fee, @Speaking_Date,@Speaker_YesNo,@Reception_Attend_YesNo, @Permission_Recorded_YesNo,
Getdate(), @ClientIp,@Track,@USCitizen,@ContactEmail,@GoingFaa,@NewMemberUniId,@Meeting)

-- Insertion for internationalvisitorregistration
insert into dbo.tblInternational_Visitor(
SpeakerUniId, PageType, FName, MName, LName, Address1, Address2, City, State, PostCode, Country, Gender,
DateOfBirth, BirthCity, BirthCountry, CurrentCitizenship, DualCitizenship, PassportNum, PassportIssueBy, PassportExpirationDate,
 VisaType, VisaExpirationDate, PermanentResident, Stc, Ctc, Ccc, JobTitle, EmployeeName, EmpAddress1, EmpAddress2,
  EmpCity, EmpState, EmpPostCode, EmpCountry, CreById, CreByIp)
values(@UniId,  @IntPageType, @IntFName, @IntMName, @IntLName, @IntAddress1, @IntAddress2, @IntCity, @IntState, @IntPostCode, @IntCountry, @IntGender,
@IntDateOfBirth, @IntBirthCity, @IntBirthCountry, @IntCurrentCitizenship, @IntDualCitizenship, @IntPassportNum, @IntPassportIssueBy, @IntPassportExpirationDate,
@IntVisaType, @IntVisaExpirationDate, @IntPermanentResident, @IntStc, @IntCtc, @IntCcc, @IntJobTitle, @IntEmployeeName, @IntEmpAddress1, @IntEmpAddress2,
 @IntEmpCity, @IntEmpState, @IntEmpPostCode, @IntEmpCountry, @NewMemberUniId, @IntCreByIp )

  IF( @Fee IS NOT NULL OR @Fee <> '' )
            BEGIN
                INSERT dbo.tblmemberorders
(memberuniid, membersubscriptionuniid, orderid, prodstoreorderid, producttype, productdesc, totalamount, paymentstatus, orderdate,
billcontactname, billadr1, billadr2, billcity, billstate, billpostcode, billcountry, clientip, entry_date)
VALUES (@NewMemberUniId, Null, @OrderID, NULL, 'ATCA-Tech Symposium 2015-SPEAKER-MODERATOR REGISTRATION' , 'Non Member', @Fee, 'Not Paid', Getdate(),
@Fname + ' ' + @Lname, @Address1, @Address2, @City, @State, @PostCode, @Country, @ClientIp, Getdate() )
            END

set @Result=1;
set @UserIdOut=@MainUserId;
set @PasswordOut=@Password;
End

          COMMIT TRANSACTION
      END try

      BEGIN catch
          ROLLBACK TRANSACTION
 --select ERROR_NUMBER() as 'ERROR_NUMBER',
    --       ERROR_SEVERITY() as 'ERROR_SEVERITY',
    --       ERROR_STATE() as 'ERROR_STATE',
    --       LEFT(ERROR_PROCEDURE(),50) as 'ERROR_PROCEDURE',
    --       ERROR_LINE() as 'ERROR_LINE' ,
    --       LEFT(ERROR_MESSAGE(),40) as 'ERROR_MESSAGE'
      END catch

  END

//C# code user

string[] Result = new string[3];
com.Parameters.Add("@Result", SqlDbType.Int).Value = 0;
                com.Parameters.Add("@UserIdOut", SqlDbType.NVarChar, 200).Value=string.Empty;
                com.Parameters.Add("@PasswordOut", SqlDbType.NVarChar, 200).Value =string.Empty;
                com.Parameters["@Result"].Direction = ParameterDirection.Output;
                com.Parameters["@UserIdOut"].Direction = ParameterDirection.Output;
                com.Parameters["@PasswordOut"].Direction = ParameterDirection.Output;


                Conn.Open();
                com.ExecuteNonQuery();
                Result[0] = Convert.ToString(com.Parameters["@Result"].Value);
                Result[1] = Convert.ToString(com.Parameters["@UserIdOut"].Value);
                Result[2] = Convert.ToString(com.Parameters["@PasswordOut"].Value);

No comments:

Post a Comment