這篇文章給大家分享的是有關(guān)SQL Server如何查找表名或列名中包含空格的表和列的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)憑借專業(yè)的設(shè)計團隊扎實的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、成都網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都十多年的網(wǎng)站建設(shè)設(shè)計經(jīng)驗,為成都上千多家中小型企業(yè)策劃設(shè)計了網(wǎng)站。
如下案例所示:
USE TEST; GO --表TEST_COLUMN中兩個字段都包含有空格 CREATE TABLE TEST_COLUMN ( "ID " INT IDENTITY (1,1), [Name ] VARCHAR(32), [Normal] VARCHAR(32) ); GO --表[TEST_TABLE ]中包含空格, 里面對應(yīng)三個字段,一個前面包含空格(后面詳細闡述),一個字段中間包含空格,一個字段后面包含空格。 CREATE TABLE [TEST_TABLE ] ( [ F_NAME] NVARCHAR(32), [M NAME] NVARCHAR(32), [L_NAME ] NVARCHAR(32) ) GO
實現(xiàn)方法:
那么要如何找出表名或字段名包含空格的相關(guān)信息呢? 不管是常規(guī)方法還是正則表達式,這個都會效率不高。我們可以用一個取巧的方法,就是通過字段的字符數(shù)和字節(jié)數(shù)的規(guī)律來判斷,如果沒有包含空格,那么列名的字節(jié)數(shù)和字符數(shù)滿足下面規(guī)律(表名也是如此):
DATALENGTH(name) = 2* LEN(name)
SELECT name , DATALENGTH(name) AS NAME_BYTES , LEN(name) AS NAME_CHARACTER FROM sys.columns WHERE object_id = OBJECT_ID('TEST_COLUMN'); clip_image001
原理是這樣的,保存這些元數(shù)據(jù)的字段類型為sysname ,其實這個系統(tǒng)數(shù)據(jù)類型,用于定義表列、變量以及存儲過程的參數(shù),是nvarchar(128)的同義詞。所以一個字母占2個字節(jié)。那么我們安裝這個規(guī)律寫了一個腳本來檢查數(shù)據(jù)中那些表名或字段名包含空格。方便巡檢。如下測試所示
IF OBJECT_ID('tempdb.dbo.#TabColums') IS NOT NULL DROP TABLE dbo.#TabColums; CREATE TABLE #TabColums ( object_id INT , column_id INT ) INSERT INTO #TabColums SELECT object_id , column_id FROM sys.columns WHERE DATALENGTH(name) != LEN(name) * 2 SELECT TL.name AS TableName, C.Name AS FieldName, T.Name AS DataType, DATALENGTH(C.name) AS COLUMN_DATALENGTH, LEN(C.name) AS COLUMN_LENGTH, CASE WHEN C.Max_Length = -1 THEN 'Max' ELSE CAST(C.Max_Length AS VARCHAR) END AS Max_Length, CASE WHEN C.is_nullable = 0 THEN '×' ELSE N'√' END AS Is_Nullable, C.is_identity, ISNULL(M.text, '') AS DefaultValue, ISNULL(P.value, '') AS FieldComment FROM sys.columns C INNER JOIN sys.types T ON C.system_type_id = T.user_type_id LEFT JOIN dbo.syscomments M ON M.id = C.default_object_id LEFT JOIN sys.extended_properties P ON P.major_id = C.object_id AND C.column_id = P.minor_id INNER JOIN sys.tables TL ON TL.object_id = C.object_id INNER JOIN #TabColums TC ON C.object_id = TC.object_id AND c.column_id = TC.column_id ORDER BY C.Column_Id ASC
那么為什么表名TEST_TABLE的三個字段里面,前面包含空格與與中間包含空格都識別不出來呢?這個與數(shù)據(jù)庫的LEN函數(shù)有關(guān)系,LEN函數(shù)返回指定字符串表達式的字符數(shù),其中
不包含尾隨空格。所以這個腳本是無法排查表名或字段名前面包含空格的。如果要排查這種情況,就需要使用下面SQL腳本(中間包含空格在此略過,這個不符合命名規(guī)則):
SELECT * FROM sys.columns WHERE NAME LIKE ' %' --字段前面包含空格。
其實到了這一步,還沒有完,如果一個實例,里面有十幾個數(shù)據(jù)庫,那么使用上面這個腳本,我要切換數(shù)據(jù)庫,執(zhí)行十幾次,對于我這種懶人來說,我覺得無法忍受的。那么必須寫
一個腳本,將所有數(shù)據(jù)庫全部檢查完。本來想用sys.sp_MSforeachdb,但是這個內(nèi)部存儲過程有一些限制,遂寫了下面腳本。
DECLARE @db_name NVARCHAR(32); DECLARE @sql_text NVARCHAR(MAX); DECLARE @db TABLE ( database_name NVARCHAR(64) ); IF OBJECT_ID('tempdb.dbo.#TabColums') IS NOT NULL DROP TABLE dbo.#TabColums; CREATE TABLE #TabColums ( object_id INT , column_id INT ); INSERT INTO @db SELECT name FROM sys.databases WHERE state_desc='ONLINE' AND database_id !=2; WHILE (1=1) BEGIN SELECT TOP 1 @db_name = database_name FROM @db ORDER BY 1; IF @@ROWCOUNT = 0 RETURN; SET @sql_text =N'USE ' + @db_name +'; TRUNCATE TABLE #TabColums; INSERT INTO #TabColums SELECT object_id , column_id FROM sys.columns WHERE DATALENGTH(name) != LEN(name) * 2; SELECT ''' + @db_name + ''' AS DatabaseName, TL.name AS TableName , C.name AS FieldName , T.name AS DataType , DATALENGTH(C.name) AS COLUMN_DATALENGTH , LEN(C.name) AS COLUMN_LENGTH , CASE WHEN C.max_length = -1 THEN ''Max'' ELSE CAST(C.max_length AS VARCHAR) END AS Max_Length , CASE WHEN C.is_nullable = 0 THEN ''×'' ELSE ''√'' END AS Is_Nullable , C.is_identity , ISNULL(M.text, '''') AS DefaultValue , ISNULL(P.value, '''') AS FieldComment FROM sys.columns C INNER JOIN sys.types T ON C.system_type_id = T.user_type_id LEFT JOIN dbo.syscomments M ON M.id = C.default_object_id LEFT JOIN sys.extended_properties P ON P.major_id = C.object_id AND C.column_id = P.minor_id INNER JOIN sys.tables TL ON TL.object_id = C.object_id INNER JOIN #TabColums TC ON C.object_id = TC.object_id AND C.column_id = TC.column_id ORDER BY C.column_id ASC;'; PRINT(@sql_text); EXECUTE(@sql_text); DELETE FROM @db WHERE database_name=@db_name; END TRUNCATE TABLE #TabColums; DROP TABLE #TabColums;
另外,對應(yīng)表名而言,可以使用下面腳本。在此略過,不做過多介紹!
DECLARE @db_name NVARCHAR(32); DECLARE @sql_text NVARCHAR(MAX); DECLARE @db TABLE ( database_name NVARCHAR(64) ); INSERT INTO @db SELECT name FROM sys.databases WHERE state_desc='ONLINE' AND database_id !=2; WHILE (1=1) BEGIN SELECT TOP 1 @db_name = database_name FROM @db ORDER BY 1; IF @@ROWCOUNT = 0 RETURN; SET @sql_text =N'USE ' + @db_name +'; SELECT ''' + @db_name + ''' as database_name, name, DATALENGTH(name) as table_name_bytes, LEN(name) as table_name_character, type_desc,create_date,modify_date FROM sys.tables WHERE DATALENGTH(name) != LEN(name) * 2; '; PRINT(@sql_text); EXECUTE(@sql_text); DELETE FROM @db WHERE database_name=@db_name; END
感謝各位的閱讀!關(guān)于“SQL Server如何查找表名或列名中包含空格的表和列”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!