将一个表中的自动递增列添加到另一表中 [英] Addding an auto increment column in one table to another table

查看:115
本文介绍了将一个表中的自动递增列添加到另一表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用VS2008和sqlserver 2008

我有两个表user和user_events. users表具有自动递增的user_id.我想检索该user_id并将其插入到user_events表中.任何人都可以引导我进行此操作.在此先感谢

Hi all,

I am using VS2008 and sqlserver 2008

I have two tables users and user_events . users table has a auto increment user_id .I want to retrieve that user_id and insert into user_events table.can anyone guide me through this . thanks in advance

推荐答案

INSERT INTO Table2 (user_id)
VALUES (SELECT user_id from Table1 WHERE <condition> )



更多信息在这里
http://www.w3schools.com/sql/sql_select_into.asp [ http://stackoverflow.com/questions/4823880/sql-server-select-into-variable [ ^ ]



More info here
http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/[^]
http://www.w3schools.com/sql/sql_select_into.asp[^]
http://stackoverflow.com/questions/4823880/sql-server-select-into-variable[^]


您可以使用SCOPE_IDENTITY()返回自动增量值.
You can use SCOPE_IDENTITY() for returning auto increment value.


这篇关于将一个表中的自动递增列添加到另一表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆