我试图使用powershell将2个参数传递给我的存储过程。
我的代码如下,当我在powershell中运行时,没有错误。但是,似乎参数似乎没有通过,因为我无法得到正确的结果。下面有什么不对吗?
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=localhost;Database=AMSDataWarehouse Test;Integrated Security=SSPI"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
$sqlcmd.CommandText = "YQBReport1"
$param1=$sqlcmd.Parameters.Add("@from" , [System.Data.SqlDbType]::DateTime)
$param1.Value = "2013-03-13"
$param2=$sqlcmd.Parameters.Add("@to" , [System.Data.SqlDbType]::DateTime)
$param2.Value = "2013-03-14"
$SqlConnection.Open()
$sqlcmd.ExecuteNonQuery()
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$SQLResult =$DataSet.Tables[0]
$commands = $SQLResult | foreach-object -process { $_.output }> output.ps1
.\output.ps1
添加YQBReport1的代码
create PROCEDURE YQBreport1
AS
declare @from datetime, @to datetime, @TypeBigAC char(3)
select @TypeBigAC='333'
Select
'$application = New-Object -ComObject Visio.Application;
$documents = $application.Documents;
$document = $documents.Add("AMSGantt.vst");
$pages = $application.ActiveDocument.Pages;
$page = $pages.Item(1);
$shape500 = $page.DrawLine(2,7.9,11,7.9);
$shape500.TextStyle = "Title";
$shape500.LineStyle = "Title";'
as output
union all
select
'$shape500.text = '+'"'+'Assignation de Barrières-' + DATENAME(WEEKDAY,@from)+','+DATENAME(MONTH,@from)+' '+DATENAME(DAY,@from)+','+DATENAME(YEAR,@from)+'"'+';'
as output
Union all
SELECT
'$shape'+cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+' = $page.DrawRectangle'+'('+CAST(X1 as varchar)+','+CAST(Y1 as varchar)+','+CAST(X2 as varchar)+','+CAST(Y2 as varchar)+')'+';'
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.'+'LineStyle = Gantt"'+';'
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.'+'TextStyle = "Gantt"'+case when left(arrival, 2)='AC' THEN 'Red' ELSE '' END+ CASE WHEN LEFT(Departure, 2)='AC' THEN 'Red' ELSE '' END+';'
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.FillStyle = "Gantt'+CASE WHEN CHARINDEX(@TypeBigAC, [ACType Iata])<>0 THEN ' Big AC' ELSE '' END+'"'+';'
+'$shape'+ cast(ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.Text = "'+case when LEN(arrival)<>0 THEN Arrival ELSE '###' END +' / '+ case when len([ACType Iata])<>0 then [ACType Iata] else ' ' end +' / '+ CASE WHEN LEN(Departure)<>0 THEN Departure ELSE '###' END +'"'+';'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+' = $page.DrawLine('+cast(X1 -0.5 as varchar)+','+cast(Y1 -0.03 as varchar)+','+cast(X1 +0.5 as varchar)+','+cast(Y1 -0.03 as varchar)+ ')'+';'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.Text = "'+CONVERT(char(5), [Allocation Start Datetime], 108)+'"'+';'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.TextStyle = "Times"'+';'
+'$shape'+ cast(100+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.LineStyle = "Times"'+';'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+' = $page.DrawLine('+cast(X2 -0.1 as varchar)+','+cast(Y1 -0.03 as varchar)+','+cast(X2 +0.1 as varchar)+','+cast(Y1 -0.03 as varchar)+ ')'+';'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.Text = "'+CONVERT(char(5), [Allocation End Datetime], 108)+'"'+';'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.TextStyle = "Times"'+';'
+'$shape'+ cast(200+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+'.LineStyle = "Times"'+';'
as output
FROM GanttReport(@from,@to,@TypeBigAC)
union all
select '$shape'+ cast(300+ROW_NUMBER()OVER(ORDER BY (SELECT 0)) as varchar)+' = $page.DrawLine('+cast(X1 as varchar)+','+cast(Y2 as varchar)+','+cast(X2 as varchar)+','+cast(Y1 as varchar)+ ')'
from GanttReportTowingLines(@from,@to,@TypeBigAC)
答案 0 :(得分:7)
您必须将命令类型设置为存储过程。
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
<强>更新强>
在您的代码@from
中,@to
不是参数。使用上面的代码创建带参数的SP。
CREATE PROCEDURE YQBreport1 @from datetime, @to datetime
AS
declare @TypeBigAC char(3)
''''
答案 1 :(得分:0)
试试这个
$commands = $SQLResult | foreach-object -process { $_.output }>> output.ps1
答案 2 :(得分:0)
我使用这样的东西:
sqlcmd -E -d Logging -S vansant.Server.local -Q "exec TestPass.ConsolidateMilestoneManualTestRuns @Milestone='$branch'"
@Milestone是一个参数。