在此上下文中不允许使用子查询 -- SQL statement from 2008 to 2005
Subqueries are not allowed in this context -- SQL statement from 2008 to 2005
我有一个 SQL 语句,它在 window 表单应用程序中 运行。
我正在使用 SQL server 2008 进行开发和测试。这里一切都好
SqlCommand command1 = new SqlCommand("insert into payment_detail ( " +
"transaction_id, enrolment_id, hkid_1, section_r, " +
"classes, section_fee, assist_amt, additional_assist_amt, " +
"discount_amt, coupon_amt, coupon_amt_no_number, " +
"paid_amt, last_updated_by, last_updated_date) values " +
"(@transaction_id, @enrolment_id, @hkid_1, @section_r, " +
"@classes, @section_fee, (select section_fee - adjusted_section_fee from coursenew where coursecode = @courseCode and section_r = @section_r), @additional_assist_amt, " +
"@discount_amt, @coupon_amt, @coupon_amt_no_number, " +
"@paid_amt, @last_updated_by, GETDATE())"
, myConnection);
但是当移动到另一个使用 SQL 服务器 2005 的工作站时。
它会提示如下错误
在此上下文中不允许使用子查询。只允许标量表达式。
很抱歉,由于一台电脑只能安装一个版本的SQL服务器。
我无法使用 SQL 服务器 2005 测试脚本。
请帮忙,谢谢。
完整的sql声明将在下面列出
insert into payment_detail ( transaction_id, enrolment_id, hkid_1, section_r, classes, section_fee, assist_amt, additional_assist_amt, discount_amt, coupon_amt, coupon_amt_no_number, paid_amt, last_updated_by, last_updated_date) values (@transaction_id, @enrolment_id, @hkid_1, @section_r, @classes, @section_fee, (select section_fee - adjusted_section_fee from coursenew where coursecode = @courseCode and section_r = @section_r), @additional_assist_amt, @discount_amt, @coupon_amt, @coupon_amt_no_number, @paid_amt, @last_updated_by, GETDATE())"
解决此问题的最简单方法是在插入 运行 之前将 select 语句放入局部变量中,然后使用临时 table 代替子查询.
https://msdn.microsoft.com/en-us/library/ms188927.aspx
编辑:select进入局部变量的具体示例。
https://msdn.microsoft.com/en-us/library/ms187330.aspx
您可以将其转换为使用子查询从 select 而不是 uf 插入的查询:
insert into payment_detail (
transaction_id, enrolment_id, hkid_1, section_r, classes, section_fee,
assist_amt,
additional_assist_amt, discount_amt, coupon_amt, coupon_amt_no_number,
paid_amt, last_updated_by, last_updated_date
)
select
@transaction_id, @enrolment_id, @hkid_1, @section_r, @classes, @section_fee,
section_fee - adjusted_section_fee,
@additional_assist_amt, @discount_amt, @coupon_amt, @coupon_amt_no_number,
@paid_amt, @last_updated_by, GETDATE()
from
coursenew
where
coursecode = @courseCode and section_r = @section_r
那是因为您的子查询 return 多于 一个 标量值。
与SQLServer版本无关,属于逻辑问题
写成下面的样子:
select TOP 1 section_fee - adjusted_section_fee from coursenew where coursecode = @courseCode and section_r = @section_r
我有一个 SQL 语句,它在 window 表单应用程序中 运行。 我正在使用 SQL server 2008 进行开发和测试。这里一切都好
SqlCommand command1 = new SqlCommand("insert into payment_detail ( " +
"transaction_id, enrolment_id, hkid_1, section_r, " +
"classes, section_fee, assist_amt, additional_assist_amt, " +
"discount_amt, coupon_amt, coupon_amt_no_number, " +
"paid_amt, last_updated_by, last_updated_date) values " +
"(@transaction_id, @enrolment_id, @hkid_1, @section_r, " +
"@classes, @section_fee, (select section_fee - adjusted_section_fee from coursenew where coursecode = @courseCode and section_r = @section_r), @additional_assist_amt, " +
"@discount_amt, @coupon_amt, @coupon_amt_no_number, " +
"@paid_amt, @last_updated_by, GETDATE())"
, myConnection);
但是当移动到另一个使用 SQL 服务器 2005 的工作站时。 它会提示如下错误
在此上下文中不允许使用子查询。只允许标量表达式。
很抱歉,由于一台电脑只能安装一个版本的SQL服务器。 我无法使用 SQL 服务器 2005 测试脚本。
请帮忙,谢谢。
完整的sql声明将在下面列出
insert into payment_detail ( transaction_id, enrolment_id, hkid_1, section_r, classes, section_fee, assist_amt, additional_assist_amt, discount_amt, coupon_amt, coupon_amt_no_number, paid_amt, last_updated_by, last_updated_date) values (@transaction_id, @enrolment_id, @hkid_1, @section_r, @classes, @section_fee, (select section_fee - adjusted_section_fee from coursenew where coursecode = @courseCode and section_r = @section_r), @additional_assist_amt, @discount_amt, @coupon_amt, @coupon_amt_no_number, @paid_amt, @last_updated_by, GETDATE())"
解决此问题的最简单方法是在插入 运行 之前将 select 语句放入局部变量中,然后使用临时 table 代替子查询.
https://msdn.microsoft.com/en-us/library/ms188927.aspx
编辑:select进入局部变量的具体示例。 https://msdn.microsoft.com/en-us/library/ms187330.aspx
您可以将其转换为使用子查询从 select 而不是 uf 插入的查询:
insert into payment_detail (
transaction_id, enrolment_id, hkid_1, section_r, classes, section_fee,
assist_amt,
additional_assist_amt, discount_amt, coupon_amt, coupon_amt_no_number,
paid_amt, last_updated_by, last_updated_date
)
select
@transaction_id, @enrolment_id, @hkid_1, @section_r, @classes, @section_fee,
section_fee - adjusted_section_fee,
@additional_assist_amt, @discount_amt, @coupon_amt, @coupon_amt_no_number,
@paid_amt, @last_updated_by, GETDATE()
from
coursenew
where
coursecode = @courseCode and section_r = @section_r
那是因为您的子查询 return 多于 一个 标量值。
与SQLServer版本无关,属于逻辑问题
写成下面的样子:
select TOP 1 section_fee - adjusted_section_fee from coursenew where coursecode = @courseCode and section_r = @section_r