简体   繁体   中英

How to create Stored Procedure using SQL 2012?

i have a table "employers"

Employerid int
InsuranceNumber varchar(10)
Ministry nvarchar(100)
Adress nvarchar(250)
PostalCode varchar(50)
Phone varchar(14)
Mobile varchar(14)
Email nvarchar(60)
UserName nvarchar(50)
Password nvarchar(50)
ContactPerson nvarchar(250)
EntryDate datetime
LastUpdateDate datetime
IsDeleted bit
Active bit 

and i have created how to insert data using insert stored proc

Create procedure dbo.Employers_Insert
@InsuranceNumber nvarchar(10),
@Ministry nvarchar(100),
@Adress nvarchar(250),
@PostalCode varchar(50),
@Phone varchar(14),
@Mobile varchar(14),
@Email nvarchar(60),
@UserName nvarchar(50),
@Password nvarchar(50),
@ContactPerson nvarchar(250),
@EntryDate datetime,
@LastUpdateDate datetime,
@IsDeleted bit,
@Active bit
-->AS
-->BEGAIN
Insert Employers (InsuranceNumber, Ministry, Adress , PostalCode ,
Phone, Mobile , Email, UserName , [Password] , ContactPerson, EntryDate,
LastUpdateDate, IsDeleted, Active)
values (@InsuranceNumber, @Ministry, @Adress , @PostalCode ,
@Phone, @Mobile , @Email, @UserName , @[Password] , @ContactPerson, EntryDate,
@LastUpdateDate, @IsDeleted, @Active)
End

i still need to create Search , GetByID , GetByPage , GetList .. Stored Procedures please.. :) any help ?

Your question is not very clear.
The correct procedure code to insert a record:

CREATE PROCEDURE dbo.Employers_Insert
(
    @InsuranceNumber nvarchar(10),
    @Ministry nvarchar(100),
    @Adress nvarchar(250),
    @PostalCode varchar(50),
    @Phone varchar(14),
    @Mobile varchar(14),
    @Email nvarchar(60),
    @UserName nvarchar(50),
    @Password nvarchar(50),
    @ContactPerson nvarchar(250),
    @EntryDate datetime,
    @LastUpdateDate datetime,
    @IsDeleted bit,
    @Active bit
)
AS
BEGIN

    INSERT INTO dbo.Employers 
    (
            InsuranceNumber, Ministry, Adress, PostalCode,
            Phone, Mobile, Email, UserName, [Password], ContactPerson, EntryDate,
            LastUpdateDate, IsDeleted, ACTIVE
    )
    VALUES 
    (
            @InsuranceNumber, @Ministry, @Adress, @PostalCode,
            @Phone, @Mobile, @Email, @UserName, @Password, @ContactPerson, @EntryDate,
            @LastUpdateDate, @IsDeleted, @Active
    )

END

Also you need specify column EmployerId with IDENTITY specification if EmployerId is autoincrement or you need pass EmployerId as parameter of stored procedure.

You have problem with dbo.Employers_Insert or your need code for Search, GetByID, GetByPage, GetList ?

Based on your business requirements you can write a Search procedure as:

CREATE PROCEDURE sp_EmployersSearch
    @Employerid int,-- Add here all the search parameters you'll send from application
    @InsuranceNumber nvarchar(10),
    @Ministry nvarchar(100),
    @Adress nvarchar(250),
    @PostalCode varchar(50),
    @Phone varchar(14),
    @Mobile varchar(14),
    @Email nvarchar(60),
    @UserName nvarchar(50),
    @Password nvarchar(50),
    @ContactPerson nvarchar(250),
    @EntryDate datetime,
    @LastUpdateDate datetime,
    @IsDeleted bit,
    @Active bit
AS
SELECT InsuranceNumber,-- Add here all the columns you want to  send back to application
       Ministry,
       Adress ,
       PostalCode ,
       Phone,
       Mobile ,
       Email,
       UserName ,
       [Password] ,
       ContactPerson,
       EntryDate,
       LastUpdateDate,
       IsDeleted,
       Active
FROM employers
WHERE     (@Employerid IS NULL OR Employerid = @Employerid)
      AND (@InsuranceNumber IS NULL OR InsuranceNumber LIKE '%' + @InsuranceNumber + '%')
      AND (@Ministry IS NULL OR Ministry LIKE '%' + @Ministry + '%')
      AND (@Adress IS NULL OR Adress LIKE '%' + @Adress + '%')
      AND (@PostalCode IS NULL OR PostalCode = @PostalCode)
      --You can add search conditions here based on your business requirements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM