The following is the stored procedure of SQL Server:
Create Procedure Pro_GenerateID
(
@tablename VARCHAR (50), - to generate the name of the delivery number
@ColumnName VARCHAR (50) ,- To generate the column name
@SignID VARCHAR (20) - Mark, if it is incremented by G-1, G-2, G-3 ..., if you pass 'g-', return G-4
)
AS
Declare @SignLength Int, - @ Signid length
@strscript varchar (800), - Generates SQL string of increment ID
@strintscript varchar (200) - SQL string of the current table maximum ID number
Set @SignLength = LEN (@SIGnID)
Select @ strintscript = 'castring (' @columnname
',' Cast ((@ signlength 1) as varchar (20)) ', (SELECT MAX (Len (' @Columnname ')) from' @tablename ') -' Cast (@SignLength As varchar (20)) ') as int)
Select @ strscript = 'SELECT TOP 1 (' '' @Signid '' Cast ((' @strintscript 1) as varchar (20))) AS ID from' @Tablename 'Where Left (' @Columnname ', ' Cast (@SignLength As Varchar (20)) ') = '' ' @Signid ' 'ORDER BY' @strintscript 'DESC'
SELECT @ strscript = '= = (selects (select * from' @tablename 'where' @Columnname 'Like' '' @Signid '%'))))
' @STRScript '
Else
SELECT '' ' (@Signid ' 1 ') ' 'AS ID'
Print @strscript
EXEC (@strscript)
If there is no data in the CCC table
Execute this stored procedure: exec pro_generateid 'ccc', 'c1', 'g-'
The display result is:
id
--------
G-1
(Return 1 line) You can write in the C # definition class:
Public class generateincreaseId {
Public Static String GetneWID (String Tablename, String Column, String Signid) {
SqlConnection Con = New SqlConnection ("...");
SQLCOMMAND COM = New Sqlcommand ("Pro_GenerateID", CON;
com.CommandType = commandtype.storedProcedure;
com.Parameters.Add (New Sqlparameter ("@ Tablename");
com.Parameters.Add (New Sqlparameter ("@ ColumnName");
com.Parameters.Add (New Sqlparameter ("@ignid", signid);
Try {
C.Open ();
Return com.executescalar (). TOSTRING ();
}
Catch (Exception EX) {
Throw (ex);
}
Finally {
IF (con.state == connectionState.open) {
C. close ();
}
CON = NULL;
COM = NULL;
}
}
}
Simply:
String s = generateIncreaseId.getnewid ("CCC", "C1", "G-");
This method can be used to generate a rule primary key, a table identifier, and the like.