-1

I am trying to get company id like "Cp-00001". If data exists in table then the id should be "Cp-00001" + 1 = "Cp=00002" and do on...

Here's what I have so far:

CREATE PROCEDURE [dbo].[sp_AutoGenerateCustomerCode] 
 AS
DECLARE @id VARCHAR(10)
BEGIN
    SELECT @id = 'Cp-' + CAST(MAX(CAST(SUBSTRING(CompanyCode,4,5) AS INTEGER))+1 AS VARCHAR) FROM [Beauty Saloon Project].[dbo].[tbl_Company];
    IF @id IS NULL
    BEGIN
        SET @id = 'Cp-00001';
    END

    RETURN @id;
END

but when i call it here

  datatable DT = new datatable
  DT = ExecuteSpDataTable("sp_AutoGenerateCustomerCode");

This returns null.

If I don't have data then it should return Cp-00001, but I have one data row in which company code is saloon is it the reason for null ???

EDIT:

public DataTable ExecuteSpDataTable(string SPName)
    {
        try
        {
            if (ConnectionOpen())
            {
                SqlCommand objSqlCommand = new SqlCommand(SPName, objConnection);
                objSqlCommand.CommandType = CommandType.StoredProcedure;
                objSqlCommand.CommandTimeout = 10000;
                SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter();
                DataTable objDataTable = new DataTable();
                objSqlDataAdapter.SelectCommand = objSqlCommand;
                objSqlDataAdapter.Fill(objDataTable);
                ConnectionClose();
                return objDataTable;
            }
            return null;
        }
        catch (Exception ex)
        {
            objErrorLogs.LogError(ex);
            return null;
        }
    }
4

1 回答 1

4

One word of advice: DON'T DO THIS! Using this SELECT MAX() + 1 approach is not safe under load, as soon as more than one user will be using your application, you WILL HAVE DUPLICATES - sooner or later.

The only viable solution is to use

  • an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value
  • a computed, persisted column to convert that numeric value to the value you need

So try this:

CREATE TABLE dbo.tblCompany
  (ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
   CompanyID AS 'CP-' + RIGHT('00000' + CAST(ID AS VARCHAR(5)), 5) PERSISTED,
   .... your other columns here....
  )

Now, every time you insert a row into tblCompany without specifying values for ID or CompanyID:

INSERT INTO dbo.tblCompany(Col1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)

then SQL Server will automatically and safely increase your ID value, and CompanyID will contain values like CP-00001, CP-00002,...... and so on - automatically, safely, reliably, no duplicates.

Update: if you want to make the CompanyID the primary key, you could use this T-SQL statement:

CREATE TABLE dbo.tblCompany
  (ID INT IDENTITY(1,1) NOT NULL,
   CompanyID AS 'CP-' + RIGHT('00000' + CAST(ID AS VARCHAR(5)), 5) PERSISTED
      CONSTRAINT PK_tblCompany PRIMARY KEY NONCLUSTERED,
   .... your other columns here....
  )

CREATE CLUSTERED INDEX CIX_Company ON dbo.tblCompany(ID);

I would leave the clustered index on ID and just move the primary key constraint to use CompanyID instead.

于 2013-08-18T14:37:18.233 回答