2019年3月6日 星期三

[SQL Server]import large sql file

在執行少量的sql寫入時,可以直接貼到Microsoft SQL Server Management Studio執行
但若是檔案太大時,可以使用sqlcmd的方式來執行
指令如下:

sqlcmd -S 資料庫IP -U 使用者名稱 -P 使用者密碼 -d 資料庫名稱 -i .sql檔案路徑

執行時會跳出錯誤,需開啟QUOTED_IDENTIFIER:
訊息 1934, 層級 16, 狀態 1, 伺服器 User, 行 4
INSERT 失敗,因為下列 SET 選項的設定錯誤: 'QUOTED_IDENTIFIER'。請確認與 索引檢視表及/或計算資料行上的索引及/或篩選的索引及/或查詢通知及/或 XML 資料類型方法及/或空間索引作業 一起使用的 SET 選項是否正確。


example:
C:\>sqlcmd -I -S localhost -U test -P test123 -d dbo -i C:\sql.sql 


另外,若資料當中有中文,sqlcmd會直接結束
sqlcmd執行必須加入 -f 65001 代表對應中文編碼
SQL當中的中文欄位前方也要加 N
  
最後sqlcmd執行的參數會變成這樣子
若要將執行的結果匯出成txt檔,可以加上-o C:\sql.log example:
C:\>sqlcmd -I -S localhost -U test -P test123 -d dbo -f 65001 -i C:\sql.sql -o C:\sql.log





Ref:
http://blog.aihuadesign.com/2014/01/15/solve-sql-file-too-large-to-import-to-sql-server/
https://docs.microsoft.com/zh-tw/sql/tools/sqlcmd-utility?view=sql-server-2017
http://www.anujchaudhary.com/2011/10/sqlcmd-quotedidentifier-is-off.html
https://blog.csdn.net/roy_88/article/details/52595854


sqlcmd  
   -a packet_size 
   -A (dedicated administrator connection) 
   -b (terminate batch job if there is an error) 
   -c batch_terminator 
   -C (trust the server certificate) 
   -d db_name 
   -e (echo input) 
   -E (use trusted connection) 
   -f codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]
   -g (enable column encryption)
   -G (use Azure Active Directory for authentication)
   -h rows_per_header 
   -H workstation_name 
   -i input_file 
   -I (enable quoted identifiers) 
   -j (Print raw error messages)
   -k[1 | 2] (remove or replace control characters) 
   -K application_intent 
   -l login_timeout 
   -L[c] (list servers, optional clean output) 
   -m error_level 
   -M multisubnet_failover 
   -N (encrypt connection) 
   -o output_file 
   -p[1] (print statistics, optional colon format) 
   -P password 
   -q "cmdline query" 
   -Q "cmdline query" (and exit) 
   -r[0 | 1] (msgs to stderr) 
   -R (use client regional settings) 
   -s col_separator 
   -S [protocol:]server[instance_name][,port] 
   -t query_timeout 
   -u (unicode output file) 
   -U login_id 
   -v var = "value" 
   -V error_severity_level 
   -w column_width 
   -W (remove trailing spaces) 
   -x (disable variable substitution) 
   -X[1] (disable commands, startup script, environment variables, optional exit) 
   -y variable_length_type_display_width 
   -Y fixed_length_type_display_width 
   -z new_password  
   -Z new_password (and exit) 
   -? (usage)
 
 sqlcmd            [-U 登入識別碼]          [-P 密碼]
  [-S 伺服器]            [-H 主機名稱]          [-E 信任連線]
  [-N 加密連線][-C 信任伺服器憑證]
  [-d 使用資料庫名稱] [-l 登入逾時]     [-t 查詢逾時]
  [-h 標頭]           [-s 資料行分隔符號]           [-w 螢幕寬度]
  [-a 封包大小]        [-e 回應輸入]        [-I 啟用引號識別項]
  [-c cmdend]            [-L[c] 伺服器清單[清除輸出]]
  [-q "命令行查詢"]   [-Q "命令行查詢" 並結束]
  [-m 錯誤等級]        [-V 嚴重性等級]     [-W 移除句尾空格]
  [-u unicode 輸出]    [-r[0|1] 訊息傳至 stderr]
  [-i 輸入檔]         [-o 輸出檔]        [-z 新密碼]
  [-f <字碼頁> | i:<字碼頁>[,o:<字碼頁>]] [-Z 新密碼並結束]
  [-k[1|2]移除 [replace] 控制項字元]
  [-y 可變長度類型顯示寬度]
  [-Y 固定長度類型顯示寬度]
  [-p[1] 列印統計資料[冒號格式]]
  [-R 使用用戶端地區設定]
  [-K 應用程式意圖]
  [-M 多重子網路容錯移轉]
  [-b 批次中止錯誤時發生]
  [-v var = "值"...]  [-A 專屬的管理連線]
  [-X[1] 停用命令、啟動指令碼、環境變數 [並結束]]
  [-x 停用變數替代]
  [-j 列印原始錯誤訊息]
  [-g 啟用資料行加密]
  [-G 為驗證使用 Azure Active Directory]
  [-? 顯示語法摘要]

2019年3月5日 星期二

[SQL Server]帳號登入錯誤

在SQL Server新建登入帳號後,登入會跳出錯誤
是因為少設定SQL Server和Windows混和驗證

先以Windows方式登入後,點databse右鍵 -> 屬性



伺服器驗證選擇 "SQL Server和Windows混和驗證"
在重啟SQL Server,就可以使用帳號密碼登入

 


2019年3月4日 星期一

[SQL SERVER] 使用mdf檔還原

手上拿到mdf檔要還原時,要使用附加的方式

1.將mdf檔複製到SQL Server資料庫存放路徑底下

資料庫檔案都是放在 C:\Program Files\Microsoft SQL Server\{version}\MSSQL\Data 目錄
 {version}要看目前您使用資料庫的版本


2.使用附加的方式,加入複製的檔案



Ref: https://ithelp.ithome.com.tw/questions/10046158

2019年1月9日 星期三

Build Classic Asp Environment in Winsdows10

因為專案有些是舊的asp程式
找了一些資料怎麼建置asp的環境
後來照這篇試出來可以跑,參考

---------------------------------------------------------------------
過了三個月後再回來看這篇,一開始設不成功
後來照著Ref的連結去設定,不確定是不是最後這步
設定完成後,應該classic asp程式就可以開啟

1.IIS -> ASP


2.將這兩個選項設為True
啟用上層路徑            -> True
將錯誤傳送到瀏覽器 -> True




Ref:
https://www.codeproject.com/Articles/43132/How-to-Setup-IIS-on-Windows-to-Allow-Classic

2019年1月2日 星期三

無腦Hard Code程式

跨年完第一天上班,還有放假症候群的時候
有user反應某個舊功能下拉選單年份要更新
看了一下,程式的年份是用寫死的 =="
(i < 6的地方,6是寫死的,不是用算的)












有可能當初需求是取最近的六年資料
改版後就可以用算的顯示年份了








2018年12月26日 星期三

[Line Bot] 建置Line Bot環境(使用Bottender + Heroku)

Bottender

Bottender是chot bot框架,著重async function語法
安裝時,node.js的版本需大於7.6

安裝Bottender的環境參考下篇
https://bottender.js.org/docs/GettingStarted



Heroku

安裝完heroku cli後
$ heroku login
將repository clone至本機
$ heroku git:clone -a <heroku app name>
$ cd <heroku app name>
push 至hekoku
$ git add .
$ git commit -am "make it better"
$ git push heroku master


Line Developer

登入至line developer新增帳號
若要刪除自動回覆訊息,要進入Line @Manager移除


訊息 -> 自動回覆訊息 delete
記下Line Developer頁面底下的Channel secret、Channel access token
並於到Heroku頁面設定參數 (Settings -> Config Vars新增變數)




另外要Use Webhhok要啟用Enabled
Webhook URL加入heroku app的網址


JS有兩的地方要調整,accessToken和channelSecret改成讀參數

bottender.config.js

module.exports = {
  // line
  accessToken: process.env.ACCESS_TOKEN,
  channelSecret: process.env.CHANNEL_SECRET,
};

sendMethod改成 reply
index.js

const { LineBot } = require('bottender');
const { createServer } = require('bottender/express');

const config = require('./bottender.config.js');

const bot = new LineBot({
  accessToken: config.accessToken,
  channelSecret: config.channelSecret,
  sendMethod: 'reply', // Default: 'push'
});

bot.onEvent(async context => {
  await context.sendText('Hello World');
});

const server = createServer(bot);

server.listen((process.env.PORT || 5000), () => {
  console.log('server is running on 5000 port...');
});


執行畫面


Ref:
http://rainstingtw.blogspot.com/2018/07/how-to-use-bottender-with-heroku-line-bot.html
來寫個氣象機器人吧!


 

[SQL Server] Where condition in comma seperated string

最近在抓資料遇到一個狀況
比對的條件是用逗號分隔,如下圖


但是SQL Server沒有類似現成的function
另外,也不能在條件的前後加"'",直接用in
後來找到一篇,可以用charIndex,並在前後加","
這樣就可以比對出想要的結果了

DECLARE @categoryId INT
SET @categoryId = 3

SELECT *
FROM myTable
WHERE CHARINDEX(',' + CAST(@categoryId AS VARCHAR(MAX)) + ',', ',' + categoryIds + ',') > 0

Ref:
https://stackoverflow.com/questions/33278789/how-can-i-check-whether-a-number-is-contained-in-comma-separated-list-stored-in