Solved

SQL Insert Trigger

Posted on 2010-11-17
1
257 Views
Last Modified: 2012-05-10
I have 3 tables - TableA, TableB, and TableC.  Table A is populated with data.  When an insert is performed on TableB I want to get appropriate data from TableA.  Then modify that data and insert the modified data into TableC.  

The data in Table A is as such:  Provider {0} has been modified by {1}.
It should be inserted into TableC as:  Provider xxxx has been modified by yyyy -- per the code in the trigger.

This is not working.  Instead Provider {0} has been modified by {1}. is being inserted into TableC.  What is wrong with my Trigger.

** Code is not inserting - Here it is below **

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER TestBInsert
   ON  _TestB
   AFTER INSERT
AS
BEGIN
      declare @uName varchar(50);
      declare @Message varchar(50);
      declare @TestB varchar(50);

      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

        -- Insert statements for trigger here
      SELECT @uName=SYSTEM_USER;
      print 'UserName: ' + @uName;

      SELECT @Message = (SELECT [TestAData] FROM _TestA Where TestApkid = 1);
      print 'Message: ' + @message;

      SELECT @TestB = (SELECT [TestBData] FROM Inserted);      
      print 'TestB Data: ' + @TestB;

      SELECT REPLACE(@Message,'{0}',@TestB);
      SELECT REPLACE(@Message,'{1}',@uName);
      print 'Insert Data: ' + @Message;

      Insert Into _TestC (TestCData) Values (@Message);

END
GO


Trigger.jpg
0
Comment
Question by:CipherIS
1 Comment
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 34156822
Change these two lines

      SELECT REPLACE(@Message,'{0}',@TestB);
      SELECT REPLACE(@Message,'{1}',@uName);

To
SET @Message = REPLACE(@Message,'{0}',@TestB);
SET @Message = REPLACE(@Message,'{1}',@uName);
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql query to Stored Procedure 6 38
Caste datetime 2 51
default constraint within a function 3 37
MS SQL Inner Join - Multiple Join Parameters 2 18
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now