如何使用SQL 2012创建存储过程?

时间:2014-02-24 11:51:03

标签: c# sql sql-server stored-procedures

我有一张桌子“雇主”

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 

我创建了如何使用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

我仍然需要创建Search,GetByID,GetByPage,GetList ..请存储过程.. :) 有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

你的问题不是很清楚 插入记录的正确程序代码:

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

如果EmployerId是自动增量,或者您需要将EmployerId作为存储过程的参数传递,则还需要使用IDENTITY规范指定列EmployerId。

dbo.Employers_Insert或Search,GetByID,GetByPage,GetList的需求代码有问题吗?

答案 1 :(得分:1)

根据您的业务要求,您可以将搜索过程编写为:

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