如何在SQL中生成userid [英] How to generate userid in SQL

查看:268
本文介绍了如何在SQL中生成userid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明@UID varchar(30)

设置@ UID = @ UserId

设置@ UID = @UserId

SET @UserId = @UID + 1;





INSERT INTO Tbl_Anti_Ragging2(UserId,CollegeId,Year,Duration,authoritites_Name,authoritites_Designation,authoritites_Contact)
VALUES(@ UserId,@ CollegeId,@ Year,@ Duration,@ authoritites_Name,@ authoritites_Designation,@ authoritites_Contact)



i我在一个发送多行时间来自我的C#代码,我想在第一次保存时插入然后值保存为1

1

1

1

第二次2

2

2

2



我尝试过:



以上是我的查询 -



i我插入值我想知道如何插入意味着查询执行然后



Userid make like 1

1

1

1

当查询第二次执行时,我会做用户ID +1 -



2

2

2

2

所以请帮帮我 -

Declare @UID varchar(30)
Set @UID=@UserId
Set @UID= @UserId
SET @UserId = @UID+1;


INSERT INTO Tbl_Anti_Ragging2(UserId,CollegeId,Year,Duration,authoritites_Name,authoritites_Designation,authoritites_Contact)
VALUES(@UserId,@CollegeId,@Year,@Duration,@authoritites_Name,@authoritites_Designation,@authoritites_Contact)

i am sending multiple row at one time from my C# code and i want to insert when First save butoon then value save like 1
1
1
1
second time 2
2
2
2

What I have tried:

Above is my Query -

i am inserting value i want to know how to insert means query execute then

Userid make like 1
1
1
1
and when query second time execute then i will do userid +1 like-

2
2
2
2
so Please help me--

推荐答案

正如@NotPoliticallyCorrect已经说过的,正确的方法是使用Identity列。至少不要将此列称为ID - 除非这只是生成数据的一种方法,并且此表上的UserId列实际上是另一个表的外键。



可以只在你的C#代码中增加一个数字 - 它必须是一个类级别(全局)变量。这不是一个好的设计 - 它假设这个程序只能由一个用户作为这个实例运行,一旦完成就不再需要了。



可以这样做......

As @NotPoliticallyCorrect has already said, the correct way to do this is to use an Identity column. At the very least do not refer to this column as an ID - unless this is just a means of generating data and the UserId column on this table is actually a foreign key to another table.

You could just increment a number within your C# code - it would have to be a class-level ("global") variable. This is not a good design however - it assumes that this program will only ever be run as this instance by one user and once finished will not be needed again.

You could do this instead...
CREATE TABLE GenerateIDs
(
	NextId int identity(1,1),
	requestBy nvarchar(125)
)



如果创建要更新的存储过程此表


If you create a Stored Procedure to update this table

CREATE PROCEDURE [dbo].GetId @who nvarchar(125)
AS
	INSERT INTO GenerateIDs VALUES(@who) 
	SELECT SCOPE_IDENTITY()



然后,每次要增加该数字时,首先运行存储过程以获取要使用的Id号。


Then each time you want to increment that number run the Stored Procedure first to get the Id number to use.


这篇关于如何在SQL中生成userid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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