使用子查询插入以将多行插入数据库
Insert Into with subquery to insert multiple rows into database
我目前正在努力生成 'notifications' 查询以使其正常工作。
我正在处理一个查询,该查询将向 table 添加一个字符串,其中将一次添加多行,在此 'bulk insert' 中只会更改其中的一列:
我目前有:
public static void addNotification(string notification)
{
SqlConnection scon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
using (SqlCommand scmd = new SqlCommand())
{
scmd.Connection = scon;
scmd.CommandType = CommandType.Text;
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) VALUES ((SELECT Email FROM AspNetUsers), '" + notification + "' ,0)";
scon.Open();
int status = scmd.ExecuteNonQuery();
scon.Close();
}
}
但是,在传递一个类似这样的字符串时:"this is a notification",它会出现以下错误:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression. The statement has been terminated.
那么,有人可以解释一下这个查询 应该如何 运行 吗?
table 包含以下列:
我知道 'PK1' 不是最好的做法,但这只是 table 用于测试目的(它设置为自动递增)
所以,isAcknowledged 应该设置为 0,通知应该是这个字符串,电子邮件应该从 table AspNetUsers?
的每一行中选择
谁能向我解释我应该如何将这些数据添加到我的数据库中?
像这样的东西应该可以工作:
scmd.CommandText = "INSERT INTO notificationAck (Email,
Notification,
isAcknowledged)
SELECT Email, '" + notification + "', 0
FROM AspNetUsers";
INSERT INTO SELECT
不需要VALUES
。因此,您只需使用 SELECT
语句从 AspNetUsers
中检索 Email
字段加上两个 constant 值。
您不想在使用查询生成结果时使用 VALUES
。您可以将 INSERT
查询更改为:
INSERT INTO notificationAck (Email, Notification, isAcknowledged)
SELECT Email, 'SOME STRING', 0 FROM AspNetUsers
翻译成您的代码如下所示:
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged)
SELECT Email, '" + notification + "', 0 FROM AspNetUsers";
您的查询
SELECT Email FROM AspNetUsers
returns 如您的错误所述,不止一行。您需要更改它,使其 returns 只有一行,例如:
SELECT TOP 1 Email FROM AspNetUsers
然后如下更改命令
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) SELECT TOP 1 Email " + notification + "' ,0 FROM AspNetUsers'";
如果要插入多行,请从查询中删除前 1 行。
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) SELECT Email " + notification + "' ,0 FROM AspNetUsers'";
Select Top 1
in Sub Query if you want to insert only 1 row
尝试
INSERT INTO notificationAck (Email, Notification, isAcknowledged)
VALUES ((SELECT top 1 Email FROM AspNetUsers), '" + notification + "' ,0)
if you want to insert all rows from AspNetUsers then use
INSERT INTO notificationAck(Email,Notification, isAcknowledged) select
Email,''+notification +"',1 from AspNetUsers
我目前正在努力生成 'notifications' 查询以使其正常工作。
我正在处理一个查询,该查询将向 table 添加一个字符串,其中将一次添加多行,在此 'bulk insert' 中只会更改其中的一列:
我目前有:
public static void addNotification(string notification)
{
SqlConnection scon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
using (SqlCommand scmd = new SqlCommand())
{
scmd.Connection = scon;
scmd.CommandType = CommandType.Text;
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) VALUES ((SELECT Email FROM AspNetUsers), '" + notification + "' ,0)";
scon.Open();
int status = scmd.ExecuteNonQuery();
scon.Close();
}
}
但是,在传递一个类似这样的字符串时:"this is a notification",它会出现以下错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
那么,有人可以解释一下这个查询 应该如何 运行 吗?
table 包含以下列:
我知道 'PK1' 不是最好的做法,但这只是 table 用于测试目的(它设置为自动递增)
所以,isAcknowledged 应该设置为 0,通知应该是这个字符串,电子邮件应该从 table AspNetUsers?
的每一行中选择谁能向我解释我应该如何将这些数据添加到我的数据库中?
像这样的东西应该可以工作:
scmd.CommandText = "INSERT INTO notificationAck (Email,
Notification,
isAcknowledged)
SELECT Email, '" + notification + "', 0
FROM AspNetUsers";
INSERT INTO SELECT
不需要VALUES
。因此,您只需使用 SELECT
语句从 AspNetUsers
中检索 Email
字段加上两个 constant 值。
您不想在使用查询生成结果时使用 VALUES
。您可以将 INSERT
查询更改为:
INSERT INTO notificationAck (Email, Notification, isAcknowledged)
SELECT Email, 'SOME STRING', 0 FROM AspNetUsers
翻译成您的代码如下所示:
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged)
SELECT Email, '" + notification + "', 0 FROM AspNetUsers";
您的查询
SELECT Email FROM AspNetUsers
returns 如您的错误所述,不止一行。您需要更改它,使其 returns 只有一行,例如:
SELECT TOP 1 Email FROM AspNetUsers
然后如下更改命令
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) SELECT TOP 1 Email " + notification + "' ,0 FROM AspNetUsers'";
如果要插入多行,请从查询中删除前 1 行。
scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) SELECT Email " + notification + "' ,0 FROM AspNetUsers'";
Select
Top 1
in Sub Query if you want to insert only 1 row
尝试
INSERT INTO notificationAck (Email, Notification, isAcknowledged)
VALUES ((SELECT top 1 Email FROM AspNetUsers), '" + notification + "' ,0)
if you want to insert all rows from AspNetUsers then use
INSERT INTO notificationAck(Email,Notification, isAcknowledged) select
Email,''+notification +"',1 from AspNetUsers