c# - 如何在插入语句中使用子查询

标签 c# sql

我已经尝试过,但出现错误:


  在此上下文消息中不允许出现SubQuery。


我有两个表ProductCategory,并且要基于categoryId使用CategoryName

查询是

Insert into Product(Product_Name,Product_Model,Price,Category_id) 
values(' P1','M1' , 100, (select CategoryID from Category where Category_Name=Laptop))


请告诉我一个带有代码的解决方案。

最佳答案

这样尝试

Insert into Product
(
Product_Name,
Product_Model,
Price,Category_id
)
Select 
'P1',
'M1' , 
100, 
CategoryID 
From 
Category 
where Category_Name='Laptop'

关于c# - 如何在插入语句中使用子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20771820/

相关文章:

C# dot net core 单实例应用程序将参数传递给第一个实例

c# - 需要解决 Microsoft.QualityTools.Testing.Fakes

c# - 将 WindowsMediaPlayer 设置为自动运行 C# Windows 窗体

c# - NUnit - ExpectedMessage 不同错误

c# - 将 2 个列表合并到一个新列表中,该列表包含列表 1 和列表 2 中的数据 - 其中唯一键为 "VariantId"

mysql - 简化UPDATE语句中的多个子查询

c# - 数据为空。不能对 Null 值调用此方法或属性

php - 如何查询列名中带句点的Excel工作表

php - 如何组合两个具有不同 ORDER BY 子句的 SQL 查询

sql - SCD 日期对的最佳实践(关闭/打开时间戳)