Sql server 执行SQL查询时在结果表中显示空值
我有一个脚本,它执行SQL文件并在控制台中打印结果(目前)。问题是我需要区分结果表中返回的空值和空字符串 这是查询在Management Studio中返回的结果: 您可以看到它包含字符串、空字符串和空值 这是查询在PowerShell IDE中返回的结果: 空和空之间没有区别 最后几列也被剪切,只打印前10列 我怎样才能解决这个问题 这是我的密码:Sql server 执行SQL查询时在结果表中显示空值,sql-server,powershell,null,Sql Server,Powershell,Null,我有一个脚本,它执行SQL文件并在控制台中打印结果(目前)。问题是我需要区分结果表中返回的空值和空字符串 这是查询在Management Studio中返回的结果: 您可以看到它包含字符串、空字符串和空值 这是查询在PowerShell IDE中返回的结果: 空和空之间没有区别 最后几列也被剪切,只打印前10列 我怎样才能解决这个问题 这是我的密码: $ConnectionString = "Data Source=...;Initial Catalog=...;User Id=..;Pas
$ConnectionString = "Data Source=...;Initial Catalog=...;User Id=..;Password=.."
$FolderToSQLFiles = "C:\SQLFilesTestFolder";
#function that executes sql queries and return result tables
function GetSQLresults {
Param(
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, Position=0)] $SQLqueryText, # sql query text returned from file
)
$objConnection = New-Object System.Data.SqlClient.SqlConnection;
$objConnection.ConnectionString = $ConnectionString
$objConnection.Open();
$ObjCmd = New-Object System.Data.SqlClient.SqlCommand;
$ObjCmd.CommandText = $SQLqueryText;
$ObjCmd.Connection = $objConnection;
$ObjCmd.CommandTimeout = 0;
$objAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$objAdapter.SelectCommand = $ObjCmd;
$objDataSet = New-Object System.Data.DataSet;
$objAdapter.Fill($objDataSet);
$ResultSets = @(); #adding all result tables to array
for ($i=0; $i -lt $objDataSet.Tables.Count; $i++) {
$tmpResultTable = $objDataSet.Tables[$i] | Format-Table | Out-String;
$ResultSets += $tmpResultTable;
}
return $ResultSets
$query = $null;
$objDataSet = $null;
$objConnection.Close();
$objConnection = $null;
}
Get-ChildItem $FolderToSQLFiles -Filter *.sql | Foreach-Object {
$tmpSQLfilePath = $_.FullName #getting the sql file full path
$tmpSQLfileContent = (Get-Content $tmpSQLfilePath) -join "`n" #getting the content of the sql
GetSQLresults -SQLqueryText $tmpSQLfileContent #passing the sql query to the executing funciton
}
PowerShell会自动将空值转换为空字符串以进行输出。您需要检查字段中值的类型,并相应地调整输出/值 像这样的方法应该会奏效:
GetSQLresults ... |
Select-Object -Property *,@{n='Col006',e={
if ($_.Col006 -is [DBNull]) {'NULL'} else {$_.Col006}
}} -Exclude Col006
在Powershell中使用:$SQL_Command=“sqlcmd-S$DbServer-U$SQL_User-p$SQL_Pass-i$TSQL_File-o$CSV_OutPut_FileName-S','-w 65530” 调用表达式$SQL\u命令 调用表达式cmdlet的第一个变量($SQL_Command)存储sqlcmd命令作为命令计算或运行指定字符串 $TSQL_File变量是包含sql语句的文件名(例如:从TableName中选择GUID….sql server中的uniqueidentifier数据类型,或者您可以使用SELECT*FROM TableName)
在SQL Server中,输出将是您想要的(NULL和空字符串)…希望这有帮助。问题是我也不知道每个表的列名。我执行许多查询,它们都有不同的结果表。此外,一个查询可以有多个结果表:/然后,您需要枚举表,枚举列,并为列中的每个值替换
[DBNull]
类型的值。我不会为此提供交钥匙解决方案。
#----------------------------------------------------------------------------------------------------------
#--- This will generate the result that you want
#----------------------------------------------------------------------------------------------------------
#--- Each line of TSQL_FileName.TXT ($ScriptsArray) contains a file name ($TSQL_File) of the TSQL statement that need to be execute
#----------------------------------------------------------------------------------------------------------
FOREACH ($TSQL_File in $ScriptsArray)
{ $Num_TSQL_File++
$Result_MSG = ''
#----------------------------------------------------------------------------------------------------------
#--- GENERATE OUT PUT FILE NAME USE FOR CSV FILE WHEN WE RUN sqlcmd
#----------------------------------------------------------------------------------------------------------
$OutPut_File = (Get-Item $TSQL_File).Basename+'_'+$OutPut_DateTime
$OutPut_File = $OutPut_File.replace(' ','_')
$OutPut_File = $OutPut_File.replace('.','_')
$OutPut_File = $OutPut_File+'.CSV'
$OutPut_Error = ''
$SQL_Command = "sqlcmd -S $DbServer -U $SQL_User -P $SQL_Pass -i $TSQL_File -o $OutPut_File -s ',' -w 65530"
Invoke-Expression $SQL_Command
$OutPut_Array = Get-Content $OutPut_File
$ThirdLine = "|"+$OutPut_Array[3]+"|"
#----------------------------------------------------------------------------------------------------------
#--- Determine if an error had occured, if so send email notification
#----------------------------------------------------------------------------------------------------------
IF (($OutPut_Array -like "Error*") -or ($OutPut_Array -like "Msg*") -or ($OutPut_Array -like "Invalid*") -or ($OutPut_Array -like "Sqlcmd: Error*") )
{ $OutPut_Error = (Get-Item $TSQL_File).Basename+'_'+$OutPut_DateTime+'.ERR'
$OutPut_Array | out-file ".\$OutPut_Error"
$Result_MSG = 'Error!'
$OutPut_File = $OutPut_Error
$OutPut_FileSizeKb = $NumOfRow_Send = 0
$global:DbErr++
"{0,-16} {1,-40} {2,-60} {3,-10} {4,-4} {5,-4}" -f $DbServer, $TSQL_File, $OutPut_File, [int]$OutPut_FileSizeKb, $NumOfRow_Send, $Result_MSG
$Results_Array += ,@($Results_Array_Index, $DbServer, $TSQL_File, $OutPut_File, $OutPut_FileSizeKb, $NumOfRow_Send, $Result_MSG)
$Results_Array_Index++
}
ELSE
{ #----------------------------------------------------------------------------------------------------------
#--- Determine if zero row return from query, if so modify the array so that it will send only header info
#----------------------------------------------------------------------------------------------------------
IF ( ($OutPut_Array.Length -EQ 3) -and ($ThirdLine -EQ '||') )
{ $NeedFix_Array = Get-Content $OutPut_File
#-------------------------------------------------------------
#--- MODIFY THE ARRAY SO THAT IT WILL SEND ONLY THE HEADER
#-------------------------------------------------------------
$NeedFix_Array = $NeedFix_Array[1]
#-----------------------------------------------------------
#--- REMOVE BLANK SPACE FROM THE LINE
#-----------------------------------------------------------
$NeedFix_Array = $NeedFix_Array -replace '\s+', ' '
$NeedFix_Array = $NeedFix_Array -replace ' ,', ','
$NeedFix_Array = $NeedFix_Array -replace ', ', ','
#-----------------------------------------------------------
$NeedFix_Array | out-file $OutPut_File -Encoding ASCII
$OutPut_FileSizeKb = (Get-Item $OutPut_File).length/1KB
$OutPut_FileSizeKb = [int][Math]::Ceiling($OutPut_FileSizeKb)
$NumOfRow_Send = 1
$Num_CSV_File_Created++
"{0,-16} {1,-40} {2,-60} {3,-10} {4,-4} {5,-4}" -f $DbServer, $TSQL_File, $OutPut_File, [int]$OutPut_FileSizeKb, $NumOfRow_Send, $Result_MSG
$Results_Array += ,@($Results_Array_Index, $DbServer, $TSQL_File, $OutPut_File, $OutPut_FileSizeKb, $NumOfRow_Send, $Result_MSG)
$Results_Array_Index++
}
#----------------------------------------------------------------------------------------------------------
#--- REMOVE 1st LINE AND '-----' IN 3rd LINE AND FIX THE SPACES BETWEEN COMMA
#----------------------------------------------------------------------------------------------------------
ELSE
{ $NeedFix_Array = Get-Content $OutPut_File
#-------------------------------------------------------------
#--- REMOVE THE FIRST LINE
#-------------------------------------------------------------
$NeedFix_Array = $NeedFix_Array[1..($NeedFix_Array.Length-1)]
#-------------------------------------------------------------
#--- REMOVE THE SECOND LINE AFTER THE FIRST LINE WERE REMOVE
#-------------------------------------------------------------
$NeedFix_Array = $NeedFix_Array[0,0+2..($NeedFix_Array.length - 1)]
$NeedFix_Array = $NeedFix_Array[1..($NeedFix_Array.Length-1)]
#-----------------------------------------------------------
#--- REMOVE BLANK SPACE FROM THE LINE
#-----------------------------------------------------------
$NeedFix_Array = $NeedFix_Array -replace '\s+', ' '
$NeedFix_Array = $NeedFix_Array -replace ' ,', ','
$NeedFix_Array = $NeedFix_Array -replace ', ', ','
#-----------------------------------------------------------
$NeedFix_Array | out-file $OutPut_File -Encoding ASCII
$OutPut_FileSizeKb = (Get-Item $OutPut_File).length/1KB
$OutPut_FileSizeKb = [int][Math]::Ceiling($OutPut_FileSizeKb)
$NumOfRow_Send = $NeedFix_Array.Length
$Num_CSV_File_Created++
"{0,-16} {1,-40} {2,-60} {3,-10} {4,-4} {5,-4}" -f $DbServer, $TSQL_File, $OutPut_File, [int]$OutPut_FileSizeKb, $NumOfRow_Send, $Result_MSG
$Results_Array += ,@($Results_Array_Index, $DbServer, $TSQL_File, $OutPut_File, $OutPut_FileSizeKb, $NumOfRow_Send, $Result_MSG)
$Results_Array_Index++
}
}
}