0

Any one please help me on this one also..I have got many schemas under one database. And in that database I have some SQL Scripts to be executed. My issue is that I should be able to execute those sql scripts in only selected schemas.

That is::Suppose I have got totally 10 schemas(A to J). And I want to execute a script in all schemas except B,G,H (PROMPT THE USER FOR EXCEPTED SCHEMAS).How can I do this??

Thanks in advance..

1 Answer 1

0

It could be something like this: (Add your logic to switch between tables and etc..)

$userInput = read-host "exception"
$exceptArray = $userInput -split ","

foreach($allowedSchema in $allowedArray){
    if($exceptArray -notcontains $allowedSchema){

        $sqlString = "select * from [" + $allowedSchema + "].[tableName]"

        $connection = New-Object System.Data.SQLClient.SQLConnection
        $connection.ConnectionString = "server={serverName}\{instanceName};database={databaseName};trusted_connection={true\false};"
        $connection.Open()

        $cmd = New-Object System.Data.SqlClient.SqlCommand
        $cmd.Connection = $connection
        $cmd.CommandText = $sqlString

        $resultSet = $cmd.ExecuteReader()
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .