溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何復(fù)制Image字段數(shù)據(jù)到新表同樣的字段內(nèi)

發(fā)布時(shí)間:2021-10-13 11:18:17 來源:億速云 閱讀:257 作者:柒染 欄目:數(shù)據(jù)庫

本篇文章給大家分享的是有關(guān)如何復(fù)制Image字段數(shù)據(jù)到新表同樣的字段內(nèi),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

  最近在寫一個(gè)升級(jí)程序,其中要求將一個(gè)舊數(shù)據(jù)庫里面的所有的照片都轉(zhuǎn)到新數(shù)據(jù)庫。暫且把舊數(shù)據(jù)庫叫OldDB,新數(shù)據(jù)庫叫NewDB,新數(shù)據(jù)庫里面的字段為【Photo】,舊數(shù)據(jù)庫叫【Picture】 。下面億速云小編來講解下如何復(fù)制Image字段數(shù)據(jù)到新表同樣的字段內(nèi)?

  如何復(fù)制Image字段數(shù)據(jù)到新表同樣的字段內(nèi)

  先讀出舊數(shù)據(jù)庫里的數(shù)據(jù),然后用insert into插入:

  insert into[ table1] values("&rs("Picture")&")

  后來發(fā)現(xiàn)不行,我以為數(shù)據(jù)類型搞錯(cuò)了,改了下代碼,如下:

  insert into[ table1] values(‘"&rs("Picture")&"’)

  多加了一對(duì)單引號(hào),系統(tǒng)還是提示錯(cuò)誤。以下為存儲(chǔ)過程:

  CENTER>ccid_nobr>

  table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">

  tr>

  td bgcolor="e6e6e6" class="code" >

  pre>ccid_code>

  if exists (select * from dbo.sysobjects

  where id = object_id(N'[dbo].[sp_textcopy]')

  and OBJECTPROPERTY(id, N'IsProcedure') = 1)

  drop procedure [dbo].[sp_textcopy]

  GO

  SET QUOTED_IDENTIFIER OFF

  GO

  SET ANSI_NULLS OFF

  GO

  CREATE PROCEDURE sp_textcopy (

  @srvname varchar (30),

  @login varchar (30),

  @password varchar (30),

  @dbname varchar (30),

  @tbname varchar (30),

  @colname varchar (30),

  @filename varchar (30),

  @whereclause varchar (40),

  @direction char(1))

  AS

  DECLARE @exec_str varchar (255)

  SELECT @exec_str =

  'textcopy /S ' + @srvname +

  ' /U ' + @login +

  ' /P ' + @password +

  ' /D ' + @dbname +

  ' /T ' + @tbname +

  ' /C ' + @colname +

  ' /W "' + @whereclause +

  '" /F ' + @filename +

  ' /' + @direction

  EXEC master..xp_cmdshell @exec_str

  GO

  SET QUOTED_IDENTIFIER OFF

  GO

  SET ANSI_NULLS ON

  GO

  /ccid_code>/pre>

  /td>

  /tr>

  /table>

  /ccid_nobr>/CENTER>

  這是調(diào)用textcopy將圖片數(shù)據(jù)導(dǎo)入和導(dǎo)出的語句,我原打算新舊數(shù)據(jù)庫調(diào)用同一個(gè)存儲(chǔ)過程,舊數(shù)據(jù)庫先將數(shù)據(jù)導(dǎo)出到硬盤,新數(shù)據(jù)庫在導(dǎo)入。在測(cè)試以后,發(fā)現(xiàn)不成功,頁面顯示不出數(shù)據(jù),單獨(dú)用textcopy測(cè)試還是可以導(dǎo)出數(shù)據(jù)的,但在程序中調(diào)用,沒有成功。我接下來換成下面的存儲(chǔ)過程:

  CENTER>ccid_nobr>

  table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">

  tr>

  td bgcolor="e6e6e6" class="code" >

  pre>ccid_code>

  if exists (select * from dbo.sysobjects

  where id = object_id(N'[dbo].[sp_imageio]')

  and OBJECTPROPERTY(id, N'IsProcedure') = 1)

  drop procedure [dbo].[sp_imageio]

  GO

  SET QUOTED_IDENTIFIER OFF

  GO

  SET ANSI_NULLS OFF

  GO

  Create proc p_binaryIO

  @servename varchar (30),

  --服務(wù)器名稱

  @username varchar (30),

  --用戶名

  @password varchar (30),

  --密碼

  @tbname varchar (500),

  --數(shù)據(jù)庫..表名

  @fdname varchar (30),

  --字段名

  @fname varchar (1000),

  --目錄+文件名,處理過程中要使用/覆蓋:@filename+_temp

  @tj varchar (1000)='',

  --處理?xiàng)l件.對(duì)于數(shù)據(jù)導(dǎo)入,如果條件中包含@fdname,請(qǐng)指定表名前綴

  @isout bit=1 --1導(dǎo)出((默認(rèn)),0導(dǎo)入

  AS

  declare @fname_in varchar(1000)

  --bcp處理應(yīng)答文件名

  ,@fsize varchar(20)

  --要處理的文件的大小

  ,@m_tbname varchar(50)

  --臨時(shí)表名

  ,@sql varchar(8000)

  --則取得導(dǎo)入文件的大小

  if @isout=1

  set @fsize='0'

  else

  begin

  create table #tb(可選名

  varchar(20),大小 int

  ,創(chuàng)建日期 varchar(10),創(chuàng)建時(shí)間

  varchar(20)

  ,上次寫操作日期 varchar(10),

  上次寫操作時(shí)間 varchar(20)

  ,上次訪問日期 varchar(10),

  上次訪問時(shí)間 varchar(20),特性 int)

  insert into #tb

  exec master..xp_getfiledetails @fname

  select @fsize=大小 from #tb

  drop table #tb

  if @fsize is null

  begin

  print '文件未找到'

  return

  end

  end

  --生成數(shù)據(jù)處理應(yīng)答文件

  set @m_tbname='

  [##temp'+cast(newid() as varchar(40))+']'

  set @sql='select * into '+@m_tbname+' from(

  select null as 類型

  union all select 0 as 前綴

  union all select '+@fsize+' as 長度

  union all select null as 結(jié)束

  union all select null as 格式

  ) a'

  exec(@sql)

  select @fname_in=@fname+'_temp'

  ,@sql='bcp "'+@m_tbname+'" out "'+@fname_in

  +'" /S"'+@servename

  +case when isnull(@username,'')='' then ''

  else '" /U"'+@username end

  +'" /P"'+isnull(@password,'')+'" /c'

  exec master..xp_cmdshell @sql

  --刪除臨時(shí)表

  set @sql='drop table '+@m_tbname

  exec(@sql)

  if @isout=1

  begin

  set @sql='bcp "select top 1 '+@fdname+' from '

  +@tbname+case isnull(@tj,'') when '' then ''

  else ' where '+@tj end

  +'" queryout "'+@fname

  +'" /S"'+@servename

  +case when isnull(@username,'')='' then ''

  else '" /U"'+@username end

  +'" /P"'+isnull(@password,'')

  +'" /i"'+@fname_in+'"'

  exec master..xp_cmdshell @sql

  end

  else

  begin

  --為數(shù)據(jù)導(dǎo)入準(zhǔn)備臨時(shí)表

  set @sql='select top 0 '+@fdname+' into '

  +@m_tbname+' from ' +@tbname

  exec(@sql)

  --將數(shù)據(jù)導(dǎo)入到臨時(shí)表

  set @sql='bcp "'+@m_tbname+'" in "'+@fname

  +'" /S"'+@servename

  +case when isnull(@username,'')='' then ''

  else '" /U"'+@username end

  +'" /P"'+isnull(@password,'')

  +'" /i"'+@fname_in+'"'

  exec master..xp_cmdshell @sql

  --將數(shù)據(jù)導(dǎo)入到正式表中

  set @sql='update '+@tbname

  +' set '+@fdname+'=b.'+@fdname

  +' from '+@tbname+' a,'

  +@m_tbname+' b'

  +case isnull(@tj,'') when '' then ''

  else ' where '+@tj end

  exec(@sql)

  --刪除數(shù)據(jù)處理臨時(shí)表

  set @sql='drop table '+@m_tbname

  end

  --刪除數(shù)據(jù)處理應(yīng)答文件

  set @sql='del '+@fname_in

  exec master..xp_cmdshell @sql

  GO

  SET QUOTED_IDENTIFIER OFF

  GO

  SET ANSI_NULLS ON

  GO

  /ccid_code>/pre>

  /td>

  /tr>

  /table>

  /ccid_nobr>/CENTER>

  如何復(fù)制Image字段數(shù)據(jù)到新表同樣的字段內(nèi)

  這個(gè)存儲(chǔ)過程,基本原理其實(shí)和上面的是一樣的。我先測(cè)試導(dǎo)出,在程序中運(yùn)行后,不斷刷新保存導(dǎo)出圖片的文件夾,發(fā)現(xiàn)系統(tǒng)確實(shí)產(chǎn)生了數(shù)據(jù),以文件+tmp的命名方式存放,但不知道為什么,系統(tǒng)運(yùn)行完畢,所有的圖片還是沒有。這條路看來不通。

  帖子發(fā)出來以后,很快有人回復(fù)。但是回復(fù)的答案就是我找到的兩個(gè)存儲(chǔ)過程的??磥韱栴}可能無法解決。

  但是任務(wù)還是的完成,最后萬般無奈之下,我給認(rèn)識(shí)的一位大俠發(fā)了求救信,他只是提供了一條思路,用流的方式寫入。我按照這個(gè)思路,抱著試試看的心情,將代碼改成如下的樣式:

  set rs2=Server.CreateObject("Adodb.Recordset")

  sql2="select top 1 * from [User] order by UserID desc"

  rs2.open sql2,cn,1,3

  rs2("Photo").AppendChunk rs("Picture1")

  rs2.update

  rs2.close

  set rs2=nothing

以上就是如何復(fù)制Image字段數(shù)據(jù)到新表同樣的字段內(nèi),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI