Click here to Skip to main content
15,917,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai,

i have a table Queue_Number having columns ID , Queue_Type , Queue_Status , Reference_ID , QID , QNo , QID_No

i have written trigger for insertion in this QID , QNo , QID_No will be inserted automatically by the trigger which is shown below , i want to insert the remaining columns

Queue_Type , Reference_ID externally.

i am inserting Queue_Type , Reference_ID externally for that i am getting result as the two columns Queue_Type , Reference_ID are inserting in one row and the remaining
QID , QNo , QID_No inserting in another row.

SQL
CREATE TRIGGER QNO_GENERATION
ON Queue_Number
FOR INSERT
AS
BEGIN

DECLARE @QID_No NVarchar(25) = NULL
DECLARE @QId NVarchar(6) = NULL
DECLARE @QNo Int = 0
DECLARE @Month Int = 0
DECLARE @Queue_Type INT
DECLARE @Reference_ID INT


SELECT @QId=I.QID FROM INSERTED I
SELECT @QNo=I.QNo FROM INSERTED I
SELECT @QID_No=I.QID_No FROM INSERTED I
SELECT @Reference_ID=I.Reference_ID FROM INSERTED I


SET @Month = (SELECT DATEPART(MONTH,GETDATE()))

IF @Month > 3
BEGIN
    SET @QId = (SELECT (CONVERT(NVARCHAR(4),DATEPART(Year,GETDATE())) + CONVERT(NVarchar(2),RIGHT((DATEPART(Year,DATEADD(Year,1,GETDATE()))),2))))
END

SET @QNo = (SELECT QNo FROM Queue_Number WHERE QID_No = @QId)

SET @QNo = @QNo + 1

SET @QID_No = @QId + (SELECT (CONVERT(NVarchar(10),@QNo)))

INSERT INTO Queue_Number (QID,QNo,QID_No,Queue_Type,Reference_ID) VALUES (@QId,@QNo,@QID_No,@Queue_Type,@Reference_ID)

SELECT @Reference_ID AS QueueNo

END 
Posted
Updated 24-Jul-12 21:57pm
v2
Comments
graciax8 25-Jul-12 2:36am    
what is your question?
sk. maqdoom ali 25-Jul-12 4:17am    
i want to insert 5 fields into the table , in that the three fields QID,QNo,QID_No are automatically inserted when i insert another another two fields ie, Queue_Type, Reference_ID .
when i am inserting these two field values externally ie, insert into table(Queue_Type, Reference_ID)values(1,12)
after doing this the insertion the inserted values in the table as
for Queue_Type, Reference_ID the values are inserted in one row but the remaining fields QID,QNo,QID_No are inserted in another row.

i want to insert all the fields in a single row only
graciax8 25-Jul-12 5:12am    
With all the parameters and declarations you wrote here. I suggest you need to create a stored procedure not a trigger.
sk. maqdoom ali 25-Jul-12 5:14am    
i want after insertion of Queue_Type, Reference_ID fields the trigger must be fire and insert the computed date into QID,QNo,QID_No fields
graciax8 25-Jul-12 5:23am    
get the id of the insertion of Queue_Type, Reference_ID.
get the result and insert into QID,QNo,QID_No fields. For me you can easily do that using stored proc. Goodluck!

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