Powershell SQL Query在SQL中有效,但在Powershell中无效

时间:2017-05-30 14:10:10

标签: sql-server powershell

我在SQL中编写了这个查询,并尝试将其粘贴到Powershell上以运行命令,但是,我收到了错误:

异常调用" ExecuteReader"用" 0"参数:"字符串或二进制数据将被截断。

Function CreateCall ($accountno)
{

Write-Verbose "Loading"

write-host $accountno

    $server = 'OMIT'
    $database = 'OMIT'
    $user = 'OMIT'
    $pwd = 'OMIT'
    $connectionString = “Server=$server;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
    $connection = New-Object System.Data.SqlClient.SqlConnection
    $connection.ConnectionString = $connectionString

    $connection.Open()

    $DBNull = [System.DBNull]::Value 
    $date = Get-Date -f "yyyy-MM-dd hh:mm:ss"

    $query3 = "INSERT INTO TABLEC (call_number, callstatus, callcode, calltype, debtor_no, account_no, occupant_code, createdon, createdby, priority, assignto,
dept_route, scheduledon, completedon, completedby, systemcomplete, note, order_no, released, ordercode, requiredon, callername, phone, postbillto, pseudo, printed,
workorder, task_, asset, reference, linktocall, email)
SELECT call_number = (select lastserial+1 from TABLEB where tablename = 'TABLEB'), 
callstatus='S', callcode='ADMIN', calltype='SERORD', debtor_no, account_no, occupant_code, createdon='$date', createdby='autotool', priority=NULL, assignto=112,
dept_route=NULL, scheduledon='$date', completedon=NULL, completedby=NULL, systemcomplete=NULL, note='Variance Pap Call Out', order_no=(select lastjournal+1 FROM TABLEA where source = 'SO'), 
released=0, ordercode='SERORD', requiredon='$date', callername=name, phone=home_phone, postbillto=1, pseudo=NULL, printed=0, workorder=NULL, task_=NULL, asset=NULL, reference=NULL, linktocall=NULL, email=NULL
FROM TABLED
where account_no = '$accountno'"

    $command = $connection.CreateCommand()

        $command.CommandText = $query3

        $result = $command.ExecuteReader()

        $connection.Close()

        Write-Verbose "Done one"
}

我尝试将NULL更改为$ DBNULL,其中= [System.DBNull] :: Value

有些NULL需要''有些不是,取决于DateTime还是INT值?

感谢。

0 个答案:

没有答案