为什么我的sql语句在我的编辑器中工作但不在我的JS函数中?

时间:2017-04-26 12:33:47

标签: javascript sql node.js salesforce

我的sql语句在我的sql编辑器中运行得非常好但是当我把它放在我的javascript函数中时它会告诉我客户端ID的无效列。

function getFeedposts(data) {
 var limit = data.dollarLimit;
 var client_id = data.salesforce_username;
 var sqlQuery = `select coalesce(advertiser,'') as advertiser, 
coalesce(partner,'') as partner, 
coalesce(advertiser,'') || ' via ' || coalesce(partner,'') as line1,
' - SSP: blah' as line2,
' - Lead Date: ' || date as line3,
'Yesterday''s Activity' as line4,
 ' - Impressions: ' || coalesce(impressions,0) as line5,
 ' - CPM: $' || coalesce(round(ecpm,2),0) as line6,
 ' - Spend: $' || coalesce(round(revenue,2),0) as line7,
 'Yesterday''s Spend Breakout' as line7a,
 coalesce(device_type,'') as line8,
'Running Spend Totals' as line9,
 ' - 3 Day Spend: $' || coalesce(round(three,2),0) as line10,
 ' - 7 Day Spend: $' || coalesce(round(seven,2),0) as line11,
 ' - 30 Day Spend: $' || coalesce(round(thirty,2),0) as line12,
 coalesce(round(thirty,2),0) as line13,
 coalesce(round(seven,2),0) as line14,
 coalesce(round(three,2),0) as line15,
 coalesce(round(three,2),0) as line15,
 client_id as client_id
 from
 (select advertiser, partner, date, impressions, ecpm, revenue,       device_type, client_id
 ,(select sum(m.revenue)
 FROM blahblah as m
 WHERE m.date > rl.date -30
 and advertiser = rl.advertiser
 and partner = rl.partner
GROUP BY partner, advertiser) as thirty
,(select sum(m.revenue)
FROM blahblah as m
WHERE m.date > rl.date -7
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as seven
,(select sum(revenue)
FROM blahblah as m
WHERE m.date > rl.date -3
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as three
from blahblah as rl
) as idunno
WHERE 
date = to_date('${data.date}','mm-dd-yyyy')
and revenue > 1 and client_id ='myemail@email.com'
`;
queryRDS(sqlQuery, data);
}

这可行,但当我做

 function getFeedposts(data) {
 var limit = data.dollarLimit;
 var client_id = data.salesforce_username;
 var sqlQuery = `select coalesce(advertiser,'') as advertiser, 
coalesce(partner,'') as partner, 
coalesce(advertiser,'') || ' via ' || coalesce(partner,'') as line1,
' - SSP: blah' as line2,
' - Lead Date: ' || date as line3,
'Yesterday''s Activity' as line4,
 ' - Impressions: ' || coalesce(impressions,0) as line5,
 ' - CPM: $' || coalesce(round(ecpm,2),0) as line6,
 ' - Spend: $' || coalesce(round(revenue,2),0) as line7,
 'Yesterday''s Spend Breakout' as line7a,
 coalesce(device_type,'') as line8,
'Running Spend Totals' as line9,
 ' - 3 Day Spend: $' || coalesce(round(three,2),0) as line10,
 ' - 7 Day Spend: $' || coalesce(round(seven,2),0) as line11,
 ' - 30 Day Spend: $' || coalesce(round(thirty,2),0) as line12,
 coalesce(round(thirty,2),0) as line13,
 coalesce(round(seven,2),0) as line14,
 coalesce(round(three,2),0) as line15,
 coalesce(round(three,2),0) as line15,
 client_id as client_id
 from
 (select advertiser, partner, date, impressions, ecpm, revenue,       device_type, client_id
 ,(select sum(m.revenue)
 FROM blahblah as m
 WHERE m.date > rl.date -30
 and advertiser = rl.advertiser
 and partner = rl.partner
GROUP BY partner, advertiser) as thirty
,(select sum(m.revenue)
FROM blahblah as m
WHERE m.date > rl.date -7
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as seven
,(select sum(revenue)
FROM blahblah as m
WHERE m.date > rl.date -3
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as three
from blahblah as rl
) as idunno
WHERE 
date = to_date('${data.date}','mm-dd-yyyy')
and revenue >
 `;
  sqlQuery += limit + 'and client_id =' + client_id;

  queryRDS(sqlQuery, data);
   }

我想知道为什么它告诉无效列的电子邮件地址是当前的client_id,当这样做时,但是当我硬编码它完全正常时,任何帮助都将非常感激

这是我收到的错误消息

RDS query successful
finished writing to rds
{ [error: column "joseph" does not exist]
 name: 'error',
 length: 101,
  severity: 'ERROR',
 code: '42703',
 detail: undefined,
 hint: undefined,
 position: '1596',
 internalPosition: undefined,
 internalQuery: undefined,
 where: undefined,
 schema: undefined,
 table: undefined,
 column: undefined,
 dataType: undefined,
 constraint: undefined,
 file: 'parse_relation.c',
 line: '3090',
 routine: 'errorMissingColumn' }

2 个答案:

答案 0 :(得分:2)

您只需要将行更改为:

sqlQuery += limit + " and client_id = '" + client_id + "'";

因为client_id变量不在引号中,所以SQL语句将其视为列名,因此您会收到无效的列ID错误。

答案 1 :(得分:1)

在工作示例中,您使用单引号包围电子邮件值。在后者中,电子邮件值没有引号。

改变这个:

sqlQuery += limit + 'and client_id =' + client_id

对此:

sqlQuery += limit + "and client_id ='" + client_id +"'"
// Notice the single quotes --------^----------------^
// enclosed in double quotes