插入带有字符串插入或表类型的表

时间:2017-12-20 03:12:37

标签: sql-server tsql insert

我有一个名为@ Tbl1的表,每个GROUP都是1行,我必须将每个行的行数提取到@Tbl_Insert类型。

 Declare @Tbl1 Table (TableName NVARCHAR(250),ColumnName NVARCHAR(250),DataType NVARCHAR(250),DataValue NVARCHAR(250),InGroup NVARCHAR(250))
 Declare @Tbl_Insert Table (ID INT, Name NVARCHAR(250), Age INT)

 -- Sample Data
 Insert Into @Tbl1 values ('@Tbl_Insert','ID','INT','1','Group1'),('@Tbl_Insert','Name','NVARCHAR(250)','John.Adam','Group1'),('@Tbl_Insert','Age','INT','10','Group1')
 Insert Into @Tbl1 values ('@Tbl_Insert','ID','INT','2','Group2'),('@Tbl_Insert','Name','NVARCHAR(250)','Andy.Law','Group2'),('@Tbl_Insert','Age','INT','18','Group2')

我可以将@ tbl1逐行转换为@Table_TEMP

Declare @Table_TEMP (Data nvarchar(max))

 Insert Into @Table_TEMP
 SELECT LEFT([DataValues] , LEN([DataValues] )-1) 
    FROM  @Tbl1 AS extern
    CROSS APPLY
    (

        SELECT Concat('''', Replace( ISNULL([DataValue],''), '''','' ) + ''',')
        FROM  @Tbl1 AS intern
        WHERE extern.InGroup = intern.InGroup
        Order By InGroup, ColumnName
        FOR XML PATH('')
    ) pre_trimmed ( [DataValues])
    GROUP BY InGroup,  [DataValues]     

我必须将@ Tbl1(或@Table_TEMP)中的行数提取到@Tbl_Insert。

我不想使用游标在@Table_TEMP中逐行循环插入,因为当你遇到大数据时(例如> 10000行)。它运行缓慢。

请帮忙。

3 个答案:

答案 0 :(得分:0)

我在stackorverflow中找到了样本

select a.*
from demo a
left join demo b on a.item_id < b.item_id
where b.item_id is null

它几乎相同,但是,

  1. 我的数据有&#34; DOT&#34;,无法使用PARSENAME

  2. 我必须知道DOT的数量才能构建动态SQL吗?

  3. PARSENAME仅支持3&#34; DOT&#34;,当更多点时,它为空。 示例:

  4. 声明@ObjectName nVarChar(1000) 设置@ObjectName =&#39; HeadOfficeSQL1.Northwind.dbo.Authors&#39;

    选择         PARSENAME(@ ObjectName,5)作为Server4,         PARSENAME(@ ObjectName,4)作为服务器,         PARSENAME(@ ObjectName,3)作为DB,         PARSENAME(@ ObjectName,2)作为所有者,         PARSENAME(@ ObjectName,1)as Object

答案 1 :(得分:0)

如果,我理解正确,您需要使用apply才能获取记录&amp;将数据插入其他表

insert into @Tbl_Insert (ID, Name, Age)
select max(a.id) [id], max(a.Name) [Name], max(a.Age) [Age] from @Tbl1 t
cross apply
          (values
             (case when t.ColumnName = 'ID' then t.DataValue end,
              case when t.ColumnName = 'Name' then t.DataValue end,
              case when t.ColumnName = 'Age' then t.DataValue end, t.InGroup)
          ) as a(id, Name, Age, [Group])
group by a.[Group]

select * from @Tbl_Insert

答案 2 :(得分:0)

我同时做了@Tbl_Insert&amp;像PARSENAME一样创建1个商店。它的性能有所提升。

create  function dbo.fnGetCsvPart(@csv varchar(8000),@index tinyint, @last bit = 0)
returns varchar(4000)
as
/* function to retrieve 0 based "column" from csv string */
begin
    declare @i int; set @i = 0
    while 1 = 1
    begin
        if @index = 0
        begin
            if @last = 1 or charindex(',',@csv,@i+1) = 0
                return substring(@csv,@i+1,len(@csv)-@i+1)
            else
                return substring(@csv,@i+1,charindex(',',@csv,@i+1)-@i-1)
        end
        select @index = @index-1, @i = charindex(',',@csv,@i+1)
        if @i = 0 break
    end
    return null
end
GO