更新查询中的案例陈述
Case Statement In Update Query
这在 SQL 服务器中有效,但每当我尝试在我的 powershell 环境中 运行 它时,它都会给我一个 d 附近语法不正确的错误。我必须更改什么才能在 powershell 中使用此 运行?
Function Run-Query
{
param([string[]]$queries)
$server='Server'
$dbname='Database'
$connStr='Server={0};Database={1};Integrated Security=SSPI;' -f $server,$dbname
For ($i = 0; $i -lt $queries.count; $i++)
{
$Command = New-Object System.Data.SqlClient.SqlCommand
$conn=New-Object System.Data.SqlClient.SQLConnection($connStr)
$conn.Open()
$Command.Connection = $conn
$Command.CommandText=$queries[$i]
$Command.ExecuteNonQuery()
$conn.Close()
}
}
$updatequery = "UPDATE d
SET [updatedate] = GETDate(),
[shipped] =
(
CASE WHEN NOT EXISTS(Select 1 *
from [server].[db].[dbo].[viewname] vw
WHERE vw.customername = d.customername)
THEN 'Yes'
ELSE
'PreviousCustomer'
END
)
FROM $fulllocation
WHERE ([shipped] IS NULL)
AND ([managerapproved] IS NOT NULL)
AND [readytoship] IN ('Go', 'Ready', 'Send')"
Run-Query -queries $updatequery
将您的查询定义为字符串文字,如下所示:
$updatequery = @"
UPDATE d
SET [updatedate] = GETDate(),
[shipped] =
(
CASE WHEN NOT EXISTS(Select 1
from [server].[db].[dbo].[viewname] vw
WHERE vw.customername = d.customername)
THEN 'Yes'
ELSE
'PreviousCustomer'
END
)
FROM $fulllocation
WHERE ([shipped] IS NULL)
AND ([managerapproved] IS NOT NULL)
AND [readytoship] IN ('Go', 'Ready', 'Send')
"@
这在 SQL 服务器中有效,但每当我尝试在我的 powershell 环境中 运行 它时,它都会给我一个 d 附近语法不正确的错误。我必须更改什么才能在 powershell 中使用此 运行?
Function Run-Query
{
param([string[]]$queries)
$server='Server'
$dbname='Database'
$connStr='Server={0};Database={1};Integrated Security=SSPI;' -f $server,$dbname
For ($i = 0; $i -lt $queries.count; $i++)
{
$Command = New-Object System.Data.SqlClient.SqlCommand
$conn=New-Object System.Data.SqlClient.SQLConnection($connStr)
$conn.Open()
$Command.Connection = $conn
$Command.CommandText=$queries[$i]
$Command.ExecuteNonQuery()
$conn.Close()
}
}
$updatequery = "UPDATE d
SET [updatedate] = GETDate(),
[shipped] =
(
CASE WHEN NOT EXISTS(Select 1 *
from [server].[db].[dbo].[viewname] vw
WHERE vw.customername = d.customername)
THEN 'Yes'
ELSE
'PreviousCustomer'
END
)
FROM $fulllocation
WHERE ([shipped] IS NULL)
AND ([managerapproved] IS NOT NULL)
AND [readytoship] IN ('Go', 'Ready', 'Send')"
Run-Query -queries $updatequery
将您的查询定义为字符串文字,如下所示:
$updatequery = @"
UPDATE d
SET [updatedate] = GETDate(),
[shipped] =
(
CASE WHEN NOT EXISTS(Select 1
from [server].[db].[dbo].[viewname] vw
WHERE vw.customername = d.customername)
THEN 'Yes'
ELSE
'PreviousCustomer'
END
)
FROM $fulllocation
WHERE ([shipped] IS NULL)
AND ([managerapproved] IS NOT NULL)
AND [readytoship] IN ('Go', 'Ready', 'Send')
"@