2016-03-04 59 views
-1

在屬性文件中使用下面的查詢並在shell腳本中使用,但由於查詢中的特殊字符,它沒有給我輸出特殊字符。SQL查詢的轉義字符

query="select top 10 source_system,updt_etl_instnc_run_id,negative_posting_flag, to_number(to_varchar(to_date(create_tmstmp),'yyyymm')) as part_date from c_fin_a.gl_transaction_data where to_number(to_varchar(to_date(create_tmstmp),'yyyymm'))=$NOW and \$CONDITIONS" 

我已經使用所有特殊字符的轉義字符,那麼它也不會給我同樣的輸出與轉義字符。

query= \ " select top 10 source_system,updt_etl_instnc_run_id,negative_posting_flag, to_number \ (to_varchar \ (to_date \ (create_tmstmp \) , \ ' yyyymm \ ' \) \) as part_date from c_fin_a.gl_transaction_data where to_number \ (to_varchar \ (to_date \ (create_tmstmp \) , \ ' yyyymm \ ' \) \)= \ $NOW and \ \$CONDITIONS \ " 
+0

我不明白的問題,這是不正確的,如果它是一個SQL查詢,那麼你應該有一個SQL標籤沒有殼標籤... – Seekheart

回答

0

首先:當您爲變量賦值時,絕對不要在等號周圍使用空格。我的意思是var=value是好的,var = value不好。

現在,讓我們假設你的shell變量具有以下值:

NOW=201603 
CONDITIONS="city='New York'" 

然後,你需要使用以下命令:

query="select top 10 source_system,updt_etl_instnc_run_id,negative_posting_flag, to_number(to_varchar(to_date(create_tmstmp),'yyyymm')) as part_date from c_fin_a.gl_transaction_data where to_number(to_varchar(to_date(create_tmstmp),'yyyymm'))=${NOW} and ${CONDITIONS}" 

生成有效的SQL語句。

+0

感謝Mauro的回覆..你可以請建議轉義字符爲上面的查詢? – marjun

+0

逃避什麼?正如我所說的上面這個變量名爲「query」的內容是一個有效的sql語句...... – mauro