Click here to Skip to main content
16,021,293 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, i need to generate one id column which increments automatically and i want concatenate another column with this column

i written the following trigger but it always returning the same value for every insertion in the table

ex.Document_Id = MGA0001 for every insertion


SQL
ALTER TRIGGER Document_Management_Trigger ON Document_Management AFTER INSERT
AS
BEGIN
DECLARE @ID INT = 0
DECLARE @Document_Type INT = 0
DECLARE @Doc_Type NVarchar(2) = NULL
DECLARE @Autogenerate_Id NVarchar(10) = NULL
DECLARE @New_Autogenerate_Id NVarchar(10) = NULL
DECLARE @Character CHAR(1) = NULL
DECLARE @Number INT = 0


SELECT @ID = I.ID FROM INSERTED I;

SELECT @Document_Type = I.Document_Type FROM INSERTED I;
SELECT @Doc_Type = I.Doc_Type FROM INSERTED I;
DECLARE @Document_Mana INT = 0

SET @Autogenerate_Id = (SELECT TOP 1 Autogenerate_ID FROM Document_Management DM WHERE ID = (SELECT MAX(ID) FROM Document_Management WHERE Document_Type = @Document_Type))

IF @Autogenerate_Id IS NULL
BEGIN
    SET @Autogenerate_Id = 'A0000'
END

SET @Character = (SELECT SUBSTRING (@Autogenerate_Id ,1, 1))

SET @Number = (SELECT SUBSTRING (@Autogenerate_Id ,2, 4))

SET @Number = @Number + 1

IF @Number > 9999
BEGIN
   SET @Number = 1

   SET @Character = CHAR(ASCII(@Character)+1)
END

SET @Autogenerate_Id = @Character + RIGHT(CONVERT(VARCHAR(4),@Number),4)


UPDATE Document_Management SET Autogenerate_Id = @Autogenerate_Id, Document_Id = @Doc_Type + @Autogenerate_Id WHERE ID = @ID

END
Posted
Updated 11-Sep-12 1:53am
v2

1 solution

Hi,

I think the problem is here

SQL
SET @Autogenerate_Id = (SELECT TOP 1 Autogenerate_ID FROM Document_Management DM WHERE ID = (SELECT MAX(ID) FROM Document_Management WHERE Document_Type = @Document_Type))

IF @Autogenerate_Id IS NULL
BEGIN
    SET @Autogenerate_Id = 'A0000'
END


The variable @Autogenerate_Id is always coming as NULL for that particular @Document_Type. check the value of Autogenerate_ID value in table Document_Management, and try check for non null values of this column. Then it may work.

Hope it helps.
 
Share this answer
 
Comments
sk. maqdoom ali 11-Sep-12 8:19am    
yes, u r correct , but when i am executing this seperately i am getting the value.
for ex:A0001

(SELECT TOP 1 Autogenerate_ID FROM Document_Management DM WHERE ID = (SELECT MAX(ID) FROM Document_Management WHERE Document_Type = @Document_Type))
Karthik Harve 11-Sep-12 8:29am    
what is the value you are getting after excuting this query

(SELECT TOP 1 Autogenerate_ID FROM Document_Management DM WHERE ID = (SELECT MAX(ID) FROM Document_Management WHERE Document_Type = @Document_Type))
sk. maqdoom ali 11-Sep-12 8:37am    
A0001
Karthik Harve 11-Sep-12 8:43am    
its is a trigger, so, as a record is inserted ID got generated but Autogenerate_ID is yet to be generated, so it is coming as NULL. that is why you are getting this always NULL. try some different logic using the ID value, since it is not NULL.
sk. maqdoom ali 11-Sep-12 9:12am    
here Autogenerate_Id is one of my column name thats why its giving output when executing separately
(SELECT TOP 1 Autogenerate_ID FROM Document_Management DM WHERE ID = (SELECT MAX(ID) FROM Document_Management WHERE Document_Type = @Document_Type))
output:A0001

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900