SQL Server:将数据移动到另一个有附加数据的表

本文关键字:数据 另一个 Server 移动 SQL | 更新日期: 2023-09-27 18:10:43

我有一个包含6000个成员数据的表。我需要那张表中的一些具体数据。我可以把这个移到我的桌子上,但是有点复杂。我的新成员系统的工作方式是这样的,当一个成员注册它给这个成员一个唯一的6-8位的id/令牌,并创建另外两个表与这个成员的令牌在它。

我的意思是第一张表是这样的

[inID][stMail][stPass][stToken]  .. etc

这个令牌是我的另一个表的键

[stToken][stName][stSurname][stAdress][stPhone]...etc..

当我将这些数据移动到我的表时,我需要创建两个与原始表具有相同令牌的表。

所以它看起来像

[1][mail][pass][123123]
[123123][name][surname][address][phone]

如何在SQL或asp.net c#中做到这一点?

我尝试从旧表中选择所有数据,取我需要的字符串值并写下我的新表,但8小时后它只写200个成员的数据,这是奇数。

对不起,我的英语很有趣。

SQL Server:将数据移动到另一个有附加数据的表

你可以像下面这样尝试。在这种情况下,我假设你有一个生成token string的逻辑。现在,我将简单地用row_number()值替换该标记。这种方法使用临时表。

-- create a #temp table
create table #temp
(
  [inID] int, 
  [stMail] varchar(20),
  [stPass] varchar(20),
  [stToken] varchar(20),
  [stName] varchar(20),
  [stSurname] varchar(20),
  [stAdress] varchar(20),
  [stPhone] varchar(20)
)
--insert values in #temp table
insert into #temp
select
  [inID]     ,
  [stMail]   ,
  [stPass]   ,
  row_number() over (order by inID,stMail) as [stToken]  ,
  [stName]   ,
  [stSurname],
  [stAdress] ,
  [stPhone]  
 from t
--copy from #temp table into t1 and t2 tables
 insert into t1
 select 
  [inID]     ,
  [stMail]   ,
  [stPass]   ,
  [stToken]  
 from #temp
 insert into t2
 select 
  [stToken]  ,
  [stName]   ,
  [stSurname],
  [stAdress] ,
  [stPhone]  
 from #temp
--don't forget to drop the #temp table
 drop table #temp

查看演示SQL小提琴链接在这里:http://sqlfiddle.com/#!3/185eb/4

相关文章: