sql2005創(chuàng)建存儲過程導(dǎo)出數(shù)據(jù)inset插入語句-如何導(dǎo)出數(shù)據(jù)
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
:sql2005創(chuàng)建存儲過程導(dǎo)出數(shù)據(jù)inset插入語句-如何導(dǎo)出數(shù)據(jù) 存儲過程代碼如下圖: create PROCEDURE [dbo].[Proc_Generateinsert] @tablename SYSNAME, @filter NVARCHAR(500), @Order NVARCHAR(500) AS DECLARE @column VARCHAR(MAX) DECLARE @columndata VARCHAR(MAX) DECLARE @sql VARCHAR(MAX) DECLARE @xtype TINYINT DECLARE @name SYSNAME DECLARE @objectId INT DECLARE @objectname SYSNAME DECLARE @ident INT
SET NOCOUNT ON SET @objectId = OBJECT_ID(@tablename)
IF @objectId IS NULL -- 判斷對象是否存在 BEGIN PRINT 'The object not exists' RETURN END
SET @objectname = RTRIM(OBJECT_NAME(@objectId))
IF @objectname IS NULL OR CHARINDEX(@objectname, @tablename) = 0 --此判斷不嚴(yán)密 BEGIN PRINT 'object not in current database' RETURN END
IF OBJECTPROPERTY(@objectId, 'IsTable') < > 1 -- 判斷對象是否是table BEGIN PRINT 'The object is not table' RETURN END
select @ident = STATUS & 0x80 from syscolumns where id = @objectid AND STATUS & 0x80 = 0x80
IF @ident IS NOT NULL PRINT 'SET IDENTITY_insert ' + @TableName + ' ON'
DECLARE syscolumns_cursor CURSOR FOR select c.name, c.xtype from syscolumns c where c.id = @objectid ORDER BY c.colid OPEN syscolumns_cursor SET @column = '' SET @columndata = '' FETCH NEXT from syscolumns_cursor INTO @name,@xtype WHILE @@fetch_status < > -1 BEGIN IF @@fetch_status < > -2 BEGIN IF @xtype NOT IN (189, 34, 35, 99, 98) --timestamp不需處理,image,text,ntext,sql_variant 暫時(shí)不處理 BEGIN SET @column = @column + CASE WHEN LEN(@column) = 0 THEN '' ELSE ',' END + '[' + RTRIM(@name) +']'
SET @columndata = @columndata + CASE WHEN LEN(@columndata) = 0 THEN '' ELSE ','','',' END
+ CASE WHEN @xtype IN (167, 175) THEN '''''''''+rtrim([' + @name + '])+''''''''' --varchar,char WHEN @xtype IN (231, 239) THEN '''''''''+rtrim([' + @name + '])+''''''''' --nvarchar,nchar WHEN @xtype = 61 THEN '''''''''+convert(char(23),[' + @name + '],121)+''''''''' --datetime WHEN @xtype = 58 THEN '''''''''+convert(char(16),[' + @name + '],120)+''''''''' --smalldatetime WHEN @xtype = 36 THEN '''''''''+convert(char(36),[' + @name + '])+''''''''' --uniqueidentifier ELSE '[' + RTRIM(@name) + ']' END END END FETCH NEXT from syscolumns_cursor INTO @name,@xtype END
CLOSE syscolumns_cursor DEALLOCATE syscolumns_cursor
SET @sql = 'set nocount on select ''insert ' + @tablename + '(' + @column + ') values(''as ''--'',' + RTRIM(@columndata) + ','')'' from ' + @tablename
IF @filter IS NOT NULL AND LEN(RTRIM(@filter)) <> 0 SET @sql = @sql + ' where ' + @filter
IF @Order IS NOT NULL AND LEN(RTRIM(@Order)) <> 0 SET @sql = @sql + ' Order By ' + @Order
PRINT '--' + @sql exec (@sql)
IF @ident IS NOT NULL PRINT 'SET IDENTITY_insert ' + @TableName + ' OFF' 創(chuàng)建完后,執(zhí)行存儲過程,要輸入三個(gè)變量,@tablename是表名,@filter是斷點(diǎn)查詢條件,接where后的條件,@Order是排序,按哪個(gè)字段排序,若全導(dǎo),設(shè)為空即可 代碼如下圖: exec Proc_Generateinsert @tablename='scm_bom_list',@filter='',@Order='' 執(zhí)行完把結(jié)果復(fù)制出來執(zhí)行即可 如下圖: 該文章在 2023/3/29 16:19:59 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |