如何在 SQL Server 2008 中 return 换行
How to return new line in SQL Server 2008
我正在 运行 关注查询 -
SELECT 'SELECT *
FROM LIFE'
我希望预期输出为 -
SELECT *
FROM LIFE
但是,当我在新的window中复制结果集时,它是
SELECT * FROM LIFE
根据下面的答案,当我使用以下
SELECT 'SELECT * ' + char(13) + char(10) + 'FROM LIFE'
输出仍然在一行中以宽space返回如下-
SELECT * FROM LIFE
我不能在结果集中换行吗?对不起,如果我遗漏了什么,但我没有很强的 SQL 技能。
您可以添加回车符 return (ASCII 13) and line feed (ASCII 10),例如:
SELECT 'SELECT * ' + char(13) + char(10) + 'FROM LIFE'
我认为这是可行的,但是当您从结果集中复制时,SSMS 正在替换回车 return。
我只是 运行 下面的 excel-vba 代码针对 SQL Server 2008 R2,并将文本打印成 excel sheet 因为它应该。要进行测试,请打开 excel 并将 test() 例程粘贴到 VBA 编辑器 (alt + f11) 中的 sheet 模块中,然后插入一个新模块(将其命名为 dbMngr),然后然后将下面的 dbMngr 代码粘贴到该模块中。
编辑:您还需要在 dbMngr 模块的顶部包含一个连接字符串常量。
集成安全:
Private Const S_CON As String = "Provider=SQLOLEDB;Data Source=MyServerName;Initial Catalog=MyDB;Integrated Security=SSPI"
SQL 服务器验证:
Private Const S_CON As String = "Provider=SQLOLEDB;Data Source=MyServerName;Initial Catalog=MyDB;UID=MyUserName;PWD=MyPassword"
测试代码:
Sub test()
Dim sSQL As String
sSQL = "SELECT 'LINE A' + Char(10) + Char(13) + 'LINE B'"
Me.Range("A1").Value = sSQL
dbMngr.PasteToRange sSQL, Me.Range("A4")
End Sub
dbMngr 代码:
Public Function GetRS(sSQL As String) As ADODB.Recordset
Dim cn As ADODB.Connection, rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo errCatch:
With cn
.ConnectionString = S_CON
.CursorLocation = adUseClient
.Mode = adModeRead
.Open
End With
With rs
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.ActiveConnection = cn
.Open sSQL
End With
Set rs.ActiveConnection = Nothing
Set GetRS = rs
errCatch:
If Err.Number <> 0 Then
MsgBox "Error in GetRS function"
Debug.Print Err.Number & " " & Err.Description
Stop
Err.Clear
End If
If cn.State = adStateOpen Then
cn.Close
End If
Set rs = Nothing
Set cn = Nothing
End Function
Public Sub PasteToRange(sSQL As String, rngDest As Range)
Dim rs As ADODB.Recordset, x As Integer, orng As Range
rngDest.CurrentRegion.Value = ""
Set rs = GetRS(sSQL)
For x = 0 To rs.Fields.Count - 1
rngDest.Offset(0, x).Value = rs.Fields(x).Name
Next x
rngDest.Offset(1, 0).CopyFromRecordset rs
rngDest.CurrentRegion.Interior.Color = RGB(255, 255, 255)
rngDest.CurrentRegion.Font.Size = 8
rngDest.WrapText = False
If rs.State = adStateOpen Then
rs.Close
End If
Set rs = Nothing
End Sub
我正在 运行 关注查询 -
SELECT 'SELECT *
FROM LIFE'
我希望预期输出为 -
SELECT *
FROM LIFE
但是,当我在新的window中复制结果集时,它是
SELECT * FROM LIFE
根据下面的答案,当我使用以下
SELECT 'SELECT * ' + char(13) + char(10) + 'FROM LIFE'
输出仍然在一行中以宽space返回如下-
SELECT * FROM LIFE
我不能在结果集中换行吗?对不起,如果我遗漏了什么,但我没有很强的 SQL 技能。
您可以添加回车符 return (ASCII 13) and line feed (ASCII 10),例如:
SELECT 'SELECT * ' + char(13) + char(10) + 'FROM LIFE'
我认为这是可行的,但是当您从结果集中复制时,SSMS 正在替换回车 return。
我只是 运行 下面的 excel-vba 代码针对 SQL Server 2008 R2,并将文本打印成 excel sheet 因为它应该。要进行测试,请打开 excel 并将 test() 例程粘贴到 VBA 编辑器 (alt + f11) 中的 sheet 模块中,然后插入一个新模块(将其命名为 dbMngr),然后然后将下面的 dbMngr 代码粘贴到该模块中。
编辑:您还需要在 dbMngr 模块的顶部包含一个连接字符串常量。
集成安全:
Private Const S_CON As String = "Provider=SQLOLEDB;Data Source=MyServerName;Initial Catalog=MyDB;Integrated Security=SSPI"
SQL 服务器验证:
Private Const S_CON As String = "Provider=SQLOLEDB;Data Source=MyServerName;Initial Catalog=MyDB;UID=MyUserName;PWD=MyPassword"
测试代码:
Sub test()
Dim sSQL As String
sSQL = "SELECT 'LINE A' + Char(10) + Char(13) + 'LINE B'"
Me.Range("A1").Value = sSQL
dbMngr.PasteToRange sSQL, Me.Range("A4")
End Sub
dbMngr 代码:
Public Function GetRS(sSQL As String) As ADODB.Recordset
Dim cn As ADODB.Connection, rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo errCatch:
With cn
.ConnectionString = S_CON
.CursorLocation = adUseClient
.Mode = adModeRead
.Open
End With
With rs
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.ActiveConnection = cn
.Open sSQL
End With
Set rs.ActiveConnection = Nothing
Set GetRS = rs
errCatch:
If Err.Number <> 0 Then
MsgBox "Error in GetRS function"
Debug.Print Err.Number & " " & Err.Description
Stop
Err.Clear
End If
If cn.State = adStateOpen Then
cn.Close
End If
Set rs = Nothing
Set cn = Nothing
End Function
Public Sub PasteToRange(sSQL As String, rngDest As Range)
Dim rs As ADODB.Recordset, x As Integer, orng As Range
rngDest.CurrentRegion.Value = ""
Set rs = GetRS(sSQL)
For x = 0 To rs.Fields.Count - 1
rngDest.Offset(0, x).Value = rs.Fields(x).Name
Next x
rngDest.Offset(1, 0).CopyFromRecordset rs
rngDest.CurrentRegion.Interior.Color = RGB(255, 255, 255)
rngDest.CurrentRegion.Font.Size = 8
rngDest.WrapText = False
If rs.State = adStateOpen Then
rs.Close
End If
Set rs = Nothing
End Sub