1

I have a function called BuildQuery that takes a Array of values as Parameters.

Function BuildQuery {
Param($start, [String[]] $KeyFields, [String] $Sch, [String] $TableName)
$Query = "select $KeyFields from '$Sch'.'$TableName'"
}

I want to call the function as :

BuildQuery -start start -KeyFields name, id, age, salary -Sch dbo -TableName Employee 

E.g. : I want to build a query "select name, id, age, salary from dbo.Employee " using the PowerShell function. The only reason I am using function is I want to see it again and again to build queries like this.

1 Answer 1

6

Use the -join operator to expand the array as a comma-separated list:

Function BuildQuery {
    Param($start, [String[]] $KeyFields, [String] $Sch, [String] $TableName)
    $Query = "select $($KeyFields -join ',') from '$Sch'.'$TableName'"
}
Sign up to request clarification or add additional context in comments.

3 Comments

ah, you were just a tick faster :-)
@MartinBrandl you've outranked me for almost 2 weeks, great to be back at full speed :-D
Awesome! Thats exactly what I wanted! . Can't accept the Answer yet because of some time restriction.. So waiting for it to finish :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.