2016-06-17 4 views
0

HTML 테이블을 this site에서 XML 파일로 변환했습니다.오류 : 입력 쿼리가 너무 깁니다.

XML 파일에서 데이터베이스 테이블로 데이터를 복사하기 위해 PowerShell에서 SQL 쿼리를 실행하려고합니다. 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() 

하지만 난 그냥 마이크로 소프트의 테크넷 갤러리 https://gallery.technet.microsoft.com/scriptcenter/Perform-ExecuteNonQuery-a05eb40a

을 볼 수 있습니다이 스크립트에서 잡아 않았다 다음과 같이 그것의

기본은 있습니다

관련 문제