Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向sql表插入大量值?_Sql_Sql Server_Tsql - Fatal编程技术网

如何向sql表插入大量值?

如何向sql表插入大量值?,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想从下面的另一个数据中添加大量数据。但我不能这样做:返回错误。但这些领域与其他领域是一样的 declare @hrmtable1 table(musterino int, ekno smallint) insert into @hrmtable1 (musterino , ekno) select distinct musterino, ekno from hareketmuhasebe (nolock) where islemtarihi >= '20120

我想从下面的另一个数据中添加大量数据。但我不能这样做:返回错误。但这些领域与其他领域是一样的

declare @hrmtable1 table(musterino int, ekno smallint)

insert into @hrmtable1 (musterino , ekno)
    select distinct musterino, ekno
    from hareketmuhasebe (nolock) 
    where islemtarihi >= '20120101'
      and isnull(musterino, 0) <> 0 
      and isnull(musterino, 0) > 9000000 
      and isnull(ekno,0) <> 0 

insert into table1(A,B,C,D,E,. . . . .N) 
   SELECT DISTINCT 
      case when ((select count(*) from table1 where musterino=e.musterino) > 0)
           then (select top 1 *
                 from dbo.table1 
                 where musterino = e.musterino  
                 order by ekno desc)
           else 
                (select 100, e.musterino, e.ekno, 0, K, L, M)
                 from @hrmtable1 e )
      end
错误:

Msg 120,15级,状态1,第10行 INSERT语句的select列表包含的项目少于INSERT列表。 SELECT值的数量必须与INSERT列的数量匹配

编辑:由于您编辑了问题,答案仍然保持不变。Select语句中的列数与代码第二部分中的insert语句列数不匹配

在第一个语句中,您指定了2列,在select中,您有5列,它们应该匹配,这正是错误告诉您的

insert into @hrmtable1 (musterino , ekno)
                         ^^^^^^^    ^^^^
select distinct musterino,ekno,defterid,dovizcinsi, subekodu
                ^^^^^^^    ^^^   ^^^^     ^^^^^      ^^^^^

从insert语句看,似乎不需要Select语句中的最后三列

如错误中所述,insert中的列数与您提供的值数不一致

insert into @hrmtable1 (musterino , ekno)
select distinct musterino,ekno
from hareketmuhasebe (nolock) 
where islemtarihi >= '20120101'
and isnull(musterino,0) <> 0 and isnull(musterino,0) < 9000000 and isnull(ekno,0) <> 0 
试试这个:

insert into @hrmtable1 (musterino , ekno)
select distinct musterino,ekno
from hareketmuhasebe (nolock) 
where islemtarihi >= '20120101'
and isnull(musterino,0) <> 0 and isnull(musterino,0) < 9000000 and isnull(ekno,0) <> 0
insert子句中有2列,select子句中有5列,这就是错误的原因

INSERT语句的select列表包含的项目少于 插入列表。SELECT值的数量必须与 插入列

错误本身告诉你你做错了什么。insert语句中的列数少于select语句中的列数

来自MSDN

子查询的select列表必须与 插入语句。如果未指定列列表,则必须选择列表 匹配要插入的表或视图中的列


阅读更多关于

的信息,在同一页中检查相同的内容query@programmerist,然后确保查询select top 1*from dbo.table1返回的列数与insert语句或其他select语句相同