MSSQL varchar, nvarchar 預設長度 30
nvarchar, varchar 不給長度時, 有預設 30 的限制
以後記得, 所有對資料庫欄位的 update, 都應該要備份, 以確保不會有慘劇發生…
- 方法1: 新增一個備份用的欄位, 將值 copy 過去
- 方法2: 直接對資料庫備份
Posted on April 15th, 2016
MSSQL varchar, nvarchar 預設長度 30
nvarchar, varchar 不給長度時, 有預設 30 的限制
以後記得, 所有對資料庫欄位的 update, 都應該要備份, 以確保不會有慘劇發生…
Posted on December 26th, 2014
START WITH ... CONNECT BY PRIOR ...SELECT Id
FROM Category START WITH Id=2 CONNECT BY
PRIOR Id=Parentid
withWITH n (id, name) AS
(SELECT id, name
FROM category
WHERE id = 2 --父節點 id
UNION ALL SELECT child.id, child.name
FROM Category AS child, n
WHERE n.id = child.parentId)
SELECT * FROM n
如果要透過 n 再查詢, 例如查詢分類樹底下所有 binder
(假設 Binder 和 Category 透過 Binder_Category 來記錄關聯性)
WITH n (id, name) AS
(SELECT id,
name
FROM category
WHERE id = 2 --父節點 id
UNION ALL SELECT child.id,
child.name
FROM Category AS child, n
WHERE n.id = child.parentId)
SELECT *
FROM Binder bb
LEFT JOIN Binder_Category bc ON bc.binder_id = bb.id
WHERE bc.cat_id IN (SELECT id FROM n)
(感謝 April 大大教學)
Posted on November 7th, 2014
No - you have to do it the other way around: add it right from the get go as INT IDENTITY - it will be filled with identity values when you do this:
ALTER TABLE dbo.YourTable
ADD ID INT IDENTITY
and then you can make it the primary key:
ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable
PRIMARY KEY(ID)
Posted on November 7th, 2014
匯入大檔, 錯誤大概都會長這樣...

資料來源選擇: 一般檔案來源
選好欲匯入的檔案,左側點選 進階
將文字欄位的 DataType 改成 文字資料流[DT_TEXT]

二位元組不帶正負號的整數[DT_UI2]圖片來源: http://lanfar.pixnet.net
REF: SQL Server:匯入資料遭遇“文字已截斷"錯誤 from 蘭花❉心得報告
Posted on September 17th, 2014
這就是 Stored Procedures, 不過這似乎要有權限才能完成!
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
Posted on September 16th, 2014
The following SQL will get you the row count of all tables in a database:
CREATE TABLE #counts
(
table_name varchar(255),
row_count int
)
EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC
The output will be a list of tables and their row counts.
If you just want the total row count across the whole database, appending:
SELECT SUM(row_count) AS total_row_count FROM #counts
will get you a single value for the total number of rows in the whole database.
REF:
Fetch_row_count_for_all_tables_in_a_SQL_SERVER by adrianbanks from stackoverflow.com
Posted on June 5th, 2014

Posted on June 5th, 2014
Posted on November 22nd, 2013
Notes from my experience.