求sql怎么一次用insert 添加多条数据

zhengllei123 2012-11-19 11:05:18
INSERT INTO BillInfo
(BillID
,BID
,IWtime
,Storage)
VALUES
('1',1,'2012-11-19',100)
,('2',1,'2012-11-19',100)
,('3',1,'2012-11-19',100)
,('4',1,'2012-11-19',100)
我试了下上面的方法不能用,请问还有什么方法可以么?
我用的sqlserver 2005的数据库
...全文
157660 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
winbobob 2014-03-25
  • 打赏
  • 举报
回复
可以用数组的方法

private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, CommandType cmdType, string cmdText, IDataParameter[] cmdParms)
    {
        if (conn.State != ConnectionState.Open)
        {
            conn.Open();
        }
        cmd.Connection = conn;
        cmd.CommandText = cmdText;
        cmd.CommandType = cmdType;
        if (cmdParms != null)
        {
            foreach (SqlParameter parameter in cmdParms)
            {
                if (parameter != null)
                {
                    if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
        }
这样就能把String字符串里的sql语句一次性都执行玩
PacoNiu 2013-11-18
  • 打赏
  • 举报
回复
晕,知道为什么了,是我在selset那加了括号,取消之后就可以执行了,代码如下
INSERT INTO table1 (mc,zb_mc,xx_mc,leibie)
SELECT 'aa',NULL,NULL,1390 union
SELECT 'bb',NULL,NULL,1400 union
SELECT 'cc',NULL,NULL,1410 union
SELECT 'dd',NULL,NULL,1420 union
SELECT 'ee',NULL,NULL,1430 union
SELECT 'ff',NULL,NULL,1440 
PacoNiu 2013-11-18
  • 打赏
  • 举报
回复
INSERT INTO table1 (mc,zb_mc,xx_mc,leibie)
SELECT('aa',NULL,NULL,1390)union
SELECT('bb',NULL,NULL,1400)union
SELECT('cc',NULL,NULL,1410)union
SELECT('dd',NULL,NULL,1420)union
SELECT('ee',NULL,NULL,1430)union
SELECT('ff',NULL,NULL,1440)
为什么执行这条语句的时候提示我多条错误:消息 102,级别 15,状态 1,第 2 行 ',' 附近有语法错误。
huantortoise 2012-11-19
  • 打赏
  • 举报
回复
引用 9 楼 zhengllei123 的回复:
引用 4 楼 ssp2009 的回复:INSERT INTO BillInfo (BillID ,BID ,IWtime ,Storage) select '1',1,'2012-11-19',100 union all select '2',1,'2012-11-19',100 ……
union all 是关键字,你可以这样理解:这种用法你应该熟悉把,从B表中选一些数据插入到A表,就这样写 insert into A (column1,column2) select column1,column2 from B。上面那个语句中的select '1',1,'2012-11-19',100 union all select '2',1,'2012-11-19',100 union all select '…… 你就当作是 select column1,column2 from B。就是说把你要出入的那些数据作为一个集合一次性插入到BillInfo这个表中。至于union all的作用和用法这里就不说了,网上一堆。
全栈极简 2012-11-19
  • 打赏
  • 举报
回复
union all 是sql内置关键字,表示合并结果集。 多条数据合并为一个结果集之后,再插入。
zhengllei123 2012-11-19
  • 打赏
  • 举报
回复
引用 4 楼 ssp2009 的回复:
INSERT INTO BillInfo (BillID ,BID ,IWtime ,Storage) select '1',1,'2012-11-19',100 union all select '2',1,'2012-11-19',100 union all select '……
请问那个'all'是什么意思,数据集么?我该怎么把数据从平台传进来?
  • 打赏
  • 举报
回复
大致如下吧:

declare @i int,@j int
set @i=1
while(@i<=6)
begin
 
   insert into BillInfo(BillID,BID ,IWtime ,Storage)
   values(cast(@j as varchar(20)),1,'2012-11-19',100)
   end
set @i=@i+1
Hauk 2012-11-19
  • 打赏
  • 举报
回复
insert into U_Info values('aaa',20),('aaa',20)恩,这个确实2008才支持
Hauk 2012-11-19
  • 打赏
  • 举报
回复

insert into U_Info values('aaa',20),('aaa',20)--sql 2005 因该支持这种方法的啊

insert into U_Info 
select 'bbb',50 
UNION ALL select 'ccc',60 
UNION ALL select 'ccc',60 
huantortoise 2012-11-19
  • 打赏
  • 举报
回复
你用的这种方式只有在sql server 2008及以上版本的数据库才支持。 2005的话可以用这种方式: INSERT INTO BillInfo (BillID ,BID ,IWtime ,Storage) select '1',1,'2012-11-19',100 union all select '2',1,'2012-11-19',100 union all select '3',1,'2012-11-19',100
快溜 2012-11-19
  • 打赏
  • 举报
回复
INSERT INTO BillInfo (BillID ,BID ,IWtime ,Storage) select '1',1,'2012-11-19',100 union all select '2',1,'2012-11-19',100 union all select '3',1,'2012-11-19',100 union all select '4',1,'2012-11-19',100
熙风 2012-11-19
  • 打赏
  • 举报
回复
存储过程。。。
色拉油 2012-11-19
  • 打赏
  • 举报
回复
bdmh 2012-11-19
  • 打赏
  • 举报
回复
如果是sqlserver支持多sql语句,你可以将所有的insert拼接成字符串,一起发送到服务器 或者你可以批量插入另一个数据集的数据 insert xxx(id,name) select id,name from xxx
zhengllei123 2012-11-19
  • 打赏
  • 举报
回复
引用 4 楼 ssp2009 的回复:
INSERT INTO BillInfo (BillID ,BID ,IWtime ,Storage) select '1',1,'2012-11-19',100 union all select '2',1,'2012-11-19',100 union all select '……
谢谢提点~~
zhengllei123 2012-11-19
  • 打赏
  • 举报
回复
引用 11 楼 huantortoise 的回复:
引用 9 楼 zhengllei123 的回复:引用 4 楼 ssp2009 的回复:INSERT INTO BillInfo (BillID ,BID ,IWtime ,Storage) select '1',1,'2012-11-19',100 union all select……
谢谢讲解~
zhengllei123 2012-11-19
  • 打赏
  • 举报
回复
引用 10 楼 guwei4037 的回复:
union all 是sql内置关键字,表示合并结果集。 多条数据合并为一个结果集之后,再插入。
谢谢讲解~
理不完的逻辑 2012-11-19
  • 打赏
  • 举报
回复
对数据没要求 就循环插入就好了
jwh2004 2012-11-19
  • 打赏
  • 举报
回复
4楼的方法是对的

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧