溫馨提示×

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

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

sqlserver中怎么導(dǎo)出插入腳本

發(fā)布時(shí)間:2021-08-04 16:06:33 來(lái)源:億速云 閱讀:263 作者:Leah 欄目:數(shù)據(jù)庫(kù)

sqlserver中怎么導(dǎo)出插入腳本,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

代碼如下: 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

關(guān)于sqlserver中怎么導(dǎo)出插入腳本問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

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

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

AI