无论如何要重置表变量的身份? [英] Is there anyway to reset the identity of a Table Variable?

查看:30
本文介绍了无论如何要重置表变量的身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个表变量:

DECLARE @MyTableVar TABLE (ID INT IDENTITY(1,1), SomeData NVARCHAR(300))

插入 250 行后,我需要对表格重新开始".我这样做:

DELETE FROM @MyTableVar

我可以对表变量做些什么,以便:

insert into @MyTableVar Values("TestData")从@MyTableVar 中选择 *

将返回:

<前>_______________________________|身份证 |一些数据 ||___________|_____|||||1 |测试数据 ||_______|_____|

而不是这个:

<前>_______________________________|身份证 |一些数据 ||___________|_____|||||第251话测试数据 ||_______|_____|

解决方案

与其依赖 Identity,为什么不使用新的排名函数,例如 Row_Number

插入@MyTableVar( Id, Value )选择 Row_Number() Over(按值排序), 价值来自其他表

Say I have a table variable:

DECLARE @MyTableVar TABLE (ID INT IDENTITY(1,1), SomeData NVARCHAR(300))

After I have inserted 250 rows, I need to "Start Over" with the table. I do this:

DELETE FROM @MyTableVar

Is there anything I can do to the table variable so that this:

insert into @MyTableVar Values("TestData")
select * from @MyTableVar

will return this:

_______________________________
|    ID     |    SomeData     |
|___________|_________________|
|           |                 |   
|     1     |    TestData     |        
|___________|_________________|

instead of this:

_______________________________
|    ID     |    SomeData     |
|___________|_________________|
|           |                 |   
|    251    |    TestData     |        
|___________|_________________|

解决方案

Instead relying on an Identity, why not use the new ranking functions such as Row_Number

Insert @MyTableVar( Id, Value )
Select Row_Number() Over ( Order By Value )
    , Value
From SomeOtherTable

这篇关于无论如何要重置表变量的身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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