select或变量上的SQL Replace()函数

时间:2011-04-14 21:10:23

标签: sql sql-server select replace

insert into PendingEmails (Subject,Body,ToEmail,PendingEmailStatusID)
select REPLACE( select subject from dbo.EmailTemplates where ID  = 7 ), '[CallID]'), (select Body from dbo.EmailTemplates where ID  = 7) ,contact.Email  ,5 
FROM  inserted AS i inner join Calls on i.CallID = Calls.CallId inner join Contact on calls.ContactID = Contact.ContactID
where contact.Email is not null and  contact.Email <> ''

我想用一个值替换主题中的'[CallID]'。该语法不起作用。 我也试过使用变量,也不会工作。

最好的方法是什么?

3 个答案:

答案 0 :(得分:3)

insert into PendingEmails (Subject)
select REPLACE(subject, '[CallID]', @callid)
from dbo.EmailTemplates
where ID  = 7

我无法看到你在哪里加入调用表来获取calls.callid,你是否已将它存储到变量中?

如果你有一个可以产生它的子查询(括号标量子查询),你可以跳过该变量

insert into PendingEmails (Subject)
select REPLACE(subject, '[CallID]', (select callid from calls where id=9))
from dbo.EmailTemplates
where ID = 7

或加入

insert into PendingEmails (Subject)
select REPLACE(e.subject, '[CallID]', c.callid)
from dbo.EmailTemplates e
join calls c on c.id=9
where e.ID = 7

答案 1 :(得分:1)

你有一些括号恶作剧:

insert into PendingEmails (Subject) 
select REPLACE(( select subject from dbo.EmailTemplates where ID = 7 ), '[CallID]' , calls.callid)

答案 2 :(得分:1)

INSERT INTO PendingEmails (Subject) 
SELECT REPLACE(subject, '[CallID]' , calls.callid)
    FROM dbo.EmailTemplates 
 WHERE ID = 7