简体   繁体   English

如何拆分ntext数据类型数据以存储在数据库中

[英]how to split ntext datatype data to store in database

I have a column of type ntext called Emp_details_list , and it consists of data like 我有一个ntext类型的列,称为Emp_details_list ,它包含如下数据

 emp1#@#emp2#@#emp3...

At most it has 20 thousand characters as string and I am storing in that column and I need to split it and save in other table EmpDet and in other column ( Single_Emp_det ) but while splitting I can't cast ntext as nvarchar so am using a local variable and declared as nvarchar(max) and splitting but I can store only 8000 character only if I have 8001 characters it showing exception because it can't store so how can I store whole ntext data in other column using splitting concept in SQL Server 最多有2万个字符作为字符串,我存储在该列中,我需要将其拆分并保存在其他表EmpDet和其他列( Single_Emp_det )中,但是在拆分时,我无法将ntext Single_Emp_detnvarchar因此我使用了局部变量并声明为nvarchar(max)并进行拆分,但是只有当我有8001个字符时显示异常,因为它无法存储,因此我只能存储8000个字符,因此我如何使用SQL Server中的拆分概念将整个ntext数据存储在其他列中

So you are probably stuck with Sql server 2000. If you can't use nvarchar(max), one possible way is to may be use substring function and copy your ntext to manageable chunks of varchar(8000) in a loop. 因此,您可能会陷入Sql Server 2000的困境。如果不能使用nvarchar(max),则一种可能的方法是使用子字符串函数并将ntext循环复制到可管理的varchar(8000)块中。 In each iteration, save the 'part of chunk after the last #', to be used in next iteration. 在每次迭代中,保存“最后一个#之后的部分块”,以在下一次迭代中使用。 So you basically loop over your table, within that loop again loop over the ntext field value in chunks of 8k and do the rest. 因此,您基本上遍历了表,在此循环中,再次遍历了8k块中的ntext字段值,然后执行其余操作。 Hope it is clear enough. 希望它足够清楚。

As others already mentioned you can easily store 20000 characters in nvarchar(max). 正如其他人已经提到的那样,您可以轻松地在nvarchar(max)中存储20000个字符。 You are probably doing something wrong when converting these types. 转换这些类型时,您可能做错了。

Here is an example of converting from and to nvarchar(max) that clearly shows how can you store 20000 characters there. 这是一个从nvarchar(max)转换为nvarchar(max)的示例,清楚地说明了如何在其中存储20000个字符。

DECLARE @v1 nvarchar(max)

DECLARE @v2 nvarchar(max)



create table #textExample

(

    id int,

    t1 ntext

)



declare @count int

set @v1 = ''

SET @count = 0

while @count < 20000

begin  

   set @v1 = @v1 + '1'

   set @count = @count + 1

end

--converting nvarchar(max) to ntext

insert into #textExample

values (1, CONVERT(ntext,@v1))



select * from #textExample

-- converting ntext back to nvarchar(max)

SET @v2 = CONVERT(nvarchar(max), (select t1 from #textExample where id = 1))

select @v2, LEN(@v2)





drop table #textExample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM