1

I was trying to pass 2 parameters to my stored procedure using powershell.

My code is as below, when I run it in powershell there's no error. However, it doesn't seem the parameters has been passed, because I can't get the correct result. Is there anything wrong on below?

$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

Adding code of 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)

3 Answers 3

7

You must set command type as stored procedure.

$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure

UPDATE

In your code @from and @to isn't parameters. Use code above to create SP with parameters.

CREATE PROCEDURE YQBreport1 @from datetime, @to datetime
AS 
declare  @TypeBigAC char(3)
''''
Sign up to request clarification or add additional context in comments.

6 Comments

I add it and got error says, procedure YQBReport1 has no parameter and arguments were supplied...
I declared the type of from and to but not the value, for some reason system don't like me reply with too many '@', therefore I delete them in the code in front of parameter just for posting purpose: create PROCEDURE YQBreport1 AS declare from datetime, to datetime, TypeBigAC char(3) select TypeBigAC='333'
sounds like the problem is that you didn't make from and to parameters.
@HamletHakobyan, please see my updated post, with the code, it was created as parameters
@Apriljuly You doesn't see any difference between yours and mine code? Look at closer.
|
0

Try this

$commands = $SQLResult | foreach-object -process { $_.output }>> output.ps1

Comments

0

I use something like this:

sqlcmd -E -d Logging -S vansant.Server.local -Q "exec TestPass.ConsolidateMilestoneManualTestRuns @Milestone='$branch'"

Where @Milestone is a param.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.