如何从变量 Select ?
How to Select from a variable?
我执行 DMV 查询的辅助函数:
Function DMV_Query($DMV_Query) {
## Prepare the connection string based on information provided
$connectionString = "Provider=msolap;Data Source=$AS_Server;Initial Catalog=$CUBE;"
## Connect to the data source and open
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand
$command.Connection = $connection
$command.CommandText = $DMV_Query
$connection.Open()
## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter #$command
$adapter.SelectCommand = $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
## Return all of the rows from their query
$dataSet.Tables[0]
}
示例调用:
$dmvResult = DMV_Query 'SELECT [SESSION_ID], [SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS'
$dmvResult
存储在 $dmvResult
中的内容示例:
PS> $dmvResult | ConvertTo-Csv
"SESSION_ID","SESSION_SPID"
"D0kl8975r6df65","5567"
"fghu5r67f65","2234"
我想 select 来自 $dmvResult
变量的所有列并将它们插入 SQL table。这是我可以 select 来自变量的方式吗?
# Doesn't work.
INSERT INTO [dbo].[Table]
SELECT * FROM @dmvResult
使用如下查询,不要将值存储在变量中..
INSERT INTO [dbo].[Table]
SELECT [SESSION_ID]
,[SESSION_SPID]`enter code here`
FROM $SYSTEM.DISCOVER_SESSIONS
注意:作为 PowerShell 中的高级字符串插值 的练习,下面的答案可能很有趣,但鉴于 $dmvResult
包含现有 [= Cataster themselves: see this post.
发现,使用 批量复制操作 从先前的查询返回的对象 13=] 更简单且效率更高
您似乎正试图通过在 PowerShell 中构建一个包含先前查询结果 $dmvResult
.
的字符串来创建 T-SQL 语句
由于 T-SQL 对 PowerShell 变量一无所知,您需要 明确合并所有值 以传递给您的 INSERT INTO
语句中的语句 string.
这是一个基于您的示例数据的示例,使用 PowerShell 强大的可扩展字符串(字符串插值;有关背景知识,请参阅this answer):
# Sample query result / imported CSV.
$dmvResult =
[pscustomobject] @{ SESSION_ID = 'D0kl8975r6df65'; SESSION_SPID = 5567 },
[pscustomobject] @{ SESSION_ID = 'fghu5r67f65'; SESSION_SPID = 2234 }
# Construct the INSERT INTO statement from $dmvResult, using an expandable
# string ("..." with $variable and $(<command> references))
$insertStatement = @"
INSERT INTO [dbo].[Table]
($($dmvResult[0].psobject.properties.name -join ', '))
VALUES
$(
@(foreach ($obj in $dmvResult) {
$values = foreach ($value in $obj.psobject.properties.value) {
if ($value -is [string]) { "`"$value`"" } else { $value }
}
'(' + ($values -join ', ') + ')'
}) -join ",`n "
)
"@
# Output the constructed statement string.
$insertStatement
请注意,属性 值的字符串化可能需要更加复杂,具体取决于您的数据;上面的代码仅区分字符串和非字符串:前者用 "..."
进行字符串化,后者按原样
以上结果:
INSERT INTO [dbo].[Table]
(SESSION_ID, SESSION_SPID)
VALUES
("D0kl8975r6df65", 5567),
("fghu5r67f65", 2234)
我找到了解决办法,BulkCopy!
$dmvResult = DMV_Query 'SELECT [SESSION_ID]
,[SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS';
$ConnectionString ='Data Source=$server; Database=$database; Trusted_Connection=True;'
$bulkCopy = new-object Data.SqlClient.SqlBulkCopy($ConnectionString)
$bulkCopy.DestinationTableName=$Table
$bulkCopy.WriteToServer($dmvResult)
然而,如果列顺序不正确,这将无法正确映射列,因为 BulkCopy 按位置而不是按名称匹配进行复制
我执行 DMV 查询的辅助函数:
Function DMV_Query($DMV_Query) {
## Prepare the connection string based on information provided
$connectionString = "Provider=msolap;Data Source=$AS_Server;Initial Catalog=$CUBE;"
## Connect to the data source and open
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand
$command.Connection = $connection
$command.CommandText = $DMV_Query
$connection.Open()
## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter #$command
$adapter.SelectCommand = $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
## Return all of the rows from their query
$dataSet.Tables[0]
}
示例调用:
$dmvResult = DMV_Query 'SELECT [SESSION_ID], [SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS'
$dmvResult
存储在 $dmvResult
中的内容示例:
PS> $dmvResult | ConvertTo-Csv
"SESSION_ID","SESSION_SPID"
"D0kl8975r6df65","5567"
"fghu5r67f65","2234"
我想 select 来自 $dmvResult
变量的所有列并将它们插入 SQL table。这是我可以 select 来自变量的方式吗?
# Doesn't work.
INSERT INTO [dbo].[Table]
SELECT * FROM @dmvResult
使用如下查询,不要将值存储在变量中..
INSERT INTO [dbo].[Table]
SELECT [SESSION_ID]
,[SESSION_SPID]`enter code here`
FROM $SYSTEM.DISCOVER_SESSIONS
注意:作为 PowerShell 中的高级字符串插值 的练习,下面的答案可能很有趣,但鉴于 $dmvResult
包含现有 [= Cataster themselves: see this post.
您似乎正试图通过在 PowerShell 中构建一个包含先前查询结果 $dmvResult
.
由于 T-SQL 对 PowerShell 变量一无所知,您需要 明确合并所有值 以传递给您的 INSERT INTO
语句中的语句 string.
这是一个基于您的示例数据的示例,使用 PowerShell 强大的可扩展字符串(字符串插值;有关背景知识,请参阅this answer):
# Sample query result / imported CSV.
$dmvResult =
[pscustomobject] @{ SESSION_ID = 'D0kl8975r6df65'; SESSION_SPID = 5567 },
[pscustomobject] @{ SESSION_ID = 'fghu5r67f65'; SESSION_SPID = 2234 }
# Construct the INSERT INTO statement from $dmvResult, using an expandable
# string ("..." with $variable and $(<command> references))
$insertStatement = @"
INSERT INTO [dbo].[Table]
($($dmvResult[0].psobject.properties.name -join ', '))
VALUES
$(
@(foreach ($obj in $dmvResult) {
$values = foreach ($value in $obj.psobject.properties.value) {
if ($value -is [string]) { "`"$value`"" } else { $value }
}
'(' + ($values -join ', ') + ')'
}) -join ",`n "
)
"@
# Output the constructed statement string.
$insertStatement
请注意,属性 值的字符串化可能需要更加复杂,具体取决于您的数据;上面的代码仅区分字符串和非字符串:前者用 "..."
进行字符串化,后者按原样
以上结果:
INSERT INTO [dbo].[Table]
(SESSION_ID, SESSION_SPID)
VALUES
("D0kl8975r6df65", 5567),
("fghu5r67f65", 2234)
我找到了解决办法,BulkCopy!
$dmvResult = DMV_Query 'SELECT [SESSION_ID]
,[SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS';
$ConnectionString ='Data Source=$server; Database=$database; Trusted_Connection=True;'
$bulkCopy = new-object Data.SqlClient.SqlBulkCopy($ConnectionString)
$bulkCopy.DestinationTableName=$Table
$bulkCopy.WriteToServer($dmvResult)
然而,如果列顺序不正确,这将无法正确映射列,因为 BulkCopy 按位置而不是按名称匹配进行复制