Solved

SQL Insert Trigger

Posted on 2010-11-17
1
258 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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