简体   繁体   English

SQL Server存储过程-插入新行

[英]SQL Server stored procedure - inserting new rows

I want to insert records in Table2 from Table1 and that too insert with my new primary key specified.. I have written following stored procedure for that :: 我想从Table1中插入Table1 Table2记录,并且也要插入指定的新主键。

DECLARE @Iterator INT, @tempCount INT, @Nbb_ID INT

SET @Iterator = 0
SET @tempCount = count('SELECT * FROM Table1')

WHILE (@Iterator < @tempCount)
BEGIN
    SET @Nbb_ID =   NEWID()

    INSERT INTO Table2 (Nbb_ID, Nbb_Name)
    VALUES (@Nbb_ID, (SELECT NAME1 FROM Table1 WHERE rownum = @Iterator + 1))

    SET @Iterator = @Iterator + 1
END

I don't understand where am I going wrong ? 我不明白我要去哪里错了?

The INSERT command comes in two flavors: INSERT命令有两种形式:

(1) either you have all your values available, as literals or SQL Server variables - in that case, you can use the INSERT .. VALUES() approach: (1)您可以将所有值都用作文字或SQL Server变量-在这种情况下,可以使用INSERT .. VALUES()方法:

INSERT INTO dbo.YourTable(Col1, Col2, ...., ColN)
VALUES(Value1, Value2, @Variable3, @Variable4, ...., ValueN)

Note: I would recommend to always explicitly specify the list of column to insert data into - that way, you won't have any nasty surprises if suddenly your table has an extra column, or if your tables has an IDENTITY or computed column. 注意:我建议始终明确指定要向其中插入数据的列的列表-这样,如果您的表突然有一个额外的列,或者您的表具有IDENTITY或计算列,您将不会感到意外。 Yes - it's a tiny bit more work - once - but then you have your INSERT statement as solid as it can be and you won't have to constantly fiddle around with it if your table changes. 是的-需要做一点工作- 一次 -但是您将拥有尽可能牢固的INSERT语句,并且如果表发生更改,您将不必不断地摆弄它。

(2) if you don't have all your values as literals and/or variables, but instead you want to rely on another table, multiple tables, or views, to provide the values, then you can use the INSERT ... SELECT ... approach: (2)如果您没有将所有值都用作文字和/或变量,而是想依靠另一个表,多个表或视图来提供值,则可以使用INSERT ... SELECT ...方法:

INSERT INTO dbo.YourTable(Col1, Col2, ...., ColN)
   SELECT
       SourceColumn1, SourceColumn2, @Variable3, @Variable4, ...., SourceColumnN
   FROM
       dbo.YourProvidingTableOrView

Here, you must define exactly as many items in the SELECT as your INSERT expects - and those can be columns from the table(s) (or view(s)), or those can be literals or variables. 在这里,您必须在SELECT定义与INSERT期望一样多的项-这些项可以是表(或视图)中的列,也可以是文字或变量。 Again: explicitly provide the list of columns to insert into - see above. 再次:明确提供要插入的列的列表-参见上文。

You can use one or the other - but you cannot mix the two - you cannot use VALUES(...) and then have a SELECT query in the middle of your list of values - pick one of the two - stick with it. 您可以使用一个或另一个 -但你不能将两者搅和-你不能使用VALUES(...)然后有一个SELECT中值列表的中间查询-挑两个中的一个-坚持下去。

So in your case, you need to change your INSERT statement to: 因此,在您的情况下,您需要将INSERT语句更改为:

INSERT INTO Table2 (Nbb_ID, Nbb_Name)
   SELECT 
      NEWID(), NAME1 
   FROM 
      Table1 

and that should do it - there's absolutely no need to create your own row-by-agonizing-row WHILE loop here.... just use a single, set-based statement and you're done! 并且应该这样做-绝对没有必要在这里创建自己的逐行 WHILE循环...。只需使用一个基于集合的语句就可以了!

I'm not entirely sure what you're trying to achieve, but if you're just wanting to copy the records from Table1 into Table2 but give a new ID to the Table2 record, then you can do this more efficiently by using 我不确定您要实现的目标,但是如果您只是想将记录从Table1复制到Table2中,但给Table2记录提供新的ID,则可以通过使用

insert into Table2 
select NEWID(), NAME1 from Table1

This will evaluate NEWID() for each record that is being taken from Table1, thus giving the Table2 record a different UID. 这将为从Table1中获取的每个记录评估NEWID(),从而为Table2记录提供不同的UID。

The benefit over your method is that this is a set-based approach which is what DBs are best at. 相对于您的方法的好处是,这是基于集合的方法,这是数据库最擅长的。 Your method is RBAR (row-by-agonizing-row) which is not the best use of SQL. 您的方法是RBAR(逐行记录),它不是SQL的最佳用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM