顯示具有 SQL 標籤的文章。 顯示所有文章
顯示具有 SQL 標籤的文章。 顯示所有文章

2023年8月4日 星期五

MSSQL Import Flat File to SQL Wizard (CSV 匯入 Table)

 https://learn.microsoft.com/zh-tw/sql/relational-databases/import-export/import-flat-file-wizard?view=sql-server-ver16

若欄位中有 ,,可以用  ‘’ 來包住

例:

idnameno
1Andy A'0912345678,0987654321'
2Bill B01-2345678

匯入執行要用 Text qualifier 輸入 ‘’



2021年9月8日 星期三

MSSQL 跨主機查詢

安全性設定

sp_configure 'show advanced options', 1;  

RECONFIGURE;

GO 

sp_configure 'Ad Hoc Distributed Queries', 1;  

RECONFIGURE;  

GO  

再下 SQL

opendatasource('SQLOLEDB','Data Source=172.16.2.48;User ID=hcm2db;Password=hcm2db5678').[db1].[dbo].[table1]


完成後再把安全性改回來




 

2020年7月30日 星期四

MSSQL 找出欄位在那個 Table

SELECT TABLE_NAME,COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME = '欲尋找欄位名稱' 

2020年3月25日 星期三

MSSQL 資料庫中找出資料在那個 Table

參考來源:https://blog.miniasp.com/post/2010/07/12/Search-all-columns-of-all-tables-in-a-database-for-a-keyword

2018年1月8日 星期一

MSSQL 顯示 TABLE 中的欄位名稱

MSSQL 顯示 TABLE 中的欄位名稱

USE [DB NAME]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLE NAME'

exec sp_columns [TABLE NAME]

2017年5月25日 星期四

mysql commad 常用

登入 mysql
mysql -u root -p

列出所有資料庫
show databases;

列出所有資料表
show tables;

參考:http://note.drx.tw/2012/12/mysql-syntax.html

2017年5月9日 星期二

MSSQL Bulk Insert 匯入文字檔

用 Bulk Insert 載入文字檔實例

假設我們有一個文字檔, 要寫入 table: StList 中, 且 StList schema 如下:

CREATE TABLE StList
(
StFName nvarchar(50) NOT NULL,
StLName nvarchar(50) NOT NULL,
StEmail nvarchar(100) NOT NULL
)
go


文字檔型態一:

Kelly中文,Reynold,kelly@reynold.com
John鮰,Smith,bill@smith.com
Sara,Parker,sara@parker.com
這個例子較簡單, 因此我們直接用下述語法:

BULK INSERT StList FROM 'c:\TxtFile2.txt' WITH (FIELDTERMINATOR = ',',CODEPAGE = '65001')


文字檔型態二:

"Kelly","Reynold","kelly@reynold.com"
"John","Smith","bill@smith.com"
"Sara","Parker","Parker"

這個例子就比較麻煩了, 若直接用以下語法,

BULK INSERT StList FROM 'c:\TxtFile2.txt' WITH (FIELDTERMINATOR = '","',CODEPAGE = '65001')
因為每個欄位的資料前後都有 double quote, 若直接用 "," 分開, 將造成"最前面"和"最後面"的double quote 拿不掉, 也就是會變成:

"Kelly Reynold kelly@reynold.com"
"John Smith bill@smith.com"
"Sara Parker Parker"
可改用以下程式處理:

BULK INSERT StList FROM 'c:\TxtFile2.txt' WITH (FIELDTERMINATOR = '","')
UPDATE StList SET StFName = SUBSTRING(StFName,2,DATALENGTH(StFName)-1) -- 【"Kelly】 變 【Kelly】
UPDATE StList SET StEmail = SUBSTRING(StFName,1,DATALENGTH(StEmail )-1) -- 【kelly@reynold.com"】 變 【kelly@reynold.com】

PS:若沒有支援 65001 參考:https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql

參考來源:http://cbw0731.pixnet.net/blog/post/24994515

2017年3月9日 星期四

MySQL binlog (backup,archive,restore)

#my.ini 開啟 binlog,保留 7 天 (WAMP 2.5)
log-bin=L:\/
expire_logs_days = 7

#login root
mysql -u root -p
pcPxyncjC8hCbUn

#主機名稱
select @@hostname;
show variables where Variable_name like '%host%';

#目前連線
select host from information_schema.processlist;
show processlist;


#顯示檔名
show master status;
SHOW BINARY LOGS;

#binlog 參數
show variables like '%bin%';

#查看 binlog 内容
show binlog events;
show binlog events in '.000002';

#重播資料庫變動過程
mysqlbinlog hostname-bin.001

#還原 binlog
mysqlbinlog -H --set-charset="utf8" --start-datetime="2017-03-08 15:22:00" --stop-datetime="2017-03-09 10:00:00" L:\.000001 L:\.000002 > L:\binary.sql

#backup.bat(完整備份,清除 binlog)
set x=%date:~0,4%%date:~5,2%%date:~8,2%
mysqldump --flush-logs -u root -pLpcPxyncjC8hCbUn chair_flow > L:\backup%x%.sql

#http://forum.slime.com.tw/thread208729.html
#http://g9677602.blogspot.tw/2010/01/mysql-binary-log-mysql-bin-log.html
#https://blog.wu-boy.com/2007/03/mysql-mysqlbinlog-%E8%B3%87%E6%96%99%E5%BA%AB%E8%99%95%E7%90%86%E4%BA%8C%E9%80%B2%E5%88%B6%E6%97%A5%E8%AA%8C%E6%AA%94%E6%A1%88%E7%9A%84%E5%AF%A6%E7%94%A8%E5%B7%A5%E5%85%B7/

#取代欄位字串
UPDATE 表格
SET 欄位名稱 = REPLACE(欄位名稱, '123\', '')
WHERE ID = 4

2016年6月21日 星期二

修改 MYSQL 預設語言

my.ini

# Change your locale here !
lc-messages=en_US

2015年11月6日 星期五

顯示某個字元位前所有字

SELECT         SUBSTRING(PROD_NO, 1, CHARINDEX('4', PROD_NO)) AS Expr1
FROM             PRODUCT 

例如SELECT出二字串"123@gmail.com"及"456@gmail.com"
顯示出@之前的字串---變成"123"及"456"

參考來源:http://www.blueshop.com.tw/board/FUM20041006152735ZFS/BRD200301231022589AD.html

2015年5月4日 星期一

Reset autoincrement in Microsoft SQL

DBCC CHECKIDENT ("YourTableNameHere", RESEED, 1);

2015年2月4日 星期三

Oracle Enable/Disable Archive Log Mode 10g/11g

Enable Archive Log Mode
The following are the steps required to enable archive log mode on an Oracle 10g or 11g database.
Verify the database log mode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[oracle@ora1 ~]$ sqlplus / as sysdba
 
SQL*Plus: Release 11.2.0.1.0 Production on Thu Apr 8 12:02:52 2010
 
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
 
 
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
 
SQL> archive log list
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     25
Current log sequence           27
SQL>
The log mode is No Archive Mode. Note that Archive destination is USE_DB_RECOVERY_FILE_DEST. You can determine the path by looking at the parameter RECOVERY_FILE_DEST.
1
2
3
4
5
6
7
8
SQL> show parameter recovery_file_dest
 
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      /u01/app/oracle/flash_recovery
                                                 _area
db_recovery_file_dest_size           big integer 3852M
SQL>
By default, archive logs will be written to the flash recovery area. If you do not want to write archive logs to the flash recovery area you can set the parameter LOG_ARCHIVE_DEST_n to the location in which you wish to write archive logs.
1
2
3
4
5
6
7
8
9
10
11
SQL> alter system set log_archive_dest_1='LOCATION=/u02/app/oracle/oradata/orcl/arch' scope = both;
 
System altered.
 
SQL> archive log list;
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            /u02/app/oracle/oradata/orcl/arch
Oldest online log sequence     25
Current log sequence           27
SQL>
Now we shutdown the database and bring it backup in mount mode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
 
Total System Global Area  849530880 bytes
Fixed Size                  1339824 bytes
Variable Size             511708752 bytes
Database Buffers          331350016 bytes
Redo Buffers                5132288 bytes
Database mounted.
SQL>
Lastly all that is needed it set archive log mode and open the database.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SQL> alter database archivelog;
 
Database altered.
 
SQL> alter database open;
 
Database altered.
 
SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u02/app/oracle/oradata/orcl/arch
Oldest online log sequence     25
Next log sequence to archive   27
Current log sequence           27
SQL>
We can now see that archive log mode is enabled. Notice that Automatic archive is enabled as well. In Oracle 9i an earlier another parameter needed to be set in order to enable automatic archiving. This in no longer the case in 10g and 11g as automatic archiving is enabled when the database is placed in archive log mode.
You can switch to the log file to see that an archive is written to archive log location.
1
2
3
4
5
6
7
8
9
10
11
SQL> alter system switch logfile;
 
System altered.
 
SQL> host
[oracle@ora1 ~]$ ls /u02/app/oracle/oradata/orcl/arch
1_27_711369564.dbf
[oracle@ora1 ~]$ exit
exit
 
SQL>
Disable Archive Log Mode
Note: It is recommended to perform a backup of the database (after a shutdown normal or immediate) prior to changing the archive status of a database.
The following are the steps required to disable archive log mode on an Oracle 10g or 11g database.
Verify the database log mode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[oracle@ora1 ~]$ sqlplus / as sysdba
 
SQL*Plus: Release 11.2.0.1.0 Production on Thu Apr 8 12:54:05 2010
 
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
 
 
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
 
SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u02/app/oracle/oradata/orcl/arch
Oldest online log sequence     26
Next log sequence to archive   28
Current log sequence           28
SQL>
The Database log mode is Archive mode. Next we shut down the database and bring up back up in mount mode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
 
Total System Global Area  849530880 bytes
Fixed Size                  1339824 bytes
Variable Size             511708752 bytes
Database Buffers          331350016 bytes
Redo Buffers                5132288 bytes
Database mounted.
SQL>
All that is left is to disable archive log mode and open the database.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SQL> alter database noarchivelog;
 
Database altered.
 
SQL> alter database open;
 
Database altered.
 
SQL> archive log list;
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            /u02/app/oracle/oradata/orcl/arch
Oldest online log sequence     26
Current log sequence           28
SQL>
As you can see, ARCHIVELOG mode has been disabled.