Solved

SQL Insert Trigger

Posted on 2010-11-17
1
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
database level memory cache..? 8 37
Present Absent from working date rage 11 49
denied execute as 13 33
SQL Query Across Multiple Tables - Help 5 25
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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