0
フィールドcustomer_codeおよびcustomer_Nameを持つテーブルがあります。 顧客名を挿入するときにcustomercodeを生成して更新するトリガーを起動したいと考えました。顧客コードを生成するためのトリガー
CustomerCodeは顧客名の最初、3番目、最後の文字でなければならず、同じ名前の顧客が複数ある場合は のコードを1,2,3 ....などで追加する必要があります(例:RAHAN-RHN、 RAHAN-RHN1、RHN2 ...など)。 * Iが
誰かがどのようにスクリプトトリガをする私を助けてくださいすることができ、この部分に貼り付けています:
もう一つは、別の名前*(ROHAN-コードがRHN3でなければならないなど)がある場合、です。
おかげ
これは私が作成したものですが、私は上記のようにエラーが、2回目の試行部にあります。
CREATE TRIGGER [dbo].[alphaNumericCustomerReference]
ON [dbo].[CUSTOMER_MASTER]
AFTER INSERT
AS
declare @NewCode nvarchar(50)
declare @NewName nvarchar(50)
declare @checkCode nvarchar(50)
declare @ExtendedRefCode nvarchar(50)
declare @id int,@msg varchar(200)
declare @sameCustomerCount int
select @id= CUST_ID from inserted i;
select @NewCode= UPPER(SUBSTRING(CUST_NAME, 1, 1)+SUBSTRING(CUST_NAME, 3, 1)+RIGHT(CUST_NAME,1)) from inserted i;
select @checkCode=CUST_CODE from dbo.CUSTOMER_MASTER where [email protected]
select @NewName=CUST_NAME from inserted i;
select @sameCustomerCount=count(CUST_NAME)-2 from dbo.CUSTOMER_MASTER where [email protected]
BEGIN
SET NOCOUNT ON;
if @checkCode is null
BEGIN TRY
UPDATE CUSTOMER_MASTER
SET CUST_CODE= @NewCode
where [email protected]
END TRY
BEGIN CATCH
PRINT 'Error occured that is'
set @msg=(SELECT ERROR_MESSAGE())
print @msg;
END CATCH
else
BEGIN TRY
select @[email protected]+1;
select @[email protected]+convert(nvarchar(255),@sameCustomerCount)
UPDATE CUSTOMER_MASTER
SET CUST_CODE= @ExtendedRefCode
where [email protected]
END TRY
BEGIN CATCH
PRINT 'Error occured that is'
set @msg=(SELECT ERROR_MESSAGE())
print @msg;
END CATCH
あなたは助けを求める前に自分でトリガーを書こうとしました。 – Jacobian
このスレッドを見てください。 http://stackoverflow.com/questions/14252244/creating-a-trigger-generating-id-column-value-before-insert-when-new-tables-is-c – ogallagher
ありがとうございました。私は試しています.. – Amjath