程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> sqlserver 導出拔出劇本代碼

sqlserver 導出拔出劇本代碼

編輯:MSSQL

sqlserver 導出拔出劇本代碼。本站提示廣大學習愛好者:(sqlserver 導出拔出劇本代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是sqlserver 導出拔出劇本代碼正文


固然有其它對象可以做這件事,但假如客戶不許可你在辦事器亂裝器械時這個劇本就會有效了。

DECLARE @tbImportTables table(tablename varchar(128), deleted tinyint)

-- append tables which you want to import
Insert Into @tbImportTables(tablename, deleted) values('tentitytype', 1)
Insert Into @tbImportTables(tablename, deleted) values('tattribute', 1)
-- append all tables
--Insert Into @tbImportTables(tablename, deleted) select table_name, 1 from INFORMATION_SCHEMA.tables where table_type = 'BASE TABLE'

DECLARE @tbImportScripts table(script varchar(max))

Declare @tablename varchar(128),
@deleted tinyint,
@columnname varchar(128),
@fieldscript varchar(max),
@valuescript varchar(max),
@insertscript varchar(max)

Declare curImportTables Cursor For
Select tablename, deleted
From @tbImportTables

Open curImportTables
Fetch Next From curImportTables Into @tablename, @deleted

WHILE @@Fetch_STATUS = 0
Begin
  If (@deleted = 1)
  begin
    Insert into @tbImportScripts(script) values ('Truncate table ' + @tablename)
  end

  Insert into @tbImportScripts(script) values ('SET IDENTITY_INSERT ' + @tablename + ' ON')

  set @fieldscript = ''
  select @fieldscript = @fieldscript + column_name + ',' from INFORMATION_SCHEMA.columns where table_name = @tablename and data_type not in('timestamp', 'image')
  set @fieldscript = substring(@fieldscript, 0, len(@fieldscript))

  set @valuescript = ''
  select @valuescript = @valuescript + 'case when ' + column_name + ' is null then ''null'' else '''''''' + convert(varchar(max), ' + column_name + ') + '''''''' end +'',''+'   from INFORMATION_SCHEMA.columns where table_name = @tablename and data_type not in('timestamp', 'image')
  set @valuescript = substring(@valuescript, 0, len(@valuescript) - 4)

  set @insertscript = 'select ''insert into ' + @tablename + '(' + @fieldscript + ') values(' + '''+' + @valuescript + ' + '')'' from ' + @tablename
  Insert into @tbImportScripts(script) exec ( @insertscript)

  Insert into @tbImportScripts(script) values ('SET IDENTITY_INSERT ' + @tablename + ' OFF')

  Insert into @tbImportScripts(script) values ('GO ')
  Fetch Next From curImportTables Into @tablename, @deleted
End

Close curImportTables
Deallocate curImportTables

Select * from @tbImportScripts

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved