需要帮助编写 SQL 查询吗?

发布于 2024-11-08 01:14:29 字数 818 浏览 1 评论 0原文

我有一个 SQL 表,其中的列包含“type|type1|type2|type3|type4”等字符串。 我需要选择字符串 id。拆分字符串并插入到另一个表中。第一项应该是默认的,我需要获取它的标识并插入到具有类型值的另一个表中。

请帮助我创建 T-SQL 查询以实现理想的结果。

示例

步骤 1 从表 1 中选择项目。
第 2 步 拆分为数组
第 3 步插入表 2(第一项为默认值)
第 4 步 使用基于 TypeID 和默认 True 的默认类型值更新表 1

表 1

ID       Items                           Default
--------------------------------------------------
1        type|type1|type2|type3|type4
2        type|type1|type2|type3|type4

表 2

ID   TypeID        Type    Default(bool)
--------------------------------------------------
1    1             type1   1
2    1             type2   0

I have a SQL table with column which contain string like 'type|type1|type2|type3|type4'.
I need to select the string, id. Split string and insert to another table. First item should be default and I need to get identity of it and insert to another table with the type value.

Please help me to create T-SQL query to accomplish desirable result.

Example

Step 1 To select Items from Table 1.
Step 2 Split to array
Step 3 Insert to Table 2 (first item will be default)
Step 4 Update Table 1 with default Type Value based on TypeID and Default True

Table 1

ID       Items                           Default
--------------------------------------------------
1        type|type1|type2|type3|type4
2        type|type1|type2|type3|type4

Table 2

ID   TypeID        Type    Default(bool)
--------------------------------------------------
1    1             type1   1
2    1             type2   0

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

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

发布评论

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

评论(2

小忆控 2024-11-15 01:14:29

使用 Arnold Fribble 在 此线程 上的回答中的 split 函数,

Create FUNCTION dbo.Split (@sep char(1), @s varchar(512))
RETURNS table
AS
RETURN (
    WITH Pieces(pn, start, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @s)
      UNION ALL
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
    )
    SELECT pn,
      SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
    FROM Pieces
  )
GO

您可以编写以下(我对 table2 中的 ID 字段以及 table1 中的 defaultTypeID 应该是什么进行了一些猜测,但您应该能够对其进行调整)

CREATE TABLE #table1       ( 
     id            INT, 
     items         VARCHAR(MAX), 
     defaulttypeid INT 
  ) 

CREATE TABLE #table2       ( 
     id        INT IDENTITY, 
     typeid    INT, 
     TYPE      VARCHAR(5), 
     isdefault BIT 
  ) 

INSERT INTO #table1 
VALUES      (1, 
             'type|type1|type2|type3|type4', 
             NULL), 
            (2, 
             'type|type1|type2|type3|type4', 
             NULL) 


INSERT INTO #table2 
            (typeid, 
             TYPE, 
             isdefault) 
SELECT id             typeid, 
       Rtrim(split.s) AS item, 
       CASE 
         WHEN ( split.pn = 1 ) THEN 1 
         ELSE 0 
       END            AS isdefault 
FROM   #table1 
       CROSS APPLY test.dbo.Split('|', items) AS split 

UPDATE #table1 
SET    defaulttypeid = t2.ID
FROM   #table1 t1 
       INNER JOIN #table2 t2 
         ON t1.id = t2.typeid 
            AND t2.isdefault = 1 


DROP TABLE #table1 

DROP TABLE #table2 

此输出

ID          Items                          DefaultTypeID
----------- ------------------------------ -------------
1           type|type1|type2|type3|type4    1     
2           type|type1|type2|type3|type4    6



ID          TypeID      Type  IsDefault
----------- ----------- ----- ---------
1           1           type  1
2           1           type1 0
3           1           type2 0
4           1           type3 0
5           1           type4 0
6           2           type  1
7           2           type1 0
8           2           type2 0
9           2           type3 0
10          2           type4 0

Using the split function from Arnold Fribble's answer on this thread

Create FUNCTION dbo.Split (@sep char(1), @s varchar(512))
RETURNS table
AS
RETURN (
    WITH Pieces(pn, start, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @s)
      UNION ALL
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
    )
    SELECT pn,
      SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
    FROM Pieces
  )
GO

You can write the following (I made some guesses about the ID field in table2 and what the defaultTypeID should be in table1 but you should be able to adjust that)

CREATE TABLE #table1       ( 
     id            INT, 
     items         VARCHAR(MAX), 
     defaulttypeid INT 
  ) 

CREATE TABLE #table2       ( 
     id        INT IDENTITY, 
     typeid    INT, 
     TYPE      VARCHAR(5), 
     isdefault BIT 
  ) 

INSERT INTO #table1 
VALUES      (1, 
             'type|type1|type2|type3|type4', 
             NULL), 
            (2, 
             'type|type1|type2|type3|type4', 
             NULL) 


INSERT INTO #table2 
            (typeid, 
             TYPE, 
             isdefault) 
SELECT id             typeid, 
       Rtrim(split.s) AS item, 
       CASE 
         WHEN ( split.pn = 1 ) THEN 1 
         ELSE 0 
       END            AS isdefault 
FROM   #table1 
       CROSS APPLY test.dbo.Split('|', items) AS split 

UPDATE #table1 
SET    defaulttypeid = t2.ID
FROM   #table1 t1 
       INNER JOIN #table2 t2 
         ON t1.id = t2.typeid 
            AND t2.isdefault = 1 


DROP TABLE #table1 

DROP TABLE #table2 

This outputs

ID          Items                          DefaultTypeID
----------- ------------------------------ -------------
1           type|type1|type2|type3|type4    1     
2           type|type1|type2|type3|type4    6



ID          TypeID      Type  IsDefault
----------- ----------- ----- ---------
1           1           type  1
2           1           type1 0
3           1           type2 0
4           1           type3 0
5           1           type4 0
6           2           type  1
7           2           type1 0
8           2           type2 0
9           2           type3 0
10          2           type4 0
爱的故事 2024-11-15 01:14:29

尽管我完全不同意使用游标,但我想不出其他方法。该解决方案尚未经过测试,但看起来应该没问题。

DECLARE @pos INT
DECLARE @id INT
DECLARE @string VARCHAR(MAX)
DECLARE @default INT
DECLARE @substring VARCHAR(MAX)

DECLARE tempCursor CURSOR FOR
SELECT id, string
    FROM table_name

OPEN tempCursor;
FETCH NEXT FROM tempCursor
    INTO @id, @string

WHILE @@FETCH_STATUS = 0
BEGIN
SET @default = 1
SET @pos = CHARINDEX('|', @string)
WHILE (@pos <> 0)
BEGIN
    SET @substring = SUBSTRING(@string, 1, @pos - 1)
    INSERT INTO table_name2(typeid, type, default) VALUES (@id, @substring, @default)
    SET @string = substring(@string, @pos+1, LEN(@string))
    SET @pos = charindex('|', @string)
    SET @default = 0
END

FETCH NEXT FROM tempCursor
    INTO @id, @string
END

CLOSE EWSCursor;
DEALLOCATE EWSCursor;

希望这有帮助。

Though I totally disagree with the use of cursors I can't think of another way. This solution isn't tested but It looks like it should be ok.

DECLARE @pos INT
DECLARE @id INT
DECLARE @string VARCHAR(MAX)
DECLARE @default INT
DECLARE @substring VARCHAR(MAX)

DECLARE tempCursor CURSOR FOR
SELECT id, string
    FROM table_name

OPEN tempCursor;
FETCH NEXT FROM tempCursor
    INTO @id, @string

WHILE @@FETCH_STATUS = 0
BEGIN
SET @default = 1
SET @pos = CHARINDEX('|', @string)
WHILE (@pos <> 0)
BEGIN
    SET @substring = SUBSTRING(@string, 1, @pos - 1)
    INSERT INTO table_name2(typeid, type, default) VALUES (@id, @substring, @default)
    SET @string = substring(@string, @pos+1, LEN(@string))
    SET @pos = charindex('|', @string)
    SET @default = 0
END

FETCH NEXT FROM tempCursor
    INTO @id, @string
END

CLOSE EWSCursor;
DEALLOCATE EWSCursor;

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文