将 Row1 与 Row2、Row3 与 Row4 连接

发布于 2024-10-17 01:15:20 字数 1927 浏览 1 评论 0原文

我有一个小问题。我需要能够将 Row1 与 Row2 连接到另一个/行/表,并在第三行重新开始,在 Row4 上加入 Row3 ..等等。

我遇到的问题是 Row1 和 Row2 之间没有关系。唯一的共同点是 Row2 是 Row1 的一部分。

这可以做到吗? (可以通过 SQL SERVER、SSIS 或 ASP.NET VB 完成)

下面是列 1(单列)中数据的示例输出 在

ROW1: 000201031993 JOHN SMITH         Enabled             02/10/2011 08:43:13 
ROW2: -->Key Fob/Passcode/AES              02/14/2008 00:00:00   07/31/2011 00:00

ROW3: 000201031994 FOO GLOO          Enabled             02/13/2011 11:01:58 
ROW4: -->Key Fob/Passcode/AES              02/14/2008 00:00:00   07/31/2011 00:00

表的开头还有其他垃圾(标题信息等)(前几个)列)(通过 SSIS 从 csv 导入),但这可以在每次导入之前手动清理(不推荐)。

更新:

我试图在 csv 转换期间通过我的光标将 ROWID 添加到表中一个存储过程。但是,我有困难。我遇到的问题是(我认为)我设置的 ROWID 变量不断被释放,因此无法保留计数。

下面是我的 SP(没有 ROWID 行)。知道我该怎么做吗?我会在之后进行插入,但这只会在表格底部添加行。

Declare @success integer
Declare @Var_RowID integer
Declare @Var_TokenID integer
Declare @Var_DisplayName varchar(50)
Declare @Var_TokenStatus varchar(30)
Declare @Var_ImportDate datetime

DECLARE c1 cursor FAST_FORWARD for 

SELECT 

LTRIM(RTRIM(LEFT(rawdata,12))) as TokenID_V,
LTRIM(RTRIM(SUBSTRING(rawdata, 13, (35 - 13)))) as DisplayName_V,
LTRIM(RTRIM(Replace((Substring(substring(rawdata, 35, LEN(rawdata)),1,20)),'.',''))) as TokenStatus_v,
Getdate() as ImportDate_V
FROM TokenDataInput
WHERE RawData like ('0%')
ORDER BY TokenID_V ASC

OPEN c1
FETCH NEXT FROM c1 INTO

@Var_TokenID,
@Var_DisplayName,
@Var_TokenStatus,
@Var_ImportDate

WHILE @@FETCH_STATUS = 0
BEGIN

INSERT INTO dbo.TokenData_Formatted(DisplayName, TokenStatus, ImportData)
VALUES(@Var_TokenID, @Var_DisplayName, @Var_TokenStatus, @Var_ImportDate)

FETCH NEXT FROM c1 INTO

@Var_TokenID,
@Var_DisplayName,
@Var_TokenStatus,
@Var_ImportDate

END
CLOSE C1
DEALLOCATE C1

SET @success = 1

RETURN @success

END
enter code here

另外,我尝试使用 Cursor.rowID 但得到“Multipart indetifier”..计数未绑定。

光标是否有一个我可以点击的内置行数?

I have a little issue. I need to be able to concatonate the Row1 with Row2 to another/row/table, and the start again on the 3rd row, Join Row3 on Row4 .. and so forth.

The issue i have is that there is no relationship between Row1 and Row2. The only common thing is that Row2 is part of Row1.

Can this be done? (Can be done with either by SQL SERVER, SSIS or ASP.NET VB)

Below is a sample output of the data in column1 (single column)

ROW1: 000201031993 JOHN SMITH         Enabled             02/10/2011 08:43:13 
ROW2: -->Key Fob/Passcode/AES              02/14/2008 00:00:00   07/31/2011 00:00

ROW3: 000201031994 FOO GLOO          Enabled             02/13/2011 11:01:58 
ROW4: -->Key Fob/Passcode/AES              02/14/2008 00:00:00   07/31/2011 00:00

There is also other rubbish (header info etc) at the start of the table (first few columns) (which was imported via SSIS from a csv) but this can be cleaned up manually (not preffered) each time before the import ..

Update:

I am trying to add the ROWID into the table during the csv converision via my Cursor in a StoredProcedure. However, i am having difficulty. The issue i am having is that (i think) the ROWID variable i have set keeps getting deallocated, hence not able to keep a count.

Below is my SP (without the ROWID lines). Any idea how i can do this. I would do an insert afterwards but that just adds the rows at the bottom of the table.

Declare @success integer
Declare @Var_RowID integer
Declare @Var_TokenID integer
Declare @Var_DisplayName varchar(50)
Declare @Var_TokenStatus varchar(30)
Declare @Var_ImportDate datetime

DECLARE c1 cursor FAST_FORWARD for 

SELECT 

LTRIM(RTRIM(LEFT(rawdata,12))) as TokenID_V,
LTRIM(RTRIM(SUBSTRING(rawdata, 13, (35 - 13)))) as DisplayName_V,
LTRIM(RTRIM(Replace((Substring(substring(rawdata, 35, LEN(rawdata)),1,20)),'.',''))) as TokenStatus_v,
Getdate() as ImportDate_V
FROM TokenDataInput
WHERE RawData like ('0%')
ORDER BY TokenID_V ASC

OPEN c1
FETCH NEXT FROM c1 INTO

@Var_TokenID,
@Var_DisplayName,
@Var_TokenStatus,
@Var_ImportDate

WHILE @@FETCH_STATUS = 0
BEGIN

INSERT INTO dbo.TokenData_Formatted(DisplayName, TokenStatus, ImportData)
VALUES(@Var_TokenID, @Var_DisplayName, @Var_TokenStatus, @Var_ImportDate)

FETCH NEXT FROM c1 INTO

@Var_TokenID,
@Var_DisplayName,
@Var_TokenStatus,
@Var_ImportDate

END
CLOSE C1
DEALLOCATE C1

SET @success = 1

RETURN @success

END
enter code here

Also, I tried to use Cursor.rowID but got the "Multipart indetifier" .. count not be bound.

Does the cursor have a built in rowcount which I could tap onto?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-10-24 01:15:20

如果你有一个 int id 列对行进行编号...顺序你可以做这样的事情...

SELECT TOP 1000 
      t1.[name],
      t1.[row_id],
       t2.[name],
      t2.[row_id]  
  FROM [testTable] t1
  inner join [testTable] t2 on t1.row_id = (t2.row_id +1)
  where (t1.row_id % 2) = 1

如果你没有这样的列,你可以使用...

alter table testTable add row_id int identity(1,1)

所以你的查询可能看起来像这样...

SELECT t1.[Data] + ' ' + t2.[Data]
    FROM [testTable] t1
        inner join [testTable] t2 
            on t1.row_id = (t2.row_id +1)
    where (t1.row_id % 2) = 1

If you had a int id column that numbered the rows... sequentially you could do something like this...

SELECT TOP 1000 
      t1.[name],
      t1.[row_id],
       t2.[name],
      t2.[row_id]  
  FROM [testTable] t1
  inner join [testTable] t2 on t1.row_id = (t2.row_id +1)
  where (t1.row_id % 2) = 1

If you don't have such a column you could use...

alter table testTable add row_id int identity(1,1)

so your query might look like this...

SELECT t1.[Data] + ' ' + t2.[Data]
    FROM [testTable] t1
        inner join [testTable] t2 
            on t1.row_id = (t2.row_id +1)
    where (t1.row_id % 2) = 1
陌生 2024-10-24 01:15:20

您需要了解的第一件事是,如果没有 ORDER BY 子句,则无法 100% 保证表行的输出顺序。从 CSV 导入的内容可能是按顺序排列的,但您必须在 CSV 数据中拥有一个键,或者保留插入顺序的 IDENTITY 列才能使其正常工作。

一旦你克服了这一点,jsobo 的查询就可以通过在奇数列上分割数据并连接到偶数列来很好地工作。

The first thing you need to understand is that without an ORDER BY clause, there is no 100% guarantee on the output order of your table rows. What you have imported from CSV could be in-order, but you must have a key within the CSV data, or an IDENTITY column that preserves insert order for this to work.

Once you get past that, jsobo's query works great by splitting the data on the odd columns and joining to the even.

随风而去 2024-10-24 01:15:20

我可能没有完全遵循你在这里所做的事情,所以如果我偏离了轨道,我很抱歉,但是一个小的 DOS BAT 文件不能为你做连接,然后你可以将它拉到你需要的地方吗?

@echo off
Setlocal EnableDelayedExpansion
set oddRow=Empty

FOR /F "tokens=* delims= " %%A in (c:\temp\test.txt) do (
   if "!oddRow!" == "Empty" (
      set oddRow=%%A
   ) else (
      echo !oddRow! %%A >> testout.txt
      set  oddRow=Empty
   )
)

I may not be follwing exactly what you are doing here, so I apologize if I'm off track but could a little DOS BAT file not do the concat for you and then you can pull it in where you need it?

@echo off
Setlocal EnableDelayedExpansion
set oddRow=Empty

FOR /F "tokens=* delims= " %%A in (c:\temp\test.txt) do (
   if "!oddRow!" == "Empty" (
      set oddRow=%%A
   ) else (
      echo !oddRow! %%A >> testout.txt
      set  oddRow=Empty
   )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文