同时在拖表中保存用户的ID. [英] Saving id of a user simalataneously in tow tables.

查看:104
本文介绍了同时在拖表中保存用户的ID.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我不得不在这里讨论一些概念上的问题.情况是这样的:
一位用户访问我的网站并创建一个新的个人资料.创建新的配置文件后,我倾向于将结果保存在datbase viz中的两个表中.

1.个人资料
2.profiles_plugins

配置文件表中的"id"是自生成的,因此将数据存储在配置文件表中没有问题
但同时,表1中的此自生成ID必须存储在表2(profiles_plugins)的profile_id列中.
数据读取器在生成表时无法从表中获取值.

因此,基本上,问题在于如何在用户提交其详细信息的同时从table1提取此自身生成的id并将其保存在表2中.

解决方案

您可以在SQL Server中使用@@ RowCount来获取该ID.但是,除此以外,您可以使用存储过程,然后将值首先插入配置文件表中,然后获取max(profile_id)并将其插入Profile_plugin表中,这可能是一种简单的方法.


<我希望表概要文件"中的自我生成的ID"列为"IDENTITY列".在这种情况下,您可以使用
获取新生成的身份值
@@ Identity :是一个系统函数,它返回最后插入的身份值.

Scope_Identity():返回插入到同一作用域的身份列中的最后一个身份值.范围是一个模块:存储过程,触发器,函数或批处理.

现在,如果您使用相同的过程在两个表中进行广告记录,则可以使用@@ Identity或Scope_identity轻松访问新创建的Identity值.

如果使用2个单独的过程创建新的配置文件并将记录插入profiles_plugins中,则可以使用 OUTPUT参数,它将新生成的标识值返回到调用代码.


Hi,
I had to discuss a bit of conceptual problem here. The scenario is like this:
A user comes on my my website and creates a new profile. As the new profile is created, I tend to save the result in tow tables in datbase viz.

1. profiles
2. profiles_plugins

The ''id'' in profiles table is self-generated and hence there is no problem in storing the data it in the profiles table
But at the same time, this self-generated id in table 1 has to be stored in table 2 (profiles_plugins) in the column profile_id.
Data reader cannot fetch the value from table while it is being generated.

So, basically the problem is how to fetch this self generated id from table1 to be saved in table 2 at the same time when the user submits his details.

解决方案

You can fetch that id using @@RowCount in sql server. But instead of this u can go for the stored procedure and insert the value in profile table first then get the max(profile_id) and insert this into Profile_plugin table it may be easy way to do that.


I hope the Self Generated ID column in Table Profile is IDENTITY Column. In this case you can retreive the newly Generated Identity Value using

@@Identity : Is a system function that returns the last-inserted identity value.
OR
Scope_Identity() : Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch.

Now if you are using same procedure to ad record in both table then simple access the newly created Identity value using @@Identity or Scope_identity.
Or
If you are using 2 seperate Procedure To Create new Profile and insert record in profiles_plugins then you can use OUTPUT Parameter which will return the newly generated Identity Value to the calling code.


这篇关于同时在拖表中保存用户的ID.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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