Category Archives: SQL Server

小心 MAX

image

廣告

SSMS 查詢文字結果過長

雖然 Grid 設定 Max 可達 64K,但實際 4 萬多字可能就被截斷了,最好是轉成 XML 呈現。

OPENROWSET 取 Tab 分隔檔

SQL 2017 以前的做法

SELECT * 
FROM OPENROWSET(
    'Microsoft.ACE.OLEDB.12.0', 
    'Text; HDR=YES; Database=C:\Text', 
    'SELECT * FROM testupload2.txt')

In C:\Text\schema.ini:

[testupload2.txt]
ColNameHeader=True
Format=TabDelimited
MaxScanRows=0

https://dba.stackexchange.com/questions/29306/can-i-query-a-tab-delimited-file-from-ssms

https://docs.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver

髒資料髒進髒出

image

image

是否啟用壓縮

SELECT ix.name,st.name,schema_name(st.schema_id)
FROM sys.partitions SP
JOIN sys.tables ST ON st.object_id = sp.object_id
left JOIN sys.sysindexes IX ON sp.object_id = ix.id and sp.index_id = ix.indid
WHERE sp.data_compression = 0

內縮在 Python 是程式區塊…

這系列的練習錯誤不少,自行修正後,最終可執行…

內縮在 Python 是程式區塊,https://docs.microsoft.com/zh-tw/sql/advanced-analytics/tutorials/sqldev-py5-train-and-save-a-model-using-t-sql 範例中因為有內縮:

image

會造成如下的錯誤:

訊息 39004,層級 16,狀態 19,行 37
執行 ‘sp_execute_external_script’ 時發生 ‘Python’ 指令碼錯誤,HRESULT 為 0x80004004。
訊息 39019,層級 16,狀態 2,行 37
發生外部指令碼錯誤:

Error in execution.  Check the output for more information.
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "C:\PROGRA~1\MICROS~1\MSSQL1~1.MSS\MSSQL\EXTENS~1\MSSQLSERVER01\3023566F-9F2F-4E78-823E-5E38CC9EED66\sqlindb.py", line 33
    import numpy
    ^
IndentationError: unexpected indent

SqlSatelliteCall error: Error in execution.  Check the output for more information.
來自外部指令碼的 STDOUT 訊息:
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER1000\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 406, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER1000\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 291, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.

只要把內縮部分砍掉,就可以正常執行

下一階段的練習也有相同的問題:https://docs.microsoft.com/zh-tw/sql/advanced-analytics/tutorials/sqldev-py6-operationalize-the-model

且參數有寫錯,修正後皆可執行

SqlBulkCopy 載入資料的資料精準度錯誤

小心

https://forums.asp.net/t/1300113.aspx?SqlBulkCopy+Precision+Error+from+C+Double+to+Sql+Decimal+

.NET 只有浮點數(single/double),並不支援 SQL Server 的 Numeric/Decimal 或 Oracle 的 Number,當使用 SqlBulkCopy 載入資料到 SQL Server 的目的欄位型別為 Numeric/Decimal 有指定小數位數十,可能發生轉換時,精準度差最小位數。

以上述的連結範例而言 27.2 放到 Numeric(10,4) 會造成 27.1999,若放到 Numeric(10,5),則是 27.19999。

可以採用如提問者自己在下方的解法,先存到中介 table,對應欄位宣告成 float,對應 .NET 使用的 double,然後再從中介轉到真正的目標 table,這時 float 轉 numeric,則數值就會正確。

將 object owner 非 dbo 的改成 dbo

declare cur cursor for
SELECT ‘[‘ + SCHEMA_NAME(schema_id) + ‘].[‘ + [name] + ‘]’
FROM sys.objects
WHERE USER_NAME(OBJECTPROPERTY(object_id, ‘OwnerId’)) not in(‘dbo’,’sys’) and is_ms_shipped=0
declare @obj nvarchar(1000),@sql nvarchar(1000)
open cur
fetch cur into @obj
while @@FETCH_STATUS=0
begin
    set @sql=’alter authorization on ‘ + @obj  + ‘ to dbo’
    –print @sql
    exec(@sql)
    fetch cur into @obj
end
close cur
deallocate cur

rename 預存程序後,最好再 alter 一遍

聽課的朋友提出的有趣問題,sp rename 後,查詢 sp_helptext 的 create proc 部分卻使用舊的預存程序名稱。實作範例如下:

 

use tempdb
go
create proc sp_a
as
print ‘hello’
go
exec sp_rename ‘sp_b’,’sp_b’

–rename 後保有語法定義 metadata 的內容不會變,所以會回傳如下的警告

Caution: Changing any part of an object name could break scripts and stored procedures.

select object_name(id) objectame,* from syscomments
select object_name(object_id) objectname,* from sys.sql_modules

image

 

–所以取回錯誤的結果

sp_helptext ‘sp_b’
go

image

 

–可以再建同名的 sp

create proc sp_a
as
print ‘hello a’

exec sp_helptext ‘sp_a’
exec sp_helptext ‘sp_b’
go

 

–有趣的是利用 object explorer 會對

image

–但透過 profiler 到的語法如下

exec sp_executesql N’SELECT
NULL AS [Text],
ISNULL(smsp.definition, ssmsp.definition) AS [Definition]
FROM
sys.all_objects AS sp
LEFT OUTER JOIN sys.sql_modules AS smsp ON smsp.object_id = sp.object_id
LEFT OUTER JOIN sys.system_sql_modules AS ssmsp ON ssmsp.object_id = sp.object_id
WHERE
(sp.type = @_msparam_0 OR sp.type = @_msparam_1 OR sp.type=@_msparam_2)and(sp.name=@_msparam_3 and SCHEMA_NAME(sp.schema_id)=@_msparam_4)’,N’@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000),@_msparam_2 nvarchar(4000),@_msparam_3 nvarchar(4000),@_msparam_4 nvarchar(4000)’,@_msparam_0=N’P’,@_msparam_1=N’RF’,@_msparam_2=N’PC’,@_msparam_3=N’sp_c’,@_msparam_4=N’dbo’

查詢的依然是 sys.sql_modules,所以仍是錯的,顯然 object explorer 會自行依物件名字改正產出的語法

–alter 之後會改成正確的 create proc sp_b

alter proc sp_b
as
print ‘hello’

所以 rename 之後,最好再 alter 一遍,保證 metadata 的正確性

透過 T-SQL Script 建立 Table 定義

Sorry,忘了來源出處,但為了讓朋友 Copy,所以貼在此處…

CREATE proc [ETL].[spCreateTable] @table_name SYSNAME,@On nvarchar(max)=",@SQLScript nvarchar(max) output
as
DECLARE
      @object_name SYSNAME
    , @object_id INT

SELECT
      @object_name = ‘[‘ + s.name + ‘].[‘ + o.name + ‘]’
    , @object_id = o.[object_id]
FROM CPADW.sys.objects o WITH (NOWAIT)
JOIN CPADW.sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id]
WHERE s.name + ‘.’ + o.name = @table_name
    AND o.[type] = ‘U’
    AND o.is_ms_shipped = 0

DECLARE @SQL NVARCHAR(MAX) = “

;WITH index_column AS
(
    SELECT
          ic.[object_id]
        , ic.index_id
        , ic.is_descending_key
        , ic.is_included_column
        , c.name
    FROM CPADW.sys.index_columns ic WITH (NOWAIT)
    JOIN CPADW.sys.columns c WITH (NOWAIT) ON ic.[object_id] = c.[object_id] AND ic.column_id = c.column_id
    WHERE ic.[object_id] = @object_id
),
fk_columns AS
(
     SELECT
          k.constraint_object_id
        , cname = c.name
        , rcname = rc.name
    FROM CPADW.sys.foreign_key_columns k WITH (NOWAIT)
    JOIN CPADW.sys.columns rc WITH (NOWAIT) ON rc.[object_id] = k.referenced_object_id AND rc.column_id = k.referenced_column_id
    JOIN CPADW.sys.columns c WITH (NOWAIT) ON c.[object_id] = k.parent_object_id AND c.column_id = k.parent_column_id
    WHERE k.parent_object_id = @object_id
)
SELECT @SQL = ‘CREATE TABLE ‘ + @object_name + CHAR(13) + ‘(‘ + CHAR(13) + STUFF((
    SELECT CHAR(9) + ‘, [‘ + c.name + ‘] ‘ +
        CASE WHEN c.is_computed = 1
            THEN ‘AS ‘ + cc.[definition]
            ELSE UPPER(tp.name) +
                CASE WHEN tp.name IN (‘varchar’, ‘char’, ‘varbinary’, ‘binary’, ‘text’)
                       THEN ‘(‘ + CASE WHEN c.max_length = -1 THEN ‘MAX’ ELSE CAST(c.max_length AS VARCHAR(5)) END + ‘)’
                     WHEN tp.name IN (‘nvarchar’, ‘nchar’, ‘ntext’)
                       THEN ‘(‘ + CASE WHEN c.max_length = -1 THEN ‘MAX’ ELSE CAST(c.max_length / 2 AS VARCHAR(5)) END + ‘)’
                     WHEN tp.name IN (‘datetime2’, ‘time2’, ‘datetimeoffset’)
                       THEN ‘(‘ + CAST(c.scale AS VARCHAR(5)) + ‘)’
                     WHEN tp.name = ‘decimal’
                       THEN ‘(‘ + CAST(c.[precision] AS VARCHAR(5)) + ‘,’ + CAST(c.scale AS VARCHAR(5)) + ‘)’
                    ELSE "
                END +
                CASE WHEN c.collation_name IS NOT NULL THEN ‘ COLLATE ‘ + c.collation_name ELSE " END +
                CASE WHEN c.is_nullable = 1 THEN ‘ NULL’ ELSE ‘ NOT NULL’ END +
                CASE WHEN dc.[definition] IS NOT NULL THEN ‘ DEFAULT’ + dc.[definition] ELSE " END +
                CASE WHEN ic.is_identity = 1 THEN ‘ IDENTITY(‘ + CAST(ISNULL(ic.seed_value, ‘0’) AS CHAR(1)) + ‘,’ + CAST(ISNULL(ic.increment_value, ‘1’) AS CHAR(1)) + ‘)’ ELSE " END
        END + CHAR(13)
    FROM CPADW.sys.columns c WITH (NOWAIT)
    JOIN CPADW.sys.types tp WITH (NOWAIT) ON c.user_type_id = tp.user_type_id
    LEFT JOIN CPADW.sys.computed_columns cc WITH (NOWAIT) ON c.[object_id] = cc.[object_id] AND c.column_id = cc.column_id
    LEFT JOIN CPADW.sys.default_constraints dc WITH (NOWAIT) ON c.default_object_id != 0 AND c.[object_id] = dc.parent_object_id AND c.column_id = dc.parent_column_id
    LEFT JOIN CPADW.sys.identity_columns ic WITH (NOWAIT) ON c.is_identity = 1 AND c.[object_id] = ic.[object_id] AND c.column_id = ic.column_id
    WHERE c.[object_id] = @object_id
    ORDER BY c.column_id
    FOR XML PATH(“), TYPE).value(‘.’, ‘NVARCHAR(MAX)’), 1, 2, CHAR(9) + ‘ ‘)
    + ISNULL((SELECT CHAR(9) + ‘, CONSTRAINT [‘ + k.name + ‘] PRIMARY KEY (‘ +
                    (SELECT STUFF((
                         SELECT ‘, [‘ + c.name + ‘] ‘ + CASE WHEN ic.is_descending_key = 1 THEN ‘DESC’ ELSE ‘ASC’ END
                         FROM CPADW.sys.index_columns ic WITH (NOWAIT)
                         JOIN CPADW.sys.columns c WITH (NOWAIT) ON c.[object_id] = ic.[object_id] AND c.column_id = ic.column_id
                         WHERE ic.is_included_column = 0
                             AND ic.[object_id] = k.parent_object_id
                             AND ic.index_id = k.unique_index_id    
                         FOR XML PATH(N"), TYPE).value(‘.’, ‘NVARCHAR(MAX)’), 1, 2, “))
            + ‘)’ + CHAR(13)
            FROM CPADW.sys.key_constraints k WITH (NOWAIT)
            WHERE k.parent_object_id = @object_id
                AND k.[type] = ‘PK’), “) + ‘) ‘ + @On + CHAR(13)
    + ISNULL((SELECT (
        SELECT CHAR(13) +
             ‘ALTER TABLE ‘ + @object_name + ‘ WITH’
            + CASE WHEN fk.is_not_trusted = 1
                THEN ‘ NOCHECK’
                ELSE ‘ CHECK’
              END +
              ‘ ADD CONSTRAINT [‘ + fk.name  + ‘] FOREIGN KEY(‘
              + STUFF((
                SELECT ‘, [‘ + k.cname + ‘]’
                FROM fk_columns k
                WHERE k.constraint_object_id = fk.[object_id]
                FOR XML PATH(“), TYPE).value(‘.’, ‘NVARCHAR(MAX)’), 1, 2, “)
               + ‘)’ +
              ‘ REFERENCES [‘ + SCHEMA_NAME(ro.[schema_id]) + ‘].[‘ + ro.name + ‘] (‘
              + STUFF((
                SELECT ‘, [‘ + k.rcname + ‘]’
                FROM fk_columns k
                WHERE k.constraint_object_id = fk.[object_id]
                FOR XML PATH(“), TYPE).value(‘.’, ‘NVARCHAR(MAX)’), 1, 2, “)
               + ‘)’
            + CASE
                WHEN fk.delete_referential_action = 1 THEN ‘ ON DELETE CASCADE’
                WHEN fk.delete_referential_action = 2 THEN ‘ ON DELETE SET NULL’
                WHEN fk.delete_referential_action = 3 THEN ‘ ON DELETE SET DEFAULT’
                ELSE "
              END
            + CASE
                WHEN fk.update_referential_action = 1 THEN ‘ ON UPDATE CASCADE’
                WHEN fk.update_referential_action = 2 THEN ‘ ON UPDATE SET NULL’
                WHEN fk.update_referential_action = 3 THEN ‘ ON UPDATE SET DEFAULT’ 
                ELSE "
              END
            + CHAR(13) + ‘ALTER TABLE ‘ + @object_name + ‘ CHECK CONSTRAINT [‘ + fk.name  + ‘]’ + CHAR(13)
        FROM CPADW.sys.foreign_keys fk WITH (NOWAIT)
        JOIN CPADW.sys.objects ro WITH (NOWAIT) ON ro.[object_id] = fk.referenced_object_id
        WHERE fk.parent_object_id = @object_id
        FOR XML PATH(N"), TYPE).value(‘.’, ‘NVARCHAR(MAX)’)), “)
    + ISNULL(((SELECT
         CHAR(13) + ‘CREATE’ + CASE WHEN i.is_unique = 1 THEN ‘ UNIQUE’ ELSE " END
                + ‘ NONCLUSTERED INDEX [‘ + i.name + ‘] ON ‘ + @object_name + ‘ (‘ +
                STUFF((
                SELECT ‘, [‘ + c.name + ‘]’ + CASE WHEN c.is_descending_key = 1 THEN ‘ DESC’ ELSE ‘ ASC’ END
                FROM index_column c
                WHERE c.is_included_column = 0
                    AND c.index_id = i.index_id
                FOR XML PATH(“), TYPE).value(‘.’, ‘NVARCHAR(MAX)’), 1, 2, “) + ‘)’ 
                + ISNULL(CHAR(13) + ‘INCLUDE (‘ +
                    STUFF((
                    SELECT ‘, [‘ + c.name + ‘]’
                    FROM index_column c
                    WHERE c.is_included_column = 1
                        AND c.index_id = i.index_id
                    FOR XML PATH(“), TYPE).value(‘.’, ‘NVARCHAR(MAX)’), 1, 2, “) + ‘)’, “)  + CHAR(13)
        FROM CPADW.sys.indexes i WITH (NOWAIT)
        WHERE i.[object_id] = @object_id
            AND i.is_primary_key = 0
            AND i.[type] = 2
        FOR XML PATH(“), TYPE).value(‘.’, ‘NVARCHAR(MAX)’)
    ), “)
set @SQLScript=@SQL