简体   繁体   中英

Stored Procedure

Can You help me out. I have this string that inserts into a table called tblApplication

 insert into tblApplication (ApplicationID,ControlNo,Name)
 values 
 (@AppID,Name);

I want that if I insert into tblApplication, a stored procedure will generate a control number for the inserted application.

the format of the control number is like 18-0001 year and the number generated according to number of application for the certain year

select Concat( ( YEAR( GETDATE() ) % 100 ),'-', ( select count(AppicationID) + 
1 from tblApplication where year(TransactionDate) = year(getDate()) + 1 ) )

My Stored Procedure is like this and based on how i understand stored procedures in google but im not sure about this

CREATE PROCEDURE GenerateControlNumberForInsertedApplication
    @ApplicationID Varchar(MAX)
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE tblApplication set ControlNo = @ApplicationID
END

As per my thinking your application table is look like

Table : tblApplication Colmuns : ApplicationID,ControlNo,Name,TransactionDate

So when you insert you only insert ApplicationId , Name. ( I hope that Application Id is Unique)

  1. Your insert statement should look like this

      INSERT INTO tblApplication (ApplicationId , Name) Values (@AppID,@Name) 
  2. Create Trigger

      CREATE TRIGGER trgAfterInsert on tblApplication FOR INSERT BEGIN DECLARE @ControlNumber NVARCHAR(50) DECLARE @AppId INT select @ControlNumber = Concat( ( YEAR( GETDATE() ) % 100 ),'-', ( select count(AppicationID) + 1 from tblApplication where year(TransactionDate) = year(getDate()) + 1 ) ) Select @AppId = ApplicationId from inserted Update tblApplication SET ControlNumber = @ControlNumber where ApplicationId = @AppId 

    END

I just want to mention another approach. Since you're counting the number of ApplicationID then adding 1 to the results. I have sevral approaches that could be useful for your scenario.

  1. Generate Control Number using Scalar Function
  2. Generate Control Number using Sequence
  3. Use Computed Column in tblApplication
  4. Use IDENTITY column in tblApplication
  5. Use A view with generated control number.

For more explanation, If you use a Scalar Function to generate a control number, then you'll have the advantage of using the generic function else where with more flexibility than a store procedure. This approach can be used either with the same way of counting the rows and adding 1 to the result, or you can add an identity column to make things automated and controlled by SQL Server. Another approach is to add a computed column using the scalar function + the identity column, this will give you a column holds the control number without the need of converting and doing extra steps.

So, the scalar function is a function similar to YEAR(), COUNT().etc functions. You just set it up, and it will give you the output where ever you use it. (kinda like methods in java C# and functions in Javascript but with SQL limitations).

To create a scalar function you could do this :

CREATE FUNCTION FN_ControlNumber()
RETURNS VARCHAR(100)
AS
BEGIN

    DECLARE
        @Result         VARCHAR(100)
    ,   @ApplicationId  INT 

SET @ApplicationId = (SELECT COUNT(ApplicationId) + 1 FROM tblApplication WHERE YEAR(TransactionDate) =  YEAR(GETDATE()) + 1)

SET 
    @Result = CAST(YEAR(GETDATE()) % 100  AS VARCHAR(2) ) + '-' + CAST(@ApplicationId + 1 AS VARCHAR(97))

    RETURN @Result

END

then call it :

SELECT dbo.FN_ControlNumber()

The results will be the new control number, so you can call it from your C#(use executeScalar), a store procedure, trigger, view or anywhere you want to generate a new control number.

If you want another approach, you could use a view like this :

CREATE VIEW tblApplication_ControlNumber
AS
SELECT 
    CAST(YEAR(GETDATE()) % 100  AS VARCHAR(2) ) + '-' + CAST(ControlNumber + 1 AS VARCHAR(97)) AS ControlNumber
FROM (
SELECT 
    COUNT(ApplicationId) + 1 AS ControlNumber 
FROM 
        tblApplication 
WHERE 
    YEAR(TransactionDate) =  YEAR(GETDATE()) + 1 
) D 

similar to the function output, but you need to call it like this :

SELECT ControlNumber FROM tblApplication_ControlNumber

If you need to use a sequential numbering globally (with other tables) or even with one table doesn't matter much here. The idea is that a sequence is similar to IDENTITY but it's not tight to a table. So, you can use it on one table or more. if you have the privileges to create one, then I suggest using it if is it going to be more effective than an IDENTITY to you.

My suggestion is to add an IDENTITY column to make the sequential controlled by DBMS and make it PRIMARY KEY.Then, use scalar function to generate the control number with the IDENTITY column as input and use it in a computed column. This will be auto calculated for each insert. So, no store procedure, trigger needed. If somehow you changed the control number format, then you can just change the format from the function, and all generated control number inside the computed column will be updated automatically.

If you are going to go with my suggetion, your function would be something like this :

CREATE FUNCTION FN_GenerateSequence
(
    @ID     INT
)
RETURNS VARCHAR(100)
AS
BEGIN

    DECLARE
        @Result         VARCHAR(100)


SET 
    @Result = CAST(YEAR(GETDATE()) % 100  AS VARCHAR(2) ) + '-' + CAST(@ApplicationId AS VARCHAR(97))


    RETURN @Result

END

Then, you can call it with any column with type INT like this :

SELECT dbo.FN_GenerateSequence(ID)

If you want to use it with computed column, just use this :

dbo.FN_GenerateSequence(ComputedColumnName)

This is some of the approaches that will be useful, I just mentioned them to give you another point of view, and might be helpful.

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