Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
x5-452
Starting Member
8 Posts |
Posted - 2014-08-08 : 09:48:30
|
See the below tables. Table1 holds a letter template information.Table2 stores the Customer information and tag name and values to stuff into the template. Table3 will hold the column (EmailBody column) to store the template with the tag replacement. Table1 - Template MasterTemplate Code/Template Text (text)TEMPLATE01 / Dear <Name>, <Account> is in Arrears of <Outstanding>Table2:Customer / Template Code / Tag Name (char(50) / Tag Value (char(100))ABC01 / TEMPLATE01 / <Name> / John DoeABC01 / TEMPLATE01 / <Account> / 1001ABC01 / TEMPLATE01 / <Outstanding> / $500.00DEF01 / TEMPLATE01 / <Name> / Jane DoeDEF01 / TEMPLATE01 / <Account> / 1002DEF01 / TEMPLATE01 / <Outstanding> / $1000.00Table3:Customer (char(15) / Other Info / EmailBody (text)ABC01 / "Whatever" / DEF01 / "Extra Info" / I've only been able to manage to replace 1 tag and can't seem to replace all tags at once. How would I go about doing this successfully? |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-08-08 : 10:35:05
|
Please post:1. CREATE TABLE statements for the tables involved2. INSERT INTO statements to populate the tables with test data3. Your query so far, along with the results it produces4. The desired results |
|
|
x5-452
Starting Member
8 Posts |
Posted - 2014-08-08 : 11:17:51
|
I managed to get it working but I really don't think it's efficient.Any pointers how to optimize my query?CREATE TABLE [dbo].[Table1]( [TEMPLATE] [char](15) NOT NULL, [TEMPLATETEXT] [text] NULL)CREATE TABLE [dbo].[Table2]( [CUSTOMER] [char](15) NOT NULL, [TEMPLATE] [char](15) NOT NULL, [TAG] [char](50) NOT NULL, [TAGVALUE] [char](100) NULL)CREATE TABLE [dbo].[Table3]( [CUSTOMER] [char](15) NULL, [OTHERINFO] [varchar](50) NOT NULL, [EMAILBODY] [text] NOT NULL)INSERT INTO [CFS].[dbo].[Table1] ([TEMPLATE] ,[TEMPLATETEXT]) VALUES ('TEMPLATE01' ,'Dear <Name>, <Account> is in Arrears of <Outstanding>')INSERT INTO [CFS].[dbo].[Table2] ([CUSTOMER] ,[TEMPLATE] ,[TAG] ,[TAGVALUE]) VALUES ('ABC01' ,'TEMPLATE01' ,'<Name>' ,'John Doe')INSERT INTO [CFS].[dbo].[Table2] ([CUSTOMER] ,[TEMPLATE] ,[TAG] ,[TAGVALUE]) VALUES ('ABC01' ,'TEMPLATE01' ,'<Account>' ,'1001') INSERT INTO [CFS].[dbo].[Table2] ([CUSTOMER] ,[TEMPLATE] ,[TAG] ,[TAGVALUE]) VALUES ('ABC01' ,'TEMPLATE01' ,'<Outstanding>' ,'$500.00') INSERT INTO [CFS].[dbo].[Table2] ([CUSTOMER] ,[TEMPLATE] ,[TAG] ,[TAGVALUE]) VALUES ('DEF01' ,'TEMPLATE01' ,'<Name>' ,'Jane Doe')INSERT INTO [CFS].[dbo].[Table2] ([CUSTOMER] ,[TEMPLATE] ,[TAG] ,[TAGVALUE]) VALUES ('DEF01' ,'TEMPLATE01' ,'<Account>' ,'1002') INSERT INTO [CFS].[dbo].[Table2] ([CUSTOMER] ,[TEMPLATE] ,[TAG] ,[TAGVALUE]) VALUES ('DEF01' ,'TEMPLATE01' ,'<Outstanding>' ,'$1000.00') INSERT INTO [CFS].[dbo].[Table3] ([CUSTOMER] ,[OTHERINFO] ,[EMAILBODY]) VALUES ('ABC01' ,'Whatever' ,'') INSERT INTO [CFS].[dbo].[Table3] ([CUSTOMER] ,[OTHERINFO] ,[EMAILBODY]) VALUES ('DEF01' ,'Other Info' ,'')Query so far:-- Update Table3 with Template bodyUPDATE Table3SET EMAILBODY = C.TEMPLATETEXTFROM Table3 AINNER JOIN TABLE2 B ON A.CUSTOMER = B.CUSTOMERINNER JOIN TABLE1 C ON B.TEMPLATE = C.TEMPLATE--Loop through until no more tags foundWHILE @@ROWCOUNT > 0UPDATE TABLE3SET EMAILBODY = cast(replace(cast(A.EMAILBODY as nvarchar(max)),LTRIM(RTRIM(B.TAG)),LTRIM(RTRIM(B.TAGVALUE))) as ntext)FROM TABLE3 AINNER JOIN TABLE2 B ON A.CUSTOMER = B.CUSTOMERINNER JOIN TABLE1 C ON B.TEMPLATE = C.TEMPLATEWHERE A.EMAILBODY LIKE '%' + LTRIM(RTRIM(B.TAG)) + '%'Results:CUSTOMER OTHERINFO EMAILBODYABC01 Whatever Dear John Doe, 1001 is in Arrears of $500.00DEF01 Other Info Dear Jane Doe, 1002 is in Arrears of $1000.00 |
|
|
|
|
|
|
|