您好,登錄后才能下訂單哦!
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
關于sqlserver中怎么導出插入腳本問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。