2016-06-17 11 views
0

this siteからXMLファイルにHTMLテーブルを変換しました。エラー:入力クエリが長すぎます

私はPowerShellでSQLクエリを実行して、XMLファイルからデータベーステーブルにデータをコピーしようとしています。 SSMS内でクエリを実行すると、正常に動作します。私はPowerShellで次のコードを実行しようとすると、しかし、私が取得:

Error: input query is too long

[string] $dbCommand = 
@" 
Truncate table DB_NAME.dbo.SQL_LIFE_CYCLE_UPLOAD_IC 

DECLARE @Data XML 
SELECT @Data = BulkColumn 
FROM OPENROWSET(BULK 'D:\Powershell\Temp\SQL_Life_Cycle.XML', SINGLE_BLOB) AS x 
INSERT INTO DB_NAME.dbo.SQL_LIFE_CYCLE_UPLOAD_IC 
(PRODUCT_RELEASED,LIFECYCLE_START_DATE,MAINSTREAM_SUPPORT_END_DATE,EXTENDED_SUPPORT_END_DATE,SERVICE_PACK__SUPPORT_END_DATE,NOTES) 
Select max(case when col=1 then value else '' end) as PRODUCT_RELEASED, 
     max(case when col=2 then value else '' end) as LIFECYCLE_START_DATE, 
     max(case when col=3 then value else '' end) as MAINSTREAM_SUPPORT_END_DATE, 
     max(case when col=4 then value else '' end) as EXTENDED_SUPPORT_END_DATE, 
     max(case when col=5 then value else '' end) as SERVICE_PACK__SUPPORT_END_DATE, 
     max(case when col=6 then value else '' end) as NOTES 
    from  
    (SELECT 
     x.y.value('Col[1]', 'int') AS [Col], 
     x.y.value('Row[1]', 'int') AS [Row], 
     x.y.value('Value[1]', 'VARCHAR(200)') AS [Value] 
     FROM @data .nodes('//DocumentElement/TableData') AS x (y) 
) rawTableData 
group by row 
having row >0 
order by row 
"@ 

OSQL.EXE -E -Q $dbCommand 

それが動作します。このスクリプトを書き換える方法上の任意の提案?

答えて

1

OSQL.exeを使用しており、command line parameterとして渡しているので、長すぎると想定しています。あなたがpowershellを使用しているのを見て、私は組み込みの.net機能を使い、そのようにしてクエリを実行します。詳細情報が必要な場合は、.net SQLExecuteNonQueryをインターネットで検索すると、多くの結果が得られます。

# Instantiate new SqlConnection object. 
$Connection = New-Object System.Data.SQLClient.SQLConnection 

# Set the SqlConnection object's connection string to the passed value. 
$Connection.ConnectionString = "place a connection string here" 

# Open the connection to the database. 
$Connection.Open() 

# Instantiate a SqlCommand object. 
$Command = New-Object System.Data.SQLClient.SQLCommand 

# Set the SqlCommand's connection to the SqlConnection object above. 
$Command.Connection = $Connection 

# Set the SqlCommand's command text to the query value passed in. 
# this is where you pass the query string you wrote to 
$Command.CommandText = $dbCommand 

# Execute the command against the database without returning results (NonQuery). 
$Command.ExecuteNonQuery() 

# Close the currently open connection. 
$Connection.Close() 

私はこのコードを数回書かれているが、私はちょうど、MicrosoftのTechNetのギャラリーにhttps://gallery.technet.microsoft.com/scriptcenter/Perform-ExecuteNonQuery-a05eb40a

提供されています。このスクリプトからそれをつかむんでした:次のように

ことの基本は、

関連する問題